方程式二分法求解 f(x) (0.0000115*pow(x,2)+0.000014*pow(x,1.5)-0.01962)
/* ex2-2.c Bisection Method is used for solving the
* nonlinear equation f(x)=0, its root is between a and b
*/
#include <stdio.h>
#include <math.h>
#define MAX 50 /* maximum iterations */
#define TOL 0.001 /* tolerance */
#define f(x) (0.0000115*pow(x,2)+0.000014*pow(x,1.5)-0.01962)
void main()
{
double x,a,b,p,h;
int i=1;
a=37;
b=38;
printf(" i a(i) b(i) p(i) f(p(i)) h(i)\n");
while(i<=MAX)
{
p=(a+b)/2.0;
h=fabs((b-a)/2.0);
printf("%2d %9.6lf %9.6lf %9.6lf %9.6lf %9.6lf\n",
i,a,b,p,f(p),h);
if(fabs(f(p))==0 || h<TOL)
{
printf("%2d iterations!!!\n",i);
printf(" The Root=%9.6lf f(%9.6lf)=%9.6lf\n",p,p,f(p));
exit(0);
}
else
{
if(f(a)*f(p) >0)
a=p;
else if(f(b)*f(p) >0)
b=p;
i++;
}
}
printf("Bisection Method failed after %d iterations!!!\n",i);
return;
}
i a(i) b(i) p(i) f(p(i)) h(i)
1 37.000000 38.000000 37.500000 -0.000233 0.500000
2 37.500000 38.000000 37.750000 0.000015 0.250000
3 37.500000 37.750000 37.625000 -0.000109 0.125000
4 37.625000 37.750000 37.687500 -0.000047 0.062500
5 37.687500 37.750000 37.718750 -0.000016 0.031250
6 37.718750 37.750000 37.734375 -0.000000 0.015625
7 37.734375 37.750000 37.742188 0.000008 0.007812
8 37.734375 37.742188 37.738281 0.000004 0.003906
9 37.734375 37.738281 37.736328 0.000002 0.001953
10 37.734375 37.736328 37.735352 0.000001 0.000977
10 iterations!!!
The Root=37.735352 f(37.735352)= 0.000001
沒有留言:
張貼留言