/* ===================================================================*\
filename: bisection.c
THE PROGRAM IS TO IMPLEMENT THE BISECTION METHOD FOR ROOT FINDING
PROGRAMMER: RiNN
INSTRUCTOR: Dr. Robert Sczech
Class: CS102 S98
Date: Tuesday February 10, 1998
\*=====================================================================*/
#include //iostream Header
#include //math Header
// GLOBAL VARABLES
const double delta = 0.00001 ; //CONSTANT DELTA
const double epsilon = 0.000001 ; //CONSTANT EPSILON
//PROTOTYPES
double bisect(double (*f)(double x), double a, double b);
double FuncOne (double x); //f(x)=x2-2, [a,b]=[1,2]
double FuncTwo (double x); //f(x)=cos(x/2), [a,b]=[3.0,3.2]
double FuncThree(double x); //f(x)=ln(x)-1, [a,b]=[2.71,2.72]
void main ()
{ cout << "\n" << bisect(FuncOne, 1 ,2 ) << endl;
cout << "\n" << bisect(FuncTwo, 3.0 ,3.2 ) << endl;
cout << "\n" << bisect(FuncThree,2.71 ,2.72) << endl;
return 0;
}
double bisect(double (*f)(double x), double a, double b){
double mid = ((a+b)/2); //Midpoint declared and calculated
cout << "\tBisect: interval=[" << a << " , " << b << "]" << endl;
if ((fabs( a - b) < delta) || (fabs(f(mid)) < epsilon))
return (mid = (( a+b )/2)); //Stop when Values are < delta/epsilon
else if ( f(a)*f(mid) < 0) //Is point in between the a & mid?
return bisect(f, a, mid); //Return if it is true
else
return bisect(f, mid,b); //Return if the point is between mid & b
}
double FuncOne(double x){
return ((x * x) -2);
}
double FuncTwo(double x){
return (cos(x/2));
}
double FuncThree(double x){
return ( log(x) - 1);
}