-
Notifications
You must be signed in to change notification settings - Fork 0
/
ode.py
168 lines (125 loc) · 4.28 KB
/
ode.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#-*- encoding: utf-8 -*-
"""
solve ordinary differential equations
Copyright (c) 2016 @myuuuuun
Released under the MIT license.
"""
import math
import numpy as np
import pandas as pd
import functools
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.cm as cm
EPSIRON = 1.0e-8
np.set_printoptions(precision=3)
np.set_printoptions(linewidth=400)
np.set_printoptions(threshold=np.nan)
pd.set_option('display.max_columns', 130)
pd.set_option('display.width', 1400)
plt.rcParams['font.size'] = 14
# 日本語対応
mpl.rcParams['font.family'] = 'Osaka'
# Explicit Euler Method
# 陽的(前進)オイラー法
def euler(func, init, t_start, step, repeat):
if not isinstance(func, list):
func = [func]
if not isinstance(init, list):
init = [init]
if len(init) != len(func):
raise ValueError("微分係数の数と初期値の数が一致しません")
dim = len(func)
path = np.zeros((dim+1, repeat), dtype=float)
path[:, 0] = [t_start] + init
for i in range(1, repeat):
current = path[1:, i-1]
path[0, i] = t_start + i * step
for s in range(dim):
path[s+1, i] = current[s] + func[s](current) * step
return path
# Modified Euler Method
# 修正オイラー法
def modified_euler(func, init, t_start, step, repeat):
if not isinstance(func, list):
func = [func]
if not isinstance(init, list):
init = [init]
if len(init) != len(func):
raise ValueError("微分係数の数と初期値の数が一致しません")
dim = len(func)
path = np.zeros((dim+1, repeat), dtype=float)
path[:, 0] = [t_start] + init
k1 = np.zeros(dim, dtype=float)
k2 = np.zeros(dim, dtype=float)
for i in range(1, repeat):
current = path[1:, i-1]
path[0, i] = t_start + i * step
# k1
for s in range(dim):
k1[s] = func[s](current)
# k2
for s in range(dim):
k2[s] = func[s](current + step * k1)
path[1:, i] = current + step * (k1 + k2) / 2
return path
# Explicit RK4 Method
# 4段4次ルンゲ・クッタ
def runge_kutta(func, init, t_start, step, repeat):
if not isinstance(func, list):
func = [func]
if not isinstance(init, list):
init = [init]
if len(init) != len(func):
raise ValueError("微分係数の数と初期値の数が一致しません")
dim = len(func)
path = np.zeros((dim+1, repeat), dtype=float)
path[:, 0] = [t_start] + init
k1 = np.zeros(dim, dtype=float)
k2 = np.zeros(dim, dtype=float)
k3 = np.zeros(dim, dtype=float)
k4 = np.zeros(dim, dtype=float)
for i in range(1, repeat):
current = path[1:, i-1]
path[0, i] = t_start + i * step
# k1
for s in range(dim):
k1[s] = func[s](current)
# k2
for s in range(dim):
k2[s] = func[s](current + step * 0.5 * k1)
# k3
for s in range(dim):
k3[s] = func[s](current + step * 0.5 * k2)
# k4
for s in range(dim):
k4[s] = func[s](current + step * k3)
path[1:, i] = current + step * (k1 + 2*k2 + 2*k3 + k4) / 6
return path
if __name__ == '__main__':
"""
Sample: solve x''(t) = -x, x(0) = 1, x'(0) = 0
analytic solution is x(t) = cos(t)
"""
x = lambda array: array[1]
dx = lambda array: -1 * array[0]
func = [x, dx]
init = [1, 0]
t_start = 0
step = 0.01
repeat = 10000
ts = np.arange(t_start, step*repeat, step)
true_path = np.cos(ts)
euler_path = euler(func, init, t_start, step, repeat)
modified_euler_path = modified_euler(func, init, t_start, step, repeat)
rk4_path = runge_kutta(func, init, t_start, step, repeat)
fig, ax = plt.subplots(figsize=(16, 8))
plt.title(r'Initial value problem $\"x = -x(t)$')
plt.xlabel("t")
plt.ylabel("x")
plt.plot(ts, true_path, color='orange', linewidth=3, label="true_path(x=cos(t))")
plt.plot(euler_path[0], euler_path[1], color='blue', linewidth=1, label="Euler approx")
plt.plot(modified_euler_path[0], modified_euler_path[1], color='green', linewidth=2, label="Modified Euler approx")
plt.plot(rk4_path[0], rk4_path[1], color='red', linewidth=1, label="RK4 approx")
plt.legend()
plt.show()