-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrunge_kutta.py
More file actions
66 lines (47 loc) · 1.73 KB
/
runge_kutta.py
File metadata and controls
66 lines (47 loc) · 1.73 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
51
52
53
54
55
56
57
58
import matplotlib.pyplot as plt
import numpy as np
class RungeKutta:
@staticmethod
def _get_epsilon(solution_range, solution, exact_function):
epsilon = -1
for n, t in enumerate(solution_range):
epsilon = max(epsilon, abs(solution[n] - exact_function(t)))
return epsilon
@staticmethod
def runge_kutta_custom(t0, x0, tn, n, f):
delta = (tn - t0) / (n - 1)
t = [t0]
x_solution = [x0]
for i in range(n - 1):
u = t0 + delta/2
k1 = delta * f(t0, x0)
k2 = delta * f(u, x0 + k1 / 2)
k3 = delta * f(u, x0 + k2 / 2)
k4 = delta * f(t0 + delta, x0 + k3)
next_x = x0 + (k1 + 2 * k2 + 2 * k3 + k4) / 6
x_solution.append(next_x)
t0 += delta
t.append(t0)
x0 = next_x
return {'t': t, 'x(t)': x_solution}
@staticmethod
def runge_kutta(t0, x0, tn, n, f, solution):
delta = (tn - t0) / (n - 1)
t = [t0]
x_solution = [x0]
start_t0 = t0
for i in range(n - 1):
u = t0 + delta/2
k1 = delta * f(t0, x0)
k2 = delta * f(u, x0 + k1 / 2)
k3 = delta * f(u, x0 + k2 / 2)
k4 = delta * f(t0 + delta, x0 + k3)
next_x = x0 + (k1 + 2 * k2 + 2 * k3 + k4) / 6
x_solution.append(next_x)
t0 += delta
t.append(t0)
x0 = next_x
exact_range = np.arange(start_t0, t[-1] + 0.001, 0.001)
exact_solution = [solution(i) for i in exact_range]
return {'t': t,'x(t)':x_solution}, {'exact_t': exact_range, 'exact_x': exact_solution},\
RungeKutta._get_epsilon(t, x_solution, solution)