//http://www.timestocome.com //linda macphee-cobb //solve an de function using bisection #include #include #define PI 3.14159 void main (void){ double middle, fleft, fright, fmiddle; double error = 1.0, left = 0.0, right = PI/2.0, tol = 0.001; //function:> f(x) = cos(x) - x fleft = cos(left) - left; fright = cos(right) - right; while(error > tol){ middle = (left+right)/2.0; fmiddle = cos(middle)-middle; //which half of interval has the root if((fleft * fmiddle) < 0){ //root is in left right = middle; fright = fmiddle; }else{ //root is in right left = middle; fleft = fmiddle; } //root now between new left and right //so check for error tolerance and redo or //return answer error = sqrt(((right-left)/middle)*((right-left)/middle)); } printf("\n the solution is %lf", middle); getchar(); }