-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathnewton.py
More file actions
50 lines (39 loc) · 1.25 KB
/
newton.py
File metadata and controls
50 lines (39 loc) · 1.25 KB
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
import numpy as np
from scipy.optimize import fsolve
def sech(x):
return 1 / np.cosh(x)
def newton_raphson(func, dfunc, xr, xt):
maxit = 50
es = 1.0e-5
iter = 0
while True:
xrold = xr
xr = float(xr - func(xr) / dfunc(xr))
iter += 1
if xr != 0:
ea = abs((xr - xrold) / xr) * 100
et = abs((xt - xr) / xt) * 100
if ea <= es or iter >= maxit:
break
root = xr
fx = func(xr)
return root, fx, ea, iter
if __name__ == '__main__':
g = 9.81
cd = 0.25
v = 36
t = 4
# 방정식 정의
fm = lambda m: np.sqrt(g * m / cd) * np.tanh(np.sqrt(g * cd / m) * t) - v
# fsolve로 실제 해 추정
xt = fsolve(fm, 1)[0]
print("Real Root =", xt)
# 뉴턴-랩슨용 함수 및 도함수
fp = lambda m: np.sqrt(g * m / cd) * np.tanh(np.sqrt(g * cd / m) * t) - v
dfp = lambda m: (0.5) * np.sqrt(g / (m * cd)) * np.tanh(np.sqrt(g * cd / m) * t) \
- (g * t / (2 * m)) * (sech(np.sqrt(g * cd / m) * t)) ** 2
root, fx, ea, iter = newton_raphson(fp, dfp, 140, xt)
print('Root weight =', root)
print('f(root weight, should be zero) =', fx)
print('ea (should be < 1.0e-4) =', ea)
print('iterations =', iter)