[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
訂閱:
張貼留言 (Atom)
113 學年度第 1 學期 RFID應用課程 Arduino程式
113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...
-
python pip 不是内部或外部命令 -- 解決方法 要安裝 Pyqt5 1. 首先,開啟命令提示字元。 2. 輸入 pip3 install pyqt5 好像不能執行 ! ! 錯誤顯示 : ‘ pip3 ’ 不是內部或外部命令、可執行的程式或批...
-
課程講義 下載 11/20 1) PPT 下載 + 程式下載 http://www.mediafire.com/file/cru4py7e8pptfda/106%E5%8B%A4%E7%9B%8A2-1.rar 11/27 2) PPT 下載...
-
• 認 識 PreFix、InFix、PostFix PreFix(前序式):* + 1 2 + 3 4 InFix(中序式): (1+2)*(3+4) PostFix(後序式):1 2 + 3 4 + * 後 序式的運算 例如: 運算時由 後序式的...
沒有留言:
張貼留言