-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathODE_single_pendulum.py
359 lines (234 loc) · 13.5 KB
/
ODE_single_pendulum.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
"""
Module to solve ODE (i.e. equations of motion of a single pendulum) using various numerical methods
Author: Son Gyo Jung
Email: sgj30@cam.ac.uk
"""
from matplotlib import pyplot as plt
import numpy as np
class single_pendulum():
"""
Class to solve equations of motion of a single pendulum using various numerical methods
Note: Since the equations for the single pendulum motion are re-scaled into natural units, all the variables are in natural units.
args:
(1) h (type:float) - step size
(2) D (type:float) - damping coefficient
(3) t - time steps defined using h
"""
def __init__(self, h = 0.01, D = 0.):
self.h = h
self.D = D
self.t = np.arange(0, 100, self.h)
def EulerForward(self, save_figure = False):
"""
Solve the ODE using Explicit Euler method
args:
(1) figure (type:bool) - whether to save the figure
(2) w - angular velocity (starting from rest)
(3) theta = angular displacement
return:
(1) Figure of angular displacement against time
"""
# Angular velocity (starting from rest)
self.EulerForward_w = np.zeros(self.t.size) #starting from rest
# Angular displacement
self.EulerForward_theta = np.ones(self.t.size) * 10. * np.pi / 180. #* 0.75 * np.pi #for ExactEuler
# Energy
self.EulerForward_energy = np.ones(self.t.size)
for i in range(1, self.t.size):
# Solve ODE using Euler Forward
self.EulerForward_w[i] = self.EulerForward_w[i - 1] + (-self.D * self.EulerForward_w[i - 1] - self.EulerForward_theta[i - 1]) * self.h
self.EulerForward_theta[i] = self.EulerForward_theta[i - 1] + self.EulerForward_w[i - 1] * self.h
self.EulerForward_energy[i] = (0.5 * self.EulerForward_w[i]**(2)) + (0.5 * self.EulerForward_theta[i]**(2))
plt.figure(figsize=(6, 4), dpi = 300)
plt.plot(self.t, self.EulerForward_theta, '.-', label = 'Explicit Euler with h=' + str(self.h) + ' , $\hat{D}$=' + str(self.D))
plt.xlabel('t('+r'$\sqrt{\frac{l}{g}}$'+')', fontsize=14)
plt.ylabel('$\Theta$ (rad)', fontsize=14)
plt.legend(loc=1, prop={'size':14})
plt.grid(True)
plt.show()
if save_figure == True:
plt.savefig('Angular_displacement_EulerForward.png', dpi = 300, bbox_inches = "tight")
def Leapfrog(self, save_figure = False):
"""
Solve the ODE using Leapfrog or Leapfrog with Euler Forward
"""
EulerForwardPrediction = str.lower(input('Predict using Euler Forward for Leapfrog? "yes" or "no": '))
self.Leapfrog_w = np.zeros(self.t.size) #starting from rest
self.Leapfrog_theta = np.ones(self.t.size) * 10. * np.pi / 180.
if EulerForwardPrediction == 'no':
for i in range(1, self.t.size):
self.Leapfrog_w[i] = self.Leapfrog_w[i - 2] + 2 * (-self.D * self.Leapfrog_w[i - 1] - self.Leapfrog_theta[i - 1]) * self.h
self.Leapfrog_theta[i] = self.Leapfrog_theta[i - 2] + 2 * self.Leapfrog_w[i - 1] * self.h
plt.figure(figsize=(6, 4), dpi = 300)
plt.plot(self.t, self.Leapfrog_theta, '.-', label = 'Leapfrog with h=' + str(self.h) + ' , $\hat{D}$=' + str(self.D))
plt.xlabel('t('+r'$\sqrt{\frac{l}{g}}$'+')')
plt.ylabel('$\Theta$ (rad)')
plt.legend(loc=1, prop={'size':14})
plt.title('Angular displacement vs time', fontsize=14)
plt.grid(True)
plt.show()
if save_figure == True:
plt.savefig('Angular_displacement_Leapfrog.png', dpi = 300, bbox_inches = "tight")
elif EulerForwardPrediction == 'yes':
for i in range(2, self.t.size): #range 2 if predicting using EulerForward
self.Leapfrog_w[1] = self.Leapfrog_w[0] + (-self.D * self.Leapfrog_w[0] - self.Leapfrog_theta[0]) * self.h #Predict only the next value required for leapfrog method
self.Leapfrog_theta[1] = self.Leapfrog_theta[0] + self.Leapfrog_w[0] * self.h
self.Leapfrog_w[i] = self.Leapfrog_w[i - 2] + 2 * (-self.D * self.Leapfrog_w[i - 1] - self.Leapfrog_theta[i - 1]) * self.h
self.Leapfrog_theta[i] = self.Leapfrog_theta[i - 2] + 2 * self.Leapfrog_w[i - 1] * self.h
plt.figure(figsize=(6, 4), dpi = 300)
plt.plot(self.t, self.Leapfrog_theta, '.-', label = 'Leapfrog with h=' + str(self.h) + ' , $\hat{D}$=' + str(self.D))
plt.xlabel('t('+r'$\sqrt{\frac{l}{g}}$'+')')
plt.ylabel('$\Theta$ (rad)')
plt.legend(loc=1, prop={'size':14})
plt.title('Angular displacement vs time', fontsize=14)
plt.grid(True)
plt.show()
if save_figure == True:
plt.savefig('Angular_displacement_Leapfrog_EulerForward.png', dpi = 300, bbox_inches = "tight")
else:
return('Invalid input')
def EulerBackward(self, save_figure = False):
"""
Solve the ODE using Implicit Euler
"""
self.EulerBackward_w = np.zeros(self.t.size)
self.EulerBackward_theta = np.ones(self.t.size) * 10. * np.pi / 180.
for i in range(1, self.t.size):
self.EulerBackward_w[i] = (-1 * self.h * self.EulerBackward_theta[i - 1] + self.EulerBackward_w[i - 1]) / (1 + self.h * self.D + self.h**(2))
self.EulerBackward_theta[i] = ((1 + self.h * self.D) * self.EulerBackward_theta[i - 1] + self.EulerBackward_w[i] * self.h) / (1 + self.h * self.D + self.h**(2))
plt.figure(figsize=(6, 4), dpi = 300)
plt.plot(self.t, self.EulerBackward_theta, 'k.-', label = 'Implicit Euler with h=' + str(self.h) + ' , $\hat{D}$=' + str(self.D))
plt.xlabel('t('+r'$\sqrt{\frac{l}{g}}$'+')')
plt.ylabel('$\Theta$ (rad)')
plt.legend(loc=1, prop={'size':14})
plt.title('Angular displacement vs time', fontsize=22)
plt.grid(True)
plt.show()
if save_figure == True:
plt.savefig('Angular_displacement_EulerBackward.png', dpi = 300, bbox_inches = "tight")
def RK4(self, save_figure = False):
"""
Solve the ODE using RK4
"""
self.RK4_w = np.zeros(self.t.size)
self.RK4_theta = np.ones(self.t.size) * 10. * np.pi / 180.
for i in range(1, self.t.size):
f_w1 = -self.D * self.RK4_w[i - 1] - self.RK4_theta[i - 1]
f_theta1 = self.RK4_w[i - 1]
w1 = self.RK4_w[i - 1] + f_w1 * self.h/2.
theta1 = self.RK4_theta[i - 1] + f_theta1 * self.h / 2.
f_w2 = -self.D * w1 - theta1
f_theta2 = w1
w2 = self.RK4_w[i - 1] + f_w2 * self.h / 2.
theta2 = self.RK4_theta[i - 1] + f_theta2 * self.h / 2.
f_w3 = -self.D * w2 - theta2
f_theta3 = w2
w3 = self.RK4_w[i-1] + f_w3 * self.h
theta3 = self.RK4_theta[i-1] + f_theta3 * self.h
f_w4 = -self.D * w3 - theta3
f_theta4 = w3
self.RK4_w[i] = self.RK4_w[i - 1] + (f_w1 + 2 * f_w2 + 2 * f_w3 + f_w4)/6. * self.h
self.RK4_theta[i] = self.RK4_theta[i - 1] + (f_theta1 + 2 * f_theta2 + 2 * f_theta3 + f_theta4) / 6 * self.h
plt.figure(figsize=(6, 4), dpi = 300)
plt.plot(self.t, self.RK4_theta, 'r--', linewidth = 2, label = 'RK4 with h=' + str(self.h) + ' , $\hat{D}$=' + str(self.D))
plt.xlabel('t('+r'$\sqrt{\frac{l}{g}}$'+')')
plt.ylabel('$\Theta$ (rad)')
plt.legend(loc=1, prop={'size':14})
plt.title('Angular displacement vs time', fontsize=14)
plt.grid(True)
plt.show()
if save_figure == True:
plt.savefig('Angular_displacement_RK4.png', dpi = 300, bbox_inches = "tight")
def Energy(self, save_figure = False, run_all = False):
"""
Function to plot the total energy against time
"""
if run_all == True:
self.EulerForward()
self.Leapfrog()
self.EulerBackward()
self.RK4()
Energy1, Energy2, Energy3, Energy4 = [], [], [], []
for i in range(self.t.size):
Energy1.append(0.5 * self.EulerForward_w[i]**(2) + 0.5 * self.EulerForward_theta[i]**(2))
Energy2.append(0.5 * self.Leapfrog_w[i]**(2) + 0.5 * self.Leapfrog_theta[i]**(2))
Energy3.append(0.5 * self.EulerBackward_w[i]**(2) + 0.5 * self.EulerBackward_theta[i]**(2))
Energy4.append(0.5 * self.RK4_w[i]**(2) + 0.5 * self.RK4_theta[i]**(2))
plt.figure(figsize=(6, 4), dpi = 300)
plt.plot(self.t, Energy1, '-', linewidth = 2, label='Explicit Euler with h=' +str(self.h) + ' , $\hat{D}$=' + str(self.D))
plt.plot(self.t, Energy2, '-', linewidth = 2, label='Leapfrog with h=' +str(self.h) + ' , $\hat{D}$=' + str(self.D))
plt.plot(self.t, Energy3, 'k',linewidth = 2, label='Implicit Euler with h=' +str(self.h) + ' , $\hat{D}$=' + str(self.D))
plt.plot(self.t, Energy4, 'r--',linewidth = 2, label='RK4 with h=' +str(self.h) + ' , $\hat{D}$=' + str(self.D))
plt.legend(loc=1,prop={'size':14})
plt.xlabel('t('+r'$\sqrt{\frac{l}{g}}$'+')', fontsize=18)
plt.ylabel('E (mgl)', fontsize=18)
plt.title('Total energy vs time', fontsize=18)
plt.grid(True)
plt.show()
if save_figure == True:
plt.savefig('Energy.png', dpi = 300, bbox_inches = "tight")
def ExactEuler(self, save_figure = False, run_EulerForward = True, h = 0.3, D = 0.2):
"""
Function to plot the dynamics predicted with and without the small angle approximation
Note: Don't forget to change the angle in EulerForward function as well as adjusting h and D
"""
self.D = D
self.h = h
self.t = np.arange(0, 100, self.h)
w = np.zeros(self.t.size) #initial speed = 0
theta = np.ones(self.t.size) * 0.75 * np.pi
energy = np.zeros(self.t.size)
if run_EulerForward == True:
self.EulerForward()
for i in range(1, self.t.size):
w[i] = w[i - 1] + (-self.D * w[i-1] - np.sin(theta[i-1])) * self.h
theta[i] = theta[i - 1] + w[i - 1] * self.h
energy[i] = 0.5*(w[i]**2) + 1 - np.cos(theta[i])
fig = plt.figure(figsize = (15, 10))
ax1 = fig.add_subplot(211)
ax1.plot(self.t, theta, label = 'Without small angle approx: h=' + str(self.h) + ' , $\hat{D}$=' + str(self.D))
ax1.plot(self.t, self.EulerForward_theta, label = 'With small angle approx: h=' + str(self.h) + ' , $\hat{D}$=' + str(self.D))
plt.legend()
plt.grid(True)
plt.legend(loc=2,prop={'size':14})
plt.xlabel('t('+r'$\sqrt{\frac{l}{g}}$'+')', fontsize=14)
plt.ylabel('$\Theta$ (rad)', fontsize=14)
ax1.set_title('Angular displacement vs time using explicit Euler for large initial angle', fontsize=14)
plt.tight_layout()
ax2 = fig.add_subplot(212)
ax2.plot(self.t, energy, label = 'Without small angle approx: h=' + str(self.h) + ' , $\hat{D}$=' + str(self.D))
ax2.plot(self.t, self.EulerForward_energy, label = 'With small angle approx: with h=' + str(self.h) + ' , $\hat{D}$=' + str(self.D))
plt.legend()
plt.grid(True)
plt.legend(loc=2,prop={'size':14})
plt.xlabel('t('+r'$\sqrt{\frac{l}{g}}$'+')', fontsize=14)
plt.ylabel('E (mgl)', fontsize=14)
ax2.set_title('Total energy vs time using explicit Euler for large initial angle', fontsize=14)
plt.tight_layout()
plt.show()
if save_figure == True:
plt.savefig('compare.png', dpi = 300, bbox_inches = "tight")
def period(self, method = 'EulerForward'):
"""
Calculating the period of the oscillations
"""
if method == 'EulerForward':
method_theta = self.EulerForward_theta
elif method == 'EulerBackward':
method_theta = self.EulerBackward_theta
elif method == 'Leapfrog':
method_theta = self.Leapfrog_theta
elif method == 'RK4':
method_theta = self.RK4_theta
period_xintercept = []
period_time = []
for i in range(1, self.t.size):
if abs(method_theta[i]) <= 0.01:
if method_theta[i] < 0 and method_theta[i + 1] > 0 or method_theta[i] > 0 and method_theta[i + 1] < 0:
period_xintercept.append(round(self.t[i], 3))
period_time.append(i)
print('x-intercept at: \n', period_xintercept, '\n')
period_difference = []
for i in range(len(period_xintercept) - 1):
period_difference.append(period_xintercept[i + 1] - period_xintercept[i])
print('period difference: \n', period_difference)