[JAVA程式語言]例題2-4 使用Newton-Raphson理則解 cos(x) - x =0 非線性方程式解
/* ex2-4.java is used for solving nonlinear equation f(x)=0
* based on Newton-Raphson Method with initial approximation
* p0.
例題2-4 使用Newton-Raphson理則解 cos(x) - x =0 非線性方程式解
*/
public class Main {
double fx(double x1) {
return (Math.cos(x1)-x1);
}
double ffx(double x1) {
return (-Math.sin(x1)-1);
}
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;
double x0,x;
x0=1.0; //假設啟動值x0=1.0
while(i<=MAX){
x= x0- func.fx(x0)/func.ffx(x0) ;
System.out.printf("%2d %10.7f\n",i-1,x0);
if(Math.abs(x-x0) <TOL){
System.out.printf("Root=%10.7f x-x0=%10.7f\n",x,Math.abs(x-x0));
break;
}
i++;
x0=x;
}
System.out.printf("Newton-Raphson Method failed after %2d iterations!!!\n",i);
}
}
輸出畫面
i (x0)
0 1.0000000
1 0.7503639
2 0.7391129
Root= 0.7390851 x-x0= 0.0000278
Newton-Raphson Method failed after 3 iterations!!!
訂閱:
張貼留言 (Atom)
MQTT 協定與 Modbus 通訊的遠端監控與控制系統架構
MQTT 協定與 Modbus 通訊的遠端監控與控制系統架構 這張圖片展示了一個 結合 MQTT 協定與 Modbus 通訊的遠端監控與控制系統架構 (主要透過 Node-RED 進行資料整合)。 系統包含三個核心部分,其運作功能說明如下: 1. ESP32 終端設備(硬體控制層...
-
數位IC設計入門-Verilog combinational logic 8 to 1 Multiplexer 多工器 Behavioral Modeling (& Test Bench) //數位IC設計入門-Verilog combinationa...
-
python pip 不是内部或外部命令 -- 解決方法 要安裝 Pyqt5 1. 首先,開啟命令提示字元。 2. 輸入 pip3 install pyqt5 好像不能執行 ! ! 錯誤顯示 : ‘ pip3 ’ 不是內部或外部命令、可執行的程式或批...
-
Line 發報機 Python TKinter (CONFIG_LINE Message API_2.py) import serial import serial.tools.list_ports import tkinter as tk from tkinter import...
沒有留言:
張貼留言