Bisection Method Example In C Language | C Program to show Bisection method
#include#include #include //bisection method to solve the equation x^4-x-10=0// float f(float x) { return(pow(x,4)-pow(x,1)-10); } void main() { float x,x1,a,b,err=0.00005; //err is the max error allowed int i=1,n; clrscr(); printf("enter the values of a,b and maximum iterations\n"); scanf("%f%f%d",&a,&b,&n); x=(a+b)/2; printf("iteration no. %d x=%10.5f\n",i,x); i++; while(i<n) { if(f(a)*f(x)<0) //checking for the signs b=x; //new interval (a,x)// else a=x; //new interval (x,b)// x=(a+b)/2; printf("iteration no. %d x=%10.5f\n",i,x); if(fabs(x1-x)<err) { printf("\nafter %d iterations, the value of root is %f\n",i,x); break; } x1=x; i++; } if(i>=n) { printf("\nsolution does not exist as iterations are not sufficient"); } getch(); }
No comments:
Post a Comment