I was asked to code a root finding program which uses the secant method. Although the exercise itself isn't any hard I am passing only 2 of 5 declared examples and I have no idea why. My function looks like this:
template <class T >
bool secant ( double &x , T f , double x0 , double x1 , double eps , int n = 1000){
x = x0;
double c, xp, xInter, xm;
if(eps<=0){cout << "Wrong epsilon" << endl; return false;}
if(n<=0){cout << "Wrong n" << endl; return false;}
if(f==nullptr){cout <<"No function" << endl; return false;}
if (f(x0)*f(x1)<0){
do{
//intermediate value
xInter = (x0*f(x1)-x1*f(x0))/(f(x1)-f(x0));
cout << n << endl;
c = f(xInter)*f(x0);
x0 = x1;
x1 = xInter;
n--;
if (c==0){
break;
}
if (n==0){
cout << "Number of iterations exceeded.";
break;
}
xm = (x0*f(x1)-x1*f(x0))/(f(x1)-f(x0));
} while(abs(xm - xInter)>= eps);
}
x = xInter;
return true;
}
And is called in main like so:
double x;
for ( auto fx : { f1 , f2 , f3 , f4 , f5 }) {
for ( auto eps : {0.1 , 0.01 , 0.001 , 0.0001 , 0.0000001}) {
if ( secant (x , fx , 0.1 , 3 , eps )) {
cout << setprecision (8) << "eps = " << eps
<< "\t root = " << x << endl;
} else {
cout << " Unable to find root " <<endl;
}
}
cout << endl ;
}
There are 5 functions to test:
- x^2
- x^2-2
- e^x+x-1
- log(x)-x+1
- sin(x+0.5)
Both 2nd and 5th functions get good results (1.4142136 and 2.6415926, as expected) but every other function returns the same numbers being:
- (for the epsilon = 0.1): 8.0...e-307
- (for every other epsilon): 1.5...e+231
What am I doing wrong?