/* ex2-9.java is used for solving nonlinear equation
* based on Fixed-Point Algorithm g(x)=x with initial
* approximation P0.
例題2-8 定點回路法 求非線性方程式 f(x)=exp(x) - 3x^2 = 0 找出f(x)=0的根=?
*/
public class Main {
double gx1(double x1) {
return ( Math.pow( (Math.exp(x1)/3) , 0.5));
}
double gx2(double x1) {
return (-1* Math.pow( (Math.exp(x1)/3) , 0.5));
}
public static void main(String args[]){
Main fun = new Main();
final int MAX = 50; /* maximum iterations */
final double TOL =0.001;/* maximum iterations */
int i=1;
double x0,x;
x0=0.57735;
while(i<=MAX) {
x=fun.gx1(x0);
System.out.printf("%2d %10.7f\n",i-1,x0);
if(Math.abs(x-x0) < TOL) {
System.out.printf("Count=%2d , The Root=%10.7f x-x0=%10.7f\n",i+1,x,Math.abs(x-x0));
break;
}
else
{
i++;
x0=x;
}
}
if(i>MAX){
System.out.printf("Fixed-point Method faileds!!!\n");
}
System.out.printf("\n\n\n");
i=1;
x0=0.0;
while(i<=MAX) {
x=fun.gx2(x0);
System.out.printf("%2d %10.7f\n",i-1,x0);
if(Math.abs(x-x0) < TOL) {
System.out.printf("Count=%2d , The Root=%10.7f x-x0=%10.7f\n",i+1,x,Math.abs(x-x0));
break;
}
else
{
i++;
x0=x;
}
}
if(i>MAX){
System.out.printf("Fixed-point Method faileds!!!\n");
}
}
}
輸出畫面
0 0.5773500
1 0.7705651
2 0.8487220
3 0.8825453
4 0.8975975
5 0.9043784
6 0.9074499
7 0.9088446
Count= 9 , The Root= 0.9094786 x-x0= 0.0006340
0 0.0000000
1 -0.5773503
2 -0.4325829
3 -0.4650559
4 -0.4575660
5 -0.4592828
Count= 7 , The Root=-0.4588887 x-x0= 0.0003941
沒有留言:
張貼留言