Skip to content

Instantly share code, notes, and snippets.

@thomasjpfan
Created May 2, 2014 21:05
Show Gist options
  • Save thomasjpfan/11486198 to your computer and use it in GitHub Desktop.
Save thomasjpfan/11486198 to your computer and use it in GitHub Desktop.
Generate plots for free damped oscillators
import matplotlib.pyplot as plt
from numpy import exp, cos, pi, linspace
from scipy.optimize import minimize
eta = 0.5
omega = 2 * pi
def damped_os(t):
return exp(-eta * t) * cos(omega * t)
t = linspace(0, 4, num=2000)
x = damped_os(t)
x_u = exp(-eta * t)
x_d = -exp(-eta * t)
fig, axes = plt.subplots()
axes.plot(t, x, 'black')
axes.plot(t, x_u, 'red')
axes.plot(t, x_d, 'red')
axes.set_xlabel('t')
axes.set_ylabel('x/A')
# plt.savefig('freedampedfull.png')
# Zooming
axes.set_xlim(0.9, 1.1)
axes.set_ylim(0.55, 0.65)
def neg_damped_os(t):
return -damped_os(t)
result = minimize(neg_damped_os, 1)
# maximum arrow
x_max, y_max = result.x, -result.fun
dx, dy = 0.02, 0.03
plt.arrow(x_max - dx, y_max - dy, dx, dy,
width=0.0001, length_includes_head=True)
plt.annotate("max", xy=(x_max - dx - 0.01, y_max - dy - 0.005))
x_tang, y_tang = 1.0, damped_os(1.0)
dx, dy = 0.03, 0.01
plt.arrow(x_tang + dx, y_tang + dy, -dx, -dy,
width=0.0001, length_includes_head=True)
plt.annotate("tangent", xy=(x_tang + dx + 0.005, y_tang + dy))
plt.savefig('freedampedzoom.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment