f(x) = 1.15*10^-5*X^2 + 1.4*10^-5* X ^(1.5) -1.962* 10^-2
請使用 二分法 計算v的值 誤差=0.001
/* ex2-2.java Bisection Method is used for solving the
* nonlinear equation f(x)=0, its root is between a and b
*/
public class Main {
double fx(double x1) {
return (0.0000115*Math.pow(x1,2)+0.000014*Math.pow(x1,1.5)-0.01962);
}
public static void main(String args[]){
Main fun = new Main(); final int MAX = 50; /* maximum iterations */
final double TOL = 0.001;/* maximum iterations */
double x,a,b,p,h;
int i=1;
a=37;
b=38;
System.out.printf(" i a(i) b(i) p(i) f(p(i)) h(i)\n");
while(i<=MAX) {
p=(a+b)/2.0;
h=Math.abs((b-a)/2.0);
System.out.printf("%2d %9.6f %9.6f %9.6f %9.6f %9.6f\n", i,a,b,p,fun.fx(p),h);
if(Math.abs(fun.fx(p))==0 || h<TOL) {
System.out.printf("%2d iterations!!!\n",i);
System.out.printf(" The Root=%9.6lf f(%9.6f)=%9.6f\n",p,p,fun.fx(p));
break;
}
else
{
if(fun.fx(a)*fun.fx(p) >0){
a=p;
}
else if(fun.fx(b)*fun.fx(p) >0) {
b=p;
}
i++;
}
}
System.out.printf("Bisection Method failed after %d iterations!!!\n",i);
}
}
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.007813
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!!!
沒有留言:
張貼留言