1.6.12.6. Integrate the Damped spring-mass oscillatorΒΆ

../../../_images/sphx_glr_plot_odeint_damped_spring_mass_001.png
import numpy as np
from scipy.integrate import odeint
from matplotlib import pyplot as plt
mass = 0.5 # kg
kspring = 4 # N/m
cviscous = 0.4 # N s/m
eps = cviscous / (2 * mass * np.sqrt(kspring/mass))
omega = np.sqrt(kspring / mass)
def calc_deri(yvec, time, eps, omega):
return (yvec[1], -eps * omega * yvec[1] - omega **2 * yvec[0])
time_vec = np.linspace(0, 10, 100)
yinit = (1, 0)
yarr = odeint(calc_deri, yinit, time_vec, args=(eps, omega))
plt.figure(figsize=(4, 3))
plt.plot(time_vec, yarr[:, 0], label='y')
plt.plot(time_vec, yarr[:, 1], label="y'")
plt.legend(loc='best')
plt.show()

Total running time of the script: ( 0 minutes 0.015 seconds)

Gallery generated by Sphinx-Gallery