HI!
I wrote a program that finds a root with the bisection method.
The problem is that i cant find the bug :P
here is the code...
can you give me your ideas for solving the problem?
Code:#include<stdio.h> double my_function(double x); double a,b,e,meso,fm,fa,fb; int main() { printf("GIVE {a} AND {b} OF THE INTERVAL [a,b] \n,AND ALSO THE TOLERANCE e:"); printf("EXAMBLE:IF a=1 AND b=2 GIVE 1 2\n"); scanf("%lf%lf%lf ",&a,&b,&e); fa=my_function(a); fb=my_function(b); while((fa>0)&&(fb>0) || (fa<0)&&(fb<0)) { printf("THE VALUES A AND B DO NOT DIFFER IN SIGN\n"); printf(" (+a)*(+b) OR (-a)*(-b) WORNG\n"); printf("GIVE {a} AND {b} OF THE INTERVAL [a,b] \n,AND ALSO THE TOLERANCE e:"); scanf("%lf%lf%lf",&a,&b,&e); fa=my_function(a); fb=my_function(b); } meso=(a+b)/2; while(my_function(meso)!=0 || ((b-a)>e) ) { fm=my_function(meso); fa=my_function(a); fb=my_function(b); if((fa>0)&&(fm>0) || (fa<0)&&(fm<0)) { b=meso; } else { a=meso; } meso=(a+b)/2; } if(my_function(meso)==0) printf("%lf IS THE ROOT OF THE FUNCTION F(X)",meso); else if(((b-a)<e)) printf("THE ROOT APPROXIMATIVE IS %lf",meso); return 0; } double my_function(double x) { double fx; fx=x*x*x+4*x*x-10; return fx; }



LinkBack URL
About LinkBacks


