-0.5 到 0.0之間的解
a=-0.5;
b=0.0;
#define TOL 0.0001 /* tolerance */
i a(i) b(i) p(i) f(p(i)) h(i)
1 -0.500000 0.000000 -0.250000 0.591301 0.250000
2 -0.500000 -0.250000 -0.375000 0.265414 0.125000
3 -0.500000 -0.375000 -0.437500 0.071430 0.062500
4 -0.500000 -0.437500 -0.468750 -0.033396 0.031250
5 -0.468750 -0.437500 -0.453125 0.019672 0.015625
6 -0.468750 -0.453125 -0.460938 -0.006698 0.007812
7 -0.460938 -0.453125 -0.457031 0.006528 0.003906
8 -0.460938 -0.457031 -0.458984 -0.000075 0.001953
9 -0.458984 -0.457031 -0.458008 0.003229 0.000977
10 -0.458984 -0.458008 -0.458496 0.001578 0.000488
11 -0.458984 -0.458496 -0.458740 0.000752 0.000244
12 -0.458984 -0.458740 -0.458862 0.000338 0.000122
13 -0.458984 -0.458862 -0.458923 0.000132 0.000061
13 iterations!!!
The Root=-0.458923 f(-0.458923)= 0.000132
0.5 到 1.0之間的解
a=0.5;
b=1.0;
#define TOL 0.0001 /* tolerance */
1 0.500000 1.000000 0.750000 0.429500 0.250000
2 0.750000 1.000000 0.875000 0.102000 0.125000
3 0.875000 1.000000 0.937500 -0.083129 0.062500
4 0.875000 0.937500 0.906250 0.011157 0.031250
5 0.906250 0.937500 0.921875 -0.035561 0.015625
6 0.906250 0.921875 0.914062 -0.012095 0.007812
7 0.906250 0.914062 0.910156 -0.000442 0.003906
8 0.906250 0.910156 0.908203 0.005364 0.001953
9 0.908203 0.910156 0.909180 0.002462 0.000977
10 0.909180 0.910156 0.909668 0.001010 0.000488
11 0.909668 0.910156 0.909912 0.000284 0.000244
12 0.909912 0.910156 0.910034 -0.000079 0.000122
13 0.909912 0.910034 0.909973 0.000102 0.000061
13 iterations!!!
The Root= 0.909973 f( 0.909973)= 0.000102
3.5 到 4.0之間的解
a=3.5;
b=4.0;
#define TOL 0.0001 /* tolerance */
i a(i) b(i) p(i) f(p(i)) h(i)
1 3.500000 4.000000 3.750000 0.333582 0.250000
2 3.500000 3.750000 3.625000 -1.897152 0.125000
3 3.625000 3.750000 3.687500 -0.848109 0.062500
4 3.687500 3.750000 3.718750 -0.274459 0.031250
5 3.718750 3.750000 3.734375 0.025184 0.015625
6 3.718750 3.734375 3.726562 -0.125722 0.007812
7 3.726562 3.734375 3.730469 -0.050541 0.003906
8 3.730469 3.734375 3.732422 -0.012747 0.001953
9 3.732422 3.734375 3.733398 0.006201 0.000977
10 3.732422 3.733398 3.732910 -0.003277 0.000488
11 3.732910 3.733398 3.733154 0.001461 0.000244
12 3.732910 3.733154 3.733032 -0.000908 0.000122
13 3.733032 3.733154 3.733093 0.000276 0.000061
13 iterations!!!
The Root= 3.733093 f( 3.733093)= 0.000276
/* 習題 ex2-2.c is used for finding out the domains
* which exist the roots of f(x).
*/
/* 習題2-1.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.0001 /* tolerance */
#define f(x) (exp(x)-3*pow(x,2))
void main()
{
double x,a,b,p,h;
int i=1;
a=-0.5;
b=0.0;
//a=0.5;
//b=1.0;
//a=3.5;
//b=04.0;
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;
}
沒有留言:
張貼留言