-
Notifications
You must be signed in to change notification settings - Fork 0
/
runge_kutta_4_system.py
70 lines (49 loc) · 1.94 KB
/
runge_kutta_4_system.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
from math import *
def rk4_system(f, f_actual, t, y, p, h, N):
n = int((p-t)/h)
zeroes = [0 for i in range(N)]
k = [zeroes.copy() for i in range(4)]
y_temp = zeroes.copy()
results = [zeroes.copy() for i in range(n)]
for i in range(n):
for j in range(N): # Filling in the k1_x for all given functions
k[0][j] = h*f[j](t, *y)
for j in range(N): # Updating y to pass into the next k2_x calculations
y_temp[j] = y[j] + k[0][j]/2
for j in range(N): # Filling in the k2_x for all given functions
k[1][j] = h*f[j](t+h/2, *y_temp)
for j in range(N): # Again need to update the y's for k3_x calculations
y_temp[j] = y[j] + k[1][j]/2
for j in range(N): # Filling in the k3_x for all given functions
k[2][j] = h*f[j](t+h/2, *y_temp)
for j in range(N): # Again need to update the y's for k3_x calculations
y_temp[j] = y[j] + k[2][j]
for j in range(N): # Filling in the k4_x for all given functions
k[3][j] = h*f[j](t+h, *y_temp)
for j in range(N): # Finally, we calculate the y and prepare for the next iteration
y[j] += (k[0][j] + 2*k[1][j] + 2*k[2][j] + k[3][j])/6
results[i][j] = y[j] # I add the final intermediate y to results in order to store them.
t += h
return results
def main():
# Define these functions depending on the problem.
f = [
lambda t, u1, u2, u3, u4: u3,
lambda t, u1, u2, u3, u4: u4,
lambda t, u1, u2, u3, u4: -u1*(u1*u1 + u2*u2)**(-1.5),
lambda t, u1, u2, u3, u4: -u2*(u1*u1 + u2*u2)**(-1.5)
]
f_analytical = [
lambda t: (exp(5*t) - exp(-t))/3 + exp(2*t),
lambda t: (exp(5*t) - 2*exp(-t))/3 + t*t*exp(t)
]
N = len(f)
y = [0]*N
t0 = float(input("t0 = "))
for i in range(N):
y[i] = float(input(f"y0_{i+1} = "))
p = float(input("Evaluation point = "))
h = float(input("Enter Step-Size: "))
print(rk4_system(f, f_analytical, t0, y, p, h, N))
if __name__ == '__main__':
main()