This notebook is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
import oscillator
undamped_df = oscillator.simulation(
sim_time=5.0,
delta_t=0.01,
mass=0.2,
spring_constant=10,
unweighted_length=1.000,
init_displacement=0.3,
radius=None,
mode="undamped",
)
damped_df = oscillator.simulation(
sim_time=5.0,
delta_t=0.01,
mass=0.2,
spring_constant=10,
unweighted_length=1.000,
init_displacement=0.3,
radius=np.sqrt(0.10 / np.pi),
mode="damped",
)
simulation_df_new = pd.concat(
[undamped_df[["time", "length", "velocity", "mode"]],
damped_df[["time", "length", "velocity", "mode"]]],
ignore_index=True
)
fig, ax = plt.subplots(nrows=2, ncols=1, dpi=120, sharex=True)
sns.lineplot(x="time", y="length", hue="mode", data=simulation_df_new, ax=ax[0], legend=False)
sns.lineplot(x="time", y="velocity", hue="mode", data=simulation_df_new, ax=ax[1])
ax[1].legend(loc='center left', bbox_to_anchor=(1, 1.1));
import bungee
nodrag_df = bungee.simulation(
sim_time=60,
delta_t=0.01,
mass=80,
spring_constant=6,
unweighted_length=30,
radius=None,
mode="no_drag",
)
withdrag_df = bungee.simulation(
sim_time=60.0,
delta_t=0.01,
mass=80,
spring_constant=6,
unweighted_length=30,
radius=np.sqrt(0.10/np.pi),
mode="with_drag",
)
bungee_df = pd.concat(
[nodrag_df[["time", "length", "velocity", "mode"]],
withdrag_df[["time", "length", "velocity", "mode"]]],
ignore_index=True
)
fig, ax = plt.subplots(nrows=2, ncols=1, dpi=120, sharex=True)
sns.lineplot(x="time", y="length", hue="mode", data=bungee_df, ax=ax[0], legend=False)
sns.lineplot(x="time", y="velocity", hue="mode", data=bungee_df, ax=ax[1])
ax[1].legend(loc='center left', bbox_to_anchor=(1, 1.1));
After our discussion where we carefully step through all the individual components of the model, take some of our earlier classroom code as a starting point, and try and implement this model yourself with the time we have left in class. Remember, the general workflow is the following:
# Instructor solution implemented in sharks.py
import sharks
sharks_df = sharks.simulation()
fig, ax = plt.subplots(dpi=120)
ax.plot(sharks_df["time"], sharks_df["bts"], "-", label="BTS shark population")
ax.plot(sharks_df["time"], sharks_df["wts"], "-", label="WTS shark population")
ax.set_xlabel("time")
ax.set_ylabel("population")
ax.legend(loc="best");