2019年2月14日 星期四

習題2-4-a 請用Newton-Raphson 求方程式的根 取 ε =0.00001 (a) cos(x) * cosh(x) + 1 =0

習題2-4-a 請用Newton-Raphson 求方程式的根
取 ε =0.00001

(a) cos(x) * cosh(x) + 1 =0

Python程式
'''
/* Pr2-4.py is used for solving nonlinear equation f(x)=0
 * based on Newton-Raphson Method with initial approximation
 * p0.
 */

 d cos(x) /dx = -sin(x)
 (coshx)′=sinhx     

 '''
import math
Max=100
TOL=  0.00001
def f(x):
    return (math.cos(x) * math.cosh(x)+1.0)

def  ff(x):
    return (-1.0* math.sin(x)* math.sinh(x) )

i=1
x0=1.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.7f , | x-x0 |=%10.7f\n" %(x,abs(x-x0)))
        break
   
    i=i+1
    x0=x

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



輸出畫面
======== RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/pr2-4-a.py ==========
 0    1.0000000

 1    2.8543172

 2   -0.1450062

 3   94.9686696

 4   96.1067873

 5   95.8103214

 6   95.8185761

Root=95.8185759 , | x-x0 |= 0.0000002

Newton-Raphson Method failed after  7  iterations!!!

>>>

沒有留言:

張貼留言

WOKWI DHT22 & LED , Node-Red + SQLite database

 WOKWI DHT22 & LED , Node-Red + SQLite database Node-Red程式 [{"id":"6f0240353e534bbd","type":"comment&...