-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpetrarch_n_laura.py
201 lines (166 loc) · 5.96 KB
/
petrarch_n_laura.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
from scipy.integrate import odeint
def f(t, x):
L = x[0]
P = x[1]
Z = x[2]
rhs = np.zeros(len(x))
rhs[0] = -3.6*L + 1.2*(P*(1-P**2) - 1)
rhs[1] = -1.2*P + 6*(L + (2/(1 + Z)))
rhs[2] = -0.12*Z + 12*P
return rhs
def rk4(f, x0, t):
N = len(x0)
M = len(t)
x = np.zeros((M, N))
x[0] = x0
h = (t[-1]-t[0])/M
for n in range(M-1):
K1 = f(t[n],x[n] )
K2 = f(t[n] + .5*h, x[n] + np.multiply(K1, h*.5))
K3 = f(t[n] + .5*h, x[n] + np.multiply(K2, h*.5))
K4 = f(t[n] + h, x[n] + np.multiply(K3, h))
phi = np.multiply(K1 + np.multiply(K2, 2) + np.multiply(K3, 2) + K4, 1/6)
x[n+1] = x[n] + np.multiply(phi,h)
return x
def rk4_once (tn, yn, h):
K1 = f(tn, yn)
K2 = f(tn + .5*h, yn + np.multiply(K1, .5*h))
K3 = f(tn + .5*h, yn + np.multiply(K2,.5*h))
K4 = f(tn + h, yn + np.multiply(K3, h))
ynew = yn + np.multiply((K1 + np.multiply(K2,2) + np.multiply(K3,2) + K4), h/6)
return ynew
def abm4(f, x0, t):
N = len(x0)
M = len(t)
x = np.zeros((M, N))
x[0] = x0
h = (t[-1]-t[0])/M
for n in range(3):
x[n+1] = rk4_once(t[n], x[n], h)
for n in range(3,M-1):
F1 = f(t[n],x[n])
F2 = f(t[n-1],x[n-1])
F3 = f(t[n-2],x[n-2])
F4 = f(t[n-3],x[n-3])
x[n+1] = x[n] + np.multiply(np.multiply(F1, 55) - np.multiply(F2, 59) \
+ np.multiply(F3, 37) - np.multiply(F4, 9), h/24)
G1 = f(t[n+1], x[n+1])
G2 = F1
G3 = F2
G4 = F3
x[n+1] = x[n] + np.multiply(np.multiply(G1, 9) \
+ np.multiply(G2, 19) - np.multiply(G3, 5) + G4, h/24)
return x
if __name__ == '__main__':
x0 = [0, 0, 0]
T = 21 # years
N = 15001
t, h = np.linspace(0, T, N, retstep = True)
x_rk4 = rk4(f, x0, t)
x_abm4 = abm4(f, x0, t)
# RK4 Method
L = x_rk4[:, 0]
P = x_rk4[:, 1]
Z = x_rk4[:, 2]
fig = plt.figure()
plt.plot(L, P, '-')
plt.title("Petrarch's Love Relative to Laura's \n Fourth-order Runge-Kutta")
plt.xlabel("Laura's Love")
plt.ylabel("Petrarch's Love")
plt.grid()
plt.show()
fig.savefig("L_P.png")
fig = plt.figure()
plt.plot(P, Z, '-')
plt.title("Petrarch's inspiration realitive to his love \n Fourth-order Runge-Kutta")
plt.xlabel("Petrarch's love")
plt.ylabel("Petrarch's inspo")
plt.grid()
plt.show()
fig.savefig("P_Z.png")
fig = plt.figure()
plt.plot(t, L, '-.', t, P, '-.', t, Z, '-.')
plt.title("Laura and Petarch's Cylical Dynamic of Love \n Fourth-order Runge-Kutta")
plt.xlabel("time (years)")
plt.ylabel("love & inspiration")
plt.legend(["Laura's love", "Petrarch's love", "Petrarch's inspo"], loc = 'best')
plt.grid()
plt.show()
fig.savefig("LP_love_rk4.png")
# ABM4 Method
L = x_abm4[:, 0]
P = x_abm4[:, 1]
Z = x_abm4[:, 2]
plt.plot(L, P, '-')
plt.title("Petrarch's Love Relative to Laura's \n Fourth-order Adam Bashforth-Moulton")
plt.xlabel("Laura's Love")
plt.ylabel("Petrarch's Love")
plt.grid()
plt.show()
plt.plot(P, Z, '-')
plt.title("Petrarch's inspiration realitive to his love \n Fourth-order Adam Bashforth-Moulton")
plt.xlabel("Petrarch's love")
plt.ylabel("Petrarch's inspo")
plt.grid()
plt.show()
fig = plt.figure()
plt.plot(t, L, '-.', t, P, '-.', t, Z, '-.')
plt.title("Laura and Petarch's Cylical Dynamic of Love \n Fourth-order Adam Bashforth-Moulton")
plt.xlabel("time (years)")
plt.ylabel("love & inspiration")
plt.legend(["Laura's love", "Petrarch's love", "Petrarch's inspo"], loc = 'best')
plt.grid()
plt.show()
fig.savefig("LP_love_abm4.png")
# Python's in-built ode solvers
tspan=[0,21]
x = solve_ivp(f, tspan, x0, method='RK45',t_eval = t) #LSODA, BDF
fig = plt.figure()
plt.plot(x.t, x.y[0], '-', x.t, x.y[1], '-', x.t, x.y[2], '-')
plt.title("Laura and Petarch's Cylical Dynamic of Love \n solve_ivp")
plt.xlabel("time (years)")
plt.ylabel("love & inspiration")
plt.legend(["Laura's love", "Petrarch's love", "Petrarch's inspo"], loc = 'best')
plt.grid()
plt.show()
fig.savefig("LP_love_solve_ivp.png")
fig = plt.figure()
plt.plot(x.t, x.y[1], '-', x.t, x.y[2], '-')
plt.title("Petrarch's Love and Inspiration Over the Years")
plt.xlabel("time (years)")
plt.ylabel("love & inspiration")
plt.legend(["Petrarch's love", "Petrarch's inspo"], loc = 'best')
plt.grid()
plt.show()
fig.savefig("loveninspo.png")
fig = plt.figure()
plt.plot(x.t, x.y[1], '-')
plt.title("Petrarch's Love Over the Years")
plt.xlabel("time (years)")
plt.ylabel("love")
plt.grid()
plt.show()
fig.savefig("pets_love.png")
fig = plt.figure()
x1 = odeint(f, x0,t,tfirst =True)
plt.plot(x.t, x1[:,0], '-', x.t, x1[:,1], '-', x.t, x1[:,2], '-')
plt.title("Laura and Petarch's Cylical Dynamic of Love \n odeint")
plt.xlabel("time (years)")
plt.ylabel("love & inspiration")
plt.legend(["Laura's love", "Petrarch's love", "Petrarch's inspo"], loc = 'best')
plt.grid()
plt.show()
fig.savefig("LP_love_odeint.png")
fig = plt.figure()
x1 = odeint(f, x0,t,tfirst =True)
plt.plot(x.t, x1[:,0], '-', x.t, x1[:,1], '-')
plt.title("Laura and Petarch's Love Over the Years")
plt.xlabel("time (years)")
plt.ylabel("love & inspiration")
plt.legend(["Laura's love", "Petrarch's love"], loc = 'best')
plt.grid()
plt.show()
fig.savefig("all_love.png")