2019年4月7日 星期日

[JAVA程式語言]Program for Fixed Point Iteration Method using Java Programming Language

Program for Fixed Point Iteration Method using Java Programming Language

Algorithm of fixed point Iteration method



/* 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 f(double x1) {
     return (Math.cos(x1)-3*x1+1);
 }

    double g(double x1) {
     return ( (1+Math.cos(x1))/ 3 );
 }


 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;
        int step=1, N=10;
        double  x0, x1, e;
        x0=1.0;
        N=10;
        e=0.00001;
        /* Implementing Fixed Point Iteration */
    System.out.printf("\nStep\tx0\t\t   f(x0)\t\tx1\t\t   f(x1)\n");
     
        do {
    x1 = fun.g(x0);
    System.out.printf("%d\t%f\t%f\t%f\t%f\n",step, x0, fun.f(x0), x1, fun.f(x1));
    step = step + 1;
    if(step>N) {
    System.out.printf("Not Convergent.");
    break;
    }
        x0 = x1;

    }while( Math.abs(fun.f(x1)) > e);

    System.out.printf("\nRoot is %f", x1);
     
    }
}

輸出畫面
Step      x0    f(x0)     x1       f(x1)
1 1.000000   -1.459698 0.513434     0.330761
2 0.513434   0.330761 0.623688     -0.059333
3 0.623688   -0.059333 0.603910     0.011391
4 0.603910   0.011391 0.607707     -0.002162
5 0.607707   -0.002162 0.606986     0.000411
6 0.606986   0.000411 0.607124     -0.000078
7 0.607124   -0.000078 0.607097     0.000015
8 0.607097   0.000015 0.607102     -0.000003

Root is 0.607102

沒有留言:

張貼留言

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

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