C語言 例題2-4使用Newton-Raphson理則解 cos(x) - x =0 非線性方程式解
'''
使用Newton-Raphson理則解 cos(x) - x =0 非線性方程式解
/* ex2-4.c is used for solving nonlinear equation f(x)=0
* based on Newton-Raphson Method with initial approximation
* p0.
*/
使用Newton-Raphson理則解 cos(x) - x =0 非線性方程式解
'''
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: F:\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!!!
>>>
訂閱:
張貼留言 (Atom)
Fultter APP控制 WOKWI ESP32 RFID+LED (使用flutter_windows_3.19.1-stable Visual studio 1.108.2 )
Fultter APP控制 WOKWI ESP32 RFID+LED (使用flutter_windows_3.19.1-stable Visual studio 1.108.2 ) 1) Android studio 環境設定 2) Visual studio 1.108...
-
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...
-
課程講義 下載 11/20 1) PPT 下載 + 程式下載 http://www.mediafire.com/file/cru4py7e8pptfda/106%E5%8B%A4%E7%9B%8A2-1.rar 11/27 2) PPT 下載...
沒有留言:
張貼留言