I don’t know that how make the code the three graph in damping harmonic oscillation model,
[X - t(time)], [V(velocity) - t(time)], [a(acceleration) - t(time)]
graph
i can make the [X - t(time)]
graph
but i don`t know how to make another graphs..
JavaScript
x
71
71
1
import numpy as np
2
3
from matplotlib import pyplot as plt
4
5
# mx'' = - bx' - kx
6
7
x_0 = 3
8
9
v_0 = 0
10
11
y_0 = np.array([x_0,v_0]) # first array
12
13
14
def Euler_Method(f,a,b,y0,step):
15
16
t = np.linspace(a,b,step)
17
18
h = t[1] - t[0]
19
20
Y = [y0]
21
22
N = len(t)
23
24
n = 0
25
26
y = y0
27
28
for n in range(0,N-1) :
29
30
y = y + h*f(y,t[n])
31
32
Y.append(y)
33
34
n = n+1
35
36
Y = np.array(Y)
37
38
return Y, t
39
40
41
42
43
def harmonic(y,t) :
44
45
k = 50
46
47
m = 200
48
49
b = 20 # drag coefficient
50
51
a = (-1*k/m)*y[0] - (b/m)*y[1] # x'' = a, y[0] : first position
52
53
v = y[1] # v = first velocity : y[1]
54
55
f = np.array([v,a])
56
57
return f
58
59
60
61
62
a = Euler_Method(harmonic, 0, 100, y_0, 100000)
63
64
X = a[0][:,0]
65
66
t = a[1]
67
68
plt.plot(t,X)
69
70
plt.show()
71
Advertisement
Answer
Why can’t you just take the derivative of X to get V and A?
JavaScript
1
10
10
1
V = np.diff(X)
2
A = np.diff(V)
3
4
fig, (ax1, ax2, ax3) = plt.subplots(3)
5
fig.suptitle('Vertically stacked subplots')
6
ax1.plot(t, X)
7
ax2.plot(t[1:], V)
8
ax3.plot(t[2:], A)
9
plt.show()
10
Gives,