/*================================================================== Written by: Roberto Mojica Last revision: 10 February 2007 -------------------------------------------------------------------- Program that implements the brute force method (with bisectional method inside) to find multiple roots of a function f(x) on [a,b]. ==================================================================*/ #include #include #include using namespace std; //configuration #define PI 3.1415926535897932385 #define MAX_NUM_OF_ITERATIONS 10000 #define LEFT_END_POINT 1 #define RIGHT_END_POINT 6 #define STEP .0001 #define EPS 1.0e-6 //#define _Fx(X) log(X*X+2)*cos(X)+sin(X) //#define _Fx(X) X*X*X-5*X*X+7*X-3 #define _Fx(X) sin(X*X)*cos(X+1)/(X*X+1) double f(double x) {return (_Fx(x));} int main() { cout.setf(ios::fixed | ios::showpoint); cout.precision(10); double xO,xL=LEFT_END_POINT,xR=LEFT_END_POINT+STEP; int iterations=0; while(xL < RIGHT_END_POINT) { //cout << "xL = " << xL << " xR = " << xR << endl; if(f(xL)*f(xR) <= 0) { while (fabs(xR-xL) >= EPS && (iterations++) <= MAX_NUM_OF_ITERATIONS) { xO=(xL+xR)/2.0; if((f(xL)*f(xO)) <= 0) xR=xO; else xL=xO; } cout << "xO = " << xO << endl; } xL=xR; xR+=STEP+EPS; } cout << "iterations = " << iterations << endl; system("pause"); return iterations; }