-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnewton1.py
More file actions
39 lines (28 loc) · 790 Bytes
/
newton1.py
File metadata and controls
39 lines (28 loc) · 790 Bytes
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
# newton1.py: ニュートン法
import numpy as np
# f(x) = 0
def func(x):
return x - np.cos(x)
# f'(x) = 0
def dfunc(x):
return 1 + np.sin(x)
# 初期値
x0 = 1.0
# 停止条件
atol = 1.0e-50
rtol = 1.0e-10
# ニュートン反復
x_old = x0
for times in range(100):
x_new = x_old - func(x_old) / dfunc(x_old)
print(f'x_{times:d} = {x_new:25.17e}, {func(x_new):10.3e}')
if np.abs(x_new - x_old) <= rtol * np.abs(x_old) + atol:
break
x_old = x_new
# 表示
print(f'最終 -> x_{times:d} = {x_new:25.17e}')
print(f'検算 -> func(x_{times:d}) = {func(x_new):25.17e}')
# -------------------------------------
# Copyright (c) 2021 Tomonori Kouya
# All rights reserved.
# -------------------------------------