-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrogatz3.4.2.py
42 lines (35 loc) · 1.09 KB
/
Strogatz3.4.2.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
## Strogatz example :3.4.2 solved by Runge kutta algorithm
import pylab
import numpy as np
x_initial=0.1
t_initial=-5
t_final=5
h=0.001
def Runge(r, x_initial, t_initial, h, t_final):
def f(t, x): return -(r*t-t**3)
time=list(np.arange(t_initial, t_final+h, h))
X=[x_initial]
for i in range(1, len(time)):
p=time[i-1]
q=X[i-1]
k1=h*f(p, q)
k2=h*f(p+h/2., q+k1/2.)
k3=h*f(p+h/2., q+k2/2.)
k4=h*f(p+h, q+k3)
X+=[q+(k1+2*k2+2*k3+k4)/6., ]
return [time, X]
pylab.show()
A=Runge(-3, x_initial, t_initial, h, t_final)
pylab.plot(A[0], A[1], 'k-')
pylab.xlabel("x -->")
pylab.ylabel("V -->")
B=Runge(0, x_initial, t_initial, h, t_final)
pylab.plot(B[0], B[1], '-')
pylab.xlabel("x -->")
pylab.ylabel("V -->")
C=Runge(5, x_initial, t_initial, h, t_final)
pylab.plot(C[0], C[1], 'r-')
pylab.xlabel("x -->")
pylab.ylabel("V -->")
pylab.title("Strogatz: Non linear dynamics, example: 3.4.2")
pylab.show()