//for implementation of regula Secants Method for // solving equations #include #include #define MAX_ITER 1000000 #define err 0.00001 double f(double x) { return x*x*x - x - 1; } // Prints root of f(x) in interval [a, b] void regulaFalsi(double a, double b) { int count=0; if (f(a) * f(b) >= 0) { printf("You have not assumed right a and b\n"); return; } printf("finding the approx root of eqn. by Secant method\n"); double c = a; // Initialize result printf("\na\t\tb\t\tf(a)\t\tf(b)\t\tc\t\tf(c)\n"); for (int i=0; i < MAX_ITER; i++) { printf("%.4f \t\t%.4f \t\t%.4f\t\t%.4f",a,b,f(a),f(b)); // Find the point that touches x axis c = (a*f(b) - b*f(a))/ (f(b) - f(a)); printf("\t\t%.4f\t\t%.4f\n",c,f(c)); // Check if the above found point is root if (f(c)<=0.00001 && f(c)>=-0.00001) { // if(fabs(a-c)>err) break; } // Decide the side to repeat the steps a=b; b=c; ++count; } printf( "The value of root is :%.4f ", c); printf(" the no of iterations required is %d",(count+1)); } void GetRootRange(double *L, double *U) { double y; for(y=-999999.0l;y<999999.0l;y=y+0.1) { if(f(y)*f(y+0.1)<0.0) {fflush(stdin); *L=y;*U=y+0.1; return; } } } // Driver program to test above ftion :"" int main() { // Initial values assumed double a , b ; GetRootRange(&a,&b); regulaFalsi(a, b); return 0; }