# Q 2A
#d2x/dt2 + gamma*dx/dt + k*x = 0
#take gamma = 0.4, k = 1 and solve using suitable scipy function
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
gam = 0.4
k = 1.
def damp(x, t):
return [x[1], -k*x[0]-gam*x[1]]
x0 = [1.0, 0.0]
t = np.linspace(0,20,1001)
sol = odeint(damp, x0, t)
plt.plot(t, sol[:,0], 'r', label="x")
plt.xlabel("time(t)")
plt.ylabel("x")
plt.axhline()
plt.title("Damped Harmonic Oscillator")
plt.legend()
plt.show()
Comments
Post a Comment