/* ex2-9.java is used for solving nonlinear equation
* based on Fixed-Point Algorithm g(x)=x with initial
* approximation P0.
例題2-9 定點回路法 求非線性方程式 f(x)=1/5^x - x = 0 找出f(x)=0的根=?
*/
public class Main {
double gx(double x1) {
return (1/Math.pow(5,x1));
}
public static void main(String args[]){
Main fun = new Main();
final int MAX = 50; /* maximum iterations */
final double TOL =0.0001;/* maximum iterations */
int i=1;
double x0,x;
x0=0.45;
while(i<=MAX) {
x=fun.gx(x0);
System.out.printf("%2d %10.7f\n",i-1,x0);
if(Math.abs(x-x0) < TOL) {
System.out.printf("The Root=%10.7f x-x0=%10.7f\n",x,Math.abs(x-x0));
break;
}
else
{
i++;
x0=x;
}
}
if(i>MAX){
System.out.printf("Fixed-point Method faileds!!!\n");
}
}
}
輸出畫面
0 0.4500000
1 0.4846894
2 0.4583705
3 0.4782035
4 0.4631803
5 0.4745160
6 0.4659374
7 0.4724151
8 0.4675155
9 0.4712167
10 0.4684181
11 0.4705327
12 0.4689340
13 0.4701421
14 0.4692289
15 0.4699191
16 0.4693974
17 0.4697917
18 0.4694936
19 0.4697189
20 0.4695486
21 0.4696773
The Root= 0.4695801 x-x0= 0.0000973
沒有留言:
張貼留言