2019年4月6日 星期六

[JAVA程式語言]Fixed-point 定點迴路法 改寫的方程式不同而無法收斂的情況(振盪的情形+-+-....)

[JAVA程式語言]Fixed-point 定點迴路法 改寫的方程式不同而無法收斂的情況(振盪的情形+-+-....)



/* ex2-9.java is used for solving nonlinear equation
 * based on Fixed-Point Algorithm g(x)=x with initial
 * approximation P0.
*/

public class Main {

    double gx(double x1) {
     return (Math.pow(x1,3)+4*Math.pow(x1,2)-10);
 }
 
    double gx1(double x1) {
     return Math.sqrt((5-0.5*x1*x1*x1));
 }

    double gx2(double x1) {
     return Math.sqrt(10/(4+x1));
 }



 public static void main(String args[]){
        Main fun = new Main();
        final int MAX = 20;  /* maximum iterations */
        final double TOL =0.00001;/* maximum iterations */
 
        int i=1;
        double x0,x;
        x0=1.4;
        System.out.printf("x0=%1.2f , The function valuet=%10.7f\n",x0,fun.gx(x0));
        x0=1.3;
        System.out.printf("x0=%1.2f , The function valuet=%10.7f\n\n\n",x0,fun.gx(x0));
        System.out.printf(" i\t  x0\t       f(x0)\t      x\t      f(x)\n");
        x0=1.6;
        while(i<=MAX) {
            x=fun.gx1(x0);
            System.out.printf("%2d  %10.7f",i-1,x0);
            System.out.printf("\t %f \t %f \t %f\n", fun.gx(x0), x, fun.gx(x));
            if(Math.abs(x-x0) < TOL) {
                System.out.printf("%f \t %f \t %f \t %f\n",x0, fun.gx(x0), x, fun.gx(x));
                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");
        System.out.printf(" i\t  x0\t       f(x0)\t      x\t      f(x)\n");
     
        i=1;
        x0=1.5;
        while(i<=MAX) {
            x=fun.gx2(x0);
            System.out.printf("%2d  %10.7f",i-1,x0);
            System.out.printf("\t %f \t %f \t %f\n", fun.gx(x0), x, fun.gx(x));
            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 faileds!!!\n");
        } 
     
     
     
 }
}

 輸出畫面
x0=1.40 , The function valuet= 0.5840000
x0=1.30 , The function valuet=-1.0430000


 i   x0            f(x0)                x             f(x)
 0   1.6000000 4.336000 1.718139 6.879945
 1   1.7181385 6.879945 1.569722 3.723949
 2   1.5697221 3.723949 1.751023 7.633100
 3   1.7510228 7.633100 1.521713 2.786142
 4   1.5217133 2.786142 1.799486 8.779617
 5   1.7994865 8.779617 1.444470 1.359860
 6   1.4444705 1.359860 1.868973 10.500674
 7   1.8689729 10.500674 1.317491 -0.769990
 8   1.3174911 -0.769990 1.963813 12.999807
 9   1.9638128 12.999807 1.101462 -3.810813
10   1.1014619 -3.810813 2.081308 16.343273
11   2.0813080 16.343273 0.701463 -7.686645
12   0.7014627 -7.686645 2.197140 19.916212
13   2.1971396 19.916212 NaN NaN
14         NaN NaN NaN NaN
15         NaN NaN NaN NaN
16         NaN NaN NaN NaN
17         NaN NaN NaN NaN
18         NaN NaN NaN NaN
19         NaN NaN NaN NaN
Fixed-point Method faileds!!!



 i   x0        f(x0)       x       f(x)
 0   1.5000000 2.375000 1.348400 -0.275637
 1   1.3483997 -0.275637 1.367376 0.035481
 2   1.3673764 0.035481 1.364957 -0.004508
 3   1.3649570 -0.004508 1.365265 0.000574
 4   1.3652647 0.000574 1.365226 -0.000073
 5   1.3652256 -0.000073 1.365231 0.000009

Count= 7 , The Root= 1.3652306  x-x0= 0.0000050

EXCEL計算結果
1.5 3.3125 1.820027472
1.820027 1.985579499 1.409105922
1.409106 3.601054085 1.897644352
1.897644 1.583240028 1.258268663
1.258269 4.003929343 2.000982095
2.000982 0.994104538 0.997047911
0.997048 4.504415073 2.122360731
2.122361 0.220003166 0.46904495
0.469045 4.948404313 2.224500913
2.224501 -0.503864957 #NUM!
#NUM! #NUM! #NUM!
#NUM! #NUM! #NUM!
#NUM! #NUM! #NUM!
#NUM! #NUM! #NUM!
#NUM! #NUM! #NUM!
#NUM! #NUM! #NUM!
#NUM! #NUM! #NUM!
#NUM! #NUM! #NUM!
#NUM! #NUM! #NUM!

沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...