2019年4月7日 星期日

[JAVA程式語言] 例題3-1 求sin(x)的微分

[JAVA程式語言] 例題3-1 求sin(x)的微分

向前差  f'(x) =  ( f(x+h) - f(x)) / h
向後差  f'(x) =  ( f(x) - f(x-h)) / h
中央差  f'(x) =  0.5 * ( f(x+h) - f(x-h)) / h

/* ex3-1.java is used for solving nonlinear equation
 * based on Fixed-Point Algorithm g(x)=x with initial
 * approximation P0.
[JAVA程式語言] 例題3-1 求sin(x)的微分
*/

public class Main {

    double fx(double x1) {
        return (Math.sin(x1));
    }

    double FordDiff(double x1, double h1){ // 前差微分
        return (( fx(x1+h1) - fx(x1) ) / h1);
    }

    double BackDiff(double x1, double h1){ // 後差微分
        return (( fx(x1) - fx(x1-h1) ) / h1);
    }

    double MidDiff(double x1, double h1){ // 中差微分
        return 0.5 * ( fx(x1+h1) - fx(x1-h1) ) / h1;
    }

    public static void main(String args[]){
        Main fun = new Main();
     
        double x,h,delta,delta2;
        x=1.0;
        double ans = Math.cos(x); // 答案
        double cal;
        double hx[] = {0.001 , 0.005 ,0.01 , 0.05 , 0.1} ;
        int i=0;
     
        for(i=0;i<=4;i++){
            h=hx[i];
            System.out.printf("The h=%2.3f\n",h);
            System.out.printf("The real value cos(1.0)=%2.5f\n",ans);
     
            cal = fun.FordDiff(x, h);
            delta = cal - ans;
            delta2= (ans-cal)/cal*100;
            System.out.printf("FordDiff : %2.6f, delta = %2.6f , error = %2.4f\n",cal,delta ,delta2);

            cal = fun.BackDiff(x, h);
            delta = cal - ans;
            delta2= (ans-cal)/cal*100;
            System.out.printf("BackDiff : %2.6f, delta = %2.6f , error = %2.4f\n",cal,delta ,delta2);
     
            cal = fun.MidDiff(x, h);
            delta = cal - ans;
            delta2= (ans-cal)/cal*100;
            System.out.printf("MidDiff  : %2.6f, delta = %2.6f , error = %2.4f\n\n\n",cal,delta ,delta2);
        } 
    }
}


輸出畫面
The h=0.001
The real value cos(1.0)=0.54030
FordDiff : 0.539881, delta = -0.000421 , error = 0.0779
BackDiff : 0.540723, delta = 0.000421 , error = -0.0778
MidDiff  : 0.540302, delta = -0.000000 , error = 0.0000


The h=0.005
The real value cos(1.0)=0.54030
FordDiff : 0.538196, delta = -0.002106 , error = 0.3913
BackDiff : 0.542404, delta = 0.002101 , error = -0.3874
MidDiff  : 0.540300, delta = -0.000002 , error = 0.0004


The h=0.010
The real value cos(1.0)=0.54030
FordDiff : 0.536086, delta = -0.004216 , error = 0.7865
BackDiff : 0.544501, delta = 0.004198 , error = -0.7710
MidDiff  : 0.540293, delta = -0.000009 , error = 0.0017


The h=0.050
The real value cos(1.0)=0.54030
FordDiff : 0.519045, delta = -0.021257 , error = 4.0955
BackDiff : 0.561110, delta = 0.020807 , error = -3.7082
MidDiff  : 0.540077, delta = -0.000225 , error = 0.0417


The h=0.100
The real value cos(1.0)=0.54030
FordDiff : 0.497364, delta = -0.042939 , error = 8.6332
BackDiff : 0.581441, delta = 0.041138 , error = -7.0753
MidDiff  : 0.539402, delta = -0.000900 , error = 0.1669

沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...