2019年2月14日 星期四

習題2-4-c 請用Newton-Raphson 求方程式的根 取 ε =0.00001 (c) log ( (1+x) , 10 ) - x^2 =0

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

(c) log  ( (1+x) , 10 ) - x^2  =0


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


(log (a)  x ) ′ =1/ x*ln(a) , a>0,a≠1

 '''
import math
Max=100
TOL=  0.00001
def f(x):
    return (math.log( (1+x) ,10) - x*x + 0.0)

def  ff(x):
    return ( 1 /  ( (1+x)* math.log(10) ) - 2*x + 0.0  )

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-c.py ===========
 0    1.0000000

 1    0.6079486

 2    0.4352604

 3    0.3779984

 4    0.3697547

 5    0.3695754

Root= 0.3695753 , | x-x0 |= 0.0000001

Newton-Raphson Method failed after  6  iterations!!!

>>> 

沒有留言:

張貼留言

WOKWI DHT22 & LED , Node-Red + SQLite database

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