Hey there, first time posting. I have a program that is kinda put together from a program to generate polynomials and evaluate them and a program to do bisection to solve for the root.

It may be a mess. The problem is, i believe, when I try to use the evalPoly in the bisection function and it gives a DOMAIN error, and sometimes just doesn't do anything (pauses when executed).

Any insight would be helpful.

#include<iostream>
#include<cmath>
#include <math.h>

#define FALSE 0
#define TRUE 1
using namespace std;
typedef short Boolean;

class Polynomial {
    
  private:
    int n;
    float *coeff;
    
  public:
    zeroPoly();
    void ReadPoly();
    void PrintPoly();
    double EvalPoly(double x);
};





zeroPoly(){
    return 0;
}

//ReadPoly -- read and store the contents of a polynomial along with its degree.

void Polynomial::ReadPoly(){
    
    Boolean b;                
    int i;

    // Enter the degree of the polynomial
    // See if the degree is postive integer
    b = FALSE;
    while (!b){
        
        cout << "What is the degree of your polynomial? ";
        cin >> n;
        
        if (n < 0){
            cout << "Only positve integer values for the degree!"<<endl<<endl;
                }
        else{
            b = TRUE;
        }
    }
    
    // Allocate memory for the coeff array
    coeff = new float[n+1];
    
    // Enter the coefficients of the polynomial
    for (i = n; i >= 0; i--){
        cout << "Enter the coefficient for the degree " << i << " term: ";
        cin >> coeff[i];
    }
}    



// Print out the Polynomial

void Polynomial::PrintPoly(){
    
    int i;

    cout << "f(x) =";
    for (i = n; i >= 0; i--){
        cout << " + " << coeff[i] << "x^" << i;
    }
    
    cout <<endl<<endl;
}



double Polynomial::EvalPoly(double x){
    
    double temp;
    int j;
    double sum = 0;
    
    for(j=0; j<=n; j++){
        temp = (pow(x, j)*(coeff[j]));
        sum = sum + temp;
    }
    
    return sum;

}



double bisect1(double xL, double xR, double accuracy, double maxiter){
	
	int i;
	double c;
        Polynomial poly;
	
        if(xL*xR < 0){
		for(i=0; i<maxiter; i++){
			
			c=(xL + xR)/2.0;
			if(fabs((poly.EvalPoly(c)) < accuracy )){
			break;		//break if solution becomes close to the convergence point(root)
			}
			
		if((poly.EvalPoly(xL))*(poly.EvalPoly(c))<0.0){
			xR = c;
		}
		else xL = c;
		}
	}
	return c;
}



int main(void){
	
	double xL, xR, root1, accuracy, maxiter;
	
        Polynomial poly;
        poly.ReadPoly();
        
	cout<<"Enter 2 bracketing values : "<<endl;	//define the two starting points
	cout<<"xL : ";
	cin>>xL;
	cout<<"xR : ";
	cin>>xR;
	cout<<"Enter the accuracy : ";
	cin>>accuracy;
	cout<<"Enter the maximum iteration value : ";
	cin>>maxiter;
	cout<<endl;
	
	root1 = bisect1(xL, xR, accuracy, maxiter);

	
	if(xL*xR<0){	
		if(fabs(poly.EvalPoly(root1)) > accuracy ){
			cout<<"ERROR: Could not compute root of X^2 - 4 within accuracy."<<endl<<endl<<endl;
		}
		else{
			cout<<"The root for X^2 - 4 is : "<<root1<<endl<<endl;
		}
		
	}
	else{
		cout << "ERROR: Improper interval, try again!" <<endl<<endl;
	}
	return 0;
}

Hi, first if i use C++ i also use C++ tools, like:

instead of:
#define FALSE 0
#define TRUE 1
typedef short Boolean;

this:
//#define FALSE 0 // == false
//#define TRUE 1 // == true
//typedef short Boolean; // == bool

second:

public:
zeroPoly();

BETTER:

public:
int zeroPoly(); // return type is int!

int Polynomial::zeroPoly(){
return 0;
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.