[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
訂閱:
張貼留言 (Atom)
Messaging API作為替代方案
LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...
-
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 + * 後 序式的運算 例如: 運算時由 後序式的...
沒有留言:
張貼留言