-
Notifications
You must be signed in to change notification settings - Fork 9
/
newton_derivative.py
65 lines (48 loc) · 1.24 KB
/
newton_derivative.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#-*-coding:utf8-*-
import numpy as np
import matplotlib.pyplot as plt
from sympy import *
def newtonRaphson(func, xl, xu):
maxit=100
es=1.0e-4
test=func(xl)*func(xu)
if test > 0:
print('no sign change')
return [], [], [], []
iter=0
xr=xl
m = symbols('m')
d = Derivative(sqrt(9.81*m/0.25)*tanh(sqrt(9.81*0.25/m)*4)-36, m)
ea=100
while(1):
xrold=xr
xr = np.float(xrold - func(xrold)/d.doit().subs({m:xrold}))
if xr != 0:
ea=np.float(np.abs(np.float(xr)-np.float(xrold))/np.float(xr))*100
if np.int(ea<=es) | np.int(iter>=maxit):
break
iter+=1
root= xr
fx=func(xr)
return root, fx, ea, iter
#root= 142.73765563964844
# 142.73763311
if __name__ == '__main__':
fm=lambda m: np.sqrt(9.81*m/0.25)*np.tanh(np.sqrt(9.81*0.25/m)*4)-36
root, fx, ea, iter=newtonRaphson(fm, 40, 200)
print('root=', root)
print('f(root)=', fx)
print('ea=', ea)
print('iter=', iter)
'''''''''false position
root= 142.73783844758216
f(root)= 4.20034974979e-06
ea= 7.781013789779357e-05
iter= 29
'''''''''
''''''''' bisection
root= 142.73765563964844
f(root)= 4.60891321552e-07
ea= 5.3450468252827136e-05
iter= 21
'''''''''