2019年4月6日 星期六

[JAVA程式語言]例題2-5 已知非線性方程式 (4x-7)/(x-2) 請使用Newton-Raphson方法找出啟動值 {1.5 , 1.625 , 1.875 , 1.95 , 3.0}的根=?

[JAVA程式語言]例題2-5 已知非線性方程式 (4x-7)/(x-2)  請使用Newton-Raphson方法找出啟動值  {1.5 , 1.625 , 1.875 , 1.95 , 3.0}的根=?

                                             f(x)= (4x-7) / (x-2)


   

/* ex2-5.java is used for solving nonlinear equation f(x)=0
 * based on Newton-Raphson Method with initial approximation
 * p0.
例題2-5 已知非線性方程式 (4x-7)/(x-2)  請使用Newton-Raphson方法找出啟動值  {1.5 , 1.625 , 1.875 , 1.95 , 3.0}的根=?
*/

public class Main {

    double fx(double x1) {
    return (  (4*x1-7)/(x1-2));
}

double ffx(double x1) {
    return (-1/ (Math.pow((x1-2),2)) );
}


public static void main(String args[]){
    Main func = new Main();
    final int MAX = 100;  /* maximum iterations */
    final double TOL = 0.001;/* maximum iterations */
        int i=1 , j=0;
        double x , x0;
     
        double x1[] = {1.5 , 1.625 ,1.875 , 1.95 , 3.0} ;

        for (j=0;j<=4;j++){
         
            x0=x1[j];
            System.out.printf("initial value x0=%1.3f\n",x0);
            while(i<=MAX){
                if(func.ffx(x0) == 0){
                    System.out.printf("Newton-Raphson Method can't find roots");
                    break;
                }
             
                x= x0- func.fx(x0)/func.ffx(x0) ;
                System.out.printf("%2d   %10.7f\n",i-1,x0);
                if(Double.isNaN(x0)){
                    System.out.printf("Newton-Raphson Method can't find roots\n\n");
                    break;
                }
             
                if(Math.abs(x-x0) <TOL){
                    System.out.printf("Root=%10.7f x-x0=%10.7f\n\n\n",x,Math.abs(x-x0));
                    break;
                }
             
                i++;
                x0=x;
            }
            if (i==1) {
                System.out.printf("Newton-Raphson Method failed after %2d iterations!!!\n",i); 
            }
            //System.out.printf("j=%2d\n",j);
            i=1;
        } 
     
}
}

輸出畫面
initial value x0=1.500
 0    1.5000000
 1    2.0000000
 2          NaN
Newton-Raphson Method can't find roots

initial value x0=1.625
 0    1.6250000
 1    1.8125000
 2    1.7656250
 3    1.7509766
Root= 1.7500038 x-x0= 0.0009727


initial value x0=1.875
 0    1.8750000
 1    1.8125000
 2    1.7656250
 3    1.7509766
Root= 1.7500038 x-x0= 0.0009727


initial value x0=1.950
 0    1.9500000
 1    1.9100000
 2    1.8524000
 3    1.7919430
 4    1.7570369
 5    1.7501981
Root= 1.7500002 x-x0= 0.0001979


initial value x0=3.000
 0    3.0000000
 1    8.0000000
 2   158.0000000
 3   97658.0000000
 4   38146972658.0000200
 5   5820766091346748000000.0000000
 6   135525271560688430000000000000000000000000000.0000000
 7   73468396926393380000000000000000000000000000000000000000000000000000000000000000000000000.0000000
Newton-Raphson Method can't find roots

沒有留言:

張貼留言

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

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