-
Notifications
You must be signed in to change notification settings - Fork 2
/
RungeKutta4.py
53 lines (41 loc) · 1.76 KB
/
RungeKutta4.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
"""
Academic work of Numerical Differential Equations
Federal University of Minas Gerais
"""
import math, sys
import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import interact, interactive
from scipy import integrate
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import cnames
from matplotlib import animation
def RungeKutta4(xt,yt,zt,n = 3500, T = 35):
x = np.zeros(n+1)
y = np.zeros(n+1)
z = np.zeros(n+1)
t = np.zeros(n+1)
x[0] = 8.0
y[0] = 1.0
z[0] = 1.0
t[0] = 0
dt = T/float(n) #0.01
#Compute the approximate solution at equally spaced times.
for k in range (n):
t[k+1] = t[k] + dt
k1 = xt(x[k], y[k], z[k], t[k])
l1 = yt(x[k], y[k], z[k], t[k])
m1 = zt(x[k], y[k], z[k], t[k])
k2 = xt((x[k] + 0.5*k1*dt), (y[k] + 0.5*l1*dt), (z[k] + 0.5*m1*dt), (t[k] + dt/2))
l2 = yt((x[k] + 0.5*k1*dt), (y[k] + 0.5*l1*dt), (z[k] + 0.5*m1*dt), (t[k] + dt/2))
m2 = zt((x[k] + 0.5*k1*dt), (y[k] + 0.5*l1*dt), (z[k] + 0.5*m1*dt), (t[k] + dt/2))
k3 = xt((x[k] + 0.5*k2*dt), (y[k] + 0.5*l2*dt), (z[k] + 0.5*m2*dt), (t[k] + dt/2))
l3 = yt((x[k] + 0.5*k2*dt), (y[k] + 0.5*l2*dt), (z[k] + 0.5*m2*dt), (t[k] + dt/2))
m3 = zt((x[k] + 0.5*k2*dt), (y[k] + 0.5*l2*dt), (z[k] + 0.5*m2*dt), (t[k] + dt/2))
k4 = xt((x[k] + k3*dt), (y[k] + l3*dt), (z[k] + m3*dt), (t[k] + dt))
l4 = yt((x[k] + k3*dt), (y[k] + l3*dt), (z[k] + m3*dt), (t[k] + dt))
m4 = zt((x[k] + k3*dt), (y[k] + l3*dt), (z[k] + m3*dt), (t[k] + dt))
x[k+1] = x[k] + (dt*(k1 + 2*k2 + 2*k3 + k4) / 6)
y[k+1] = y[k] + (dt*(l1 + 2*l2 + 2*l3 + l4) / 6)
z[k+1] = z[k] + (dt*(m1 + 2*m2 + 2*m3 + m4) / 6)
return x, y, z, t