2018年12月28日 星期五

使用Newton-Raphson理則解 cos(x) - x =0 非線性方程式解 PYTHON語言

使用Newton-Raphson理則解 cos(x) - x =0 非線性方程式解 PYTHON語言

'''
/* ex2-4.c is used for solving nonlinear equation f(x)=0
 * based on Newton-Raphson Method with initial approximation
 * p0.
 */
'''
from math import *
import sys

MAX=100
TOL=0.001

def f(x):
    tmp=0.0+cos(x)-x
    return tmp


def ff(x):
    tmp=0.0-sin(x)-1.0
    return tmp

i=1
x0=1.0
x=0.0

while(i<=MAX):
    x=x0-f(x0)/ff(x0)
    print("{%2d}   {%10.7f}\n" %(i-1,x0))
   
    if (abs(x-x0) < TOL):
        print("Root=%10.7lf x-x0=%10.7lf\n" %(x,fabs(x-x0)))
        break
    i=i+1
    x0=x

print("Newton-Raphson Method failed after {%2d} \iterations!!!\n" %(i))



======== RESTART: H:/2018-09勤益科大數值分析/數值分析/PYTHON/EX2-4.py ============
{ 0}   { 1.0000000}

{ 1}   { 0.7503639}

{ 2}   { 0.7391129}

Root= 0.7390851 x-x0= 0.0000278

Newton-Raphson Method failed after { 3} \iterations!!!

沒有留言:

張貼留言

ESP32 微控制器 採集的環境數據,經由 MQTT 協定 轉換並寫入工業標準的 Modbus 暫存器 中

  一個典型的 IoT(物聯網)與工業自動化系統整合(SCADA/機台連線) 的架構圖。它透過 Node-RED 作為核心資料網關,將前端 ESP32 微控制器 採集的環境數據,經由 MQTT 協定 轉換並寫入工業標準的 Modbus 暫存器 中。 1. 各模組功能拆解...