...

/

Solution Review: Harmonographs

Solution Review: Harmonographs

Solution review to the harmonographs exercise.

Solution 1 #

Press + to interact
import numpy as np
import matplotlib.pyplot as plt
# defining function with default paramters value
def pendulum(t, A=1, f=100, p=0, d=0.05):
return (A * np.sin (2 * np.pi * f * t + p) * np.exp(-1 * d * t))
# creating array for time
t = np.linspace(0, 2 * np.pi, 1000)
x_t = pendulum(t, 1, 2, 0, 0.5)
# plotting and setting labels
plt.figure(figsize=(12, 9), dpi=200)
plt.plot(t, x_t)
# setting labels
plt.title('Damped Pendulum')
plt.xlabel('t')
plt.ylabel('x(t)')
# saving figure
plt.savefig('output/harmonograph1.png')

Explanation #

  • In lines 5 - 6, we have defined the function pendulum() and set the default values for A, f, p, and d.

  • In line 9, we have created an array for time t.

  • In line 10, we have used custom arguments for the pendulum() function to generate data for a pendulum moving along the x-axis.

  • In lines 13 - 14, we are plotting the motion of the pendulum x_t against time t.

Solution 2

We have to plot the following equations:

x(t)=2sin(8πt1)e0.05t+sin(4πtπ2)e0.01tx(t)=2sin(8\pi t -1)e^{-0.05t}+sin(4\pi t -\frac{\pi}{2})e^{-0.01t} ...