Solution Review: Harmonographs
Solution review to the harmonographs exercise.
We'll cover the following...
Solution 1 #
Press + to interact
import numpy as npimport matplotlib.pyplot as plt# defining function with default paramters valuedef 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 timet = np.linspace(0, 2 * np.pi, 1000)x_t = pendulum(t, 1, 2, 0, 0.5)# plotting and setting labelsplt.figure(figsize=(12, 9), dpi=200)plt.plot(t, x_t)# setting labelsplt.title('Damped Pendulum')plt.xlabel('t')plt.ylabel('x(t)')# saving figureplt.savefig('output/harmonograph1.png')
Explanation #
-
In lines 5 - 6, we have defined the function
pendulum()
and set the default values forA
,f
,p
, andd
. -
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 timet
.
Solution 2
We have to plot the following equations:
...