Skip to content

Commit

Permalink
feat: add image of distributions
Browse files Browse the repository at this point in the history
  • Loading branch information
Michelle Ly committed Feb 14, 2025
1 parent 23cc87d commit b2c357e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 60 deletions.
91 changes: 31 additions & 60 deletions examples/01_Simulation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1497,45 +1497,16 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also define the shape of the noise to be uniform or triangular instead of normal. This example demonstrates a uniform process noise distribution."
"We can also define the shape of the noise to be uniform or triangular instead of normal. The image below shows the shapes of these distributions.\n",
"\n",
"![Graphs of normal, triangular, and uniform distributions](distributions.png \"Distributions\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# TODO: Add screenshot of shapes of distribution\n",
"\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from scipy.stats import norm, triang, uniform\n",
"\n",
"x=np.linspace(-4, 4, 1000)\n",
"norm_pdf = norm.pdf(x, loc=0, scale=1)\n",
"triang_pdf = triang.pdf(x, c=0.5, loc=-4, scale=8)\n",
"uni_pdf = uniform.pdf(x, loc=-4, scale=8)\n",
"\n",
"fig, ax = plt.subplots(1, 3, figsize=(14, 4))\n",
"\n",
"ax[0].plot(x, norm_pdf)\n",
"ax[0].set_title(\"Normal distribution\", pad=10)\n",
"ax[0].xaxis.set_visible(False)\n",
"ax[0].yaxis.set_visible(False)\n",
"\n",
"ax[1].plot(x, triang_pdf)\n",
"ax[1].set_title(\"Triangular distribution\", pad=10)\n",
"ax[1].xaxis.set_visible(False)\n",
"ax[1].yaxis.set_visible(False)\n",
"\n",
"ax[2].plot(x, uni_pdf)\n",
"ax[2].set_title(\"Uniform distribution\", pad=10)\n",
"ax[2].xaxis.set_visible(False)\n",
"ax[2].yaxis.set_visible(False)\n",
"\n",
"plt.tight_layout()\n",
"plt.show()"
" This example demonstrates a uniform process noise distribution."
]
},
{
Expand Down Expand Up @@ -1643,7 +1614,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"In this example, we will be using `simulate_to_threshold` with vectorized states. The ThrownObject model will be used to simulate multiple thrown objects."
"In this example, we will be using `simulate_to_threshold` with vectorized states. Vectorized states are a representation of a system's current conditions. The ThrownObject model will be used to simulate multiple thrown objects. Let's start by getting the necessary imports."
]
},
{
Expand All @@ -1661,22 +1632,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We will now set up the vectorized initial state. In this example, we will define 4 throws of varying positions (x) and strengths (v) in `first_state`. We will then simulate to the threshold using this state."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"first_state = {\n",
" 'x': array([1.75, 1.8, 1.85, 1.9]),\n",
" 'v': array([35, 39, 22, 47])\n",
"}\n",
"\n",
"m = ThrownObject()\n",
"simulated_results = m.simulate_to_threshold(x=first_state, events='impact', print = True, dt=0.1, save_freq=2)"
"We will also define a helper function to visualize the four throws."
]
},
{
Expand Down Expand Up @@ -1714,18 +1670,14 @@
" \n",
" plt.xlabel(\"time\")\n",
" plt.ylabel(\"state\") \n",
" plt.legend(loc='upper left', bbox_to_anchor=(1, 1))\n",
"\n",
"print_vectorized_sim_plots(simulated_results)\n",
"plt.title(\"Vectorized simulation until any object hits the ground\", pad=10)\n",
"plt.show()"
" plt.legend(loc='upper left', bbox_to_anchor=(1, 1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's do the same thing but only stop when all objects hit the ground by defining a `thresholds_met_eqn`."
"We will now set up the vectorized initial state. In this example, we will define 4 throws of varying positions (x) and strengths (v) in `first_state`. We will then simulate to the threshold using this state. We should see the simulation stop once any one object hits the ground."
]
},
{
Expand All @@ -1734,10 +1686,24 @@
"metadata": {},
"outputs": [],
"source": [
"def thresholds_met_eqn(thresholds_met):\n",
" return all(thresholds_met['impact'])\n",
"first_state = {\n",
" 'x': array([1.75, 1.8, 1.85, 1.9]),\n",
" 'v': array([35, 39, 22, 47])\n",
"}\n",
"\n",
"simulated_results = m.simulate_to_threshold(x = first_state, thresholds_met_eqn=thresholds_met_eqn, print = True, dt=0.1, save_freq=2)"
"m = ThrownObject()\n",
"simulated_results = m.simulate_to_threshold(x=first_state, events='impact', print=True, dt=0.1, save_freq=1)\n",
"\n",
"print_vectorized_sim_plots(simulated_results)\n",
"plt.title(\"Vectorized simulation until any object hits the ground\", pad=10)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's do the same thing but only stop when all objects hit the ground by defining a `thresholds_met_eqn`."
]
},
{
Expand All @@ -1746,6 +1712,11 @@
"metadata": {},
"outputs": [],
"source": [
"def thresholds_met_eqn(thresholds_met):\n",
" return all(thresholds_met['impact'])\n",
"\n",
"simulated_results = m.simulate_to_threshold(x = first_state, thresholds_met_eqn=thresholds_met_eqn, print=True, dt=0.1, save_freq=1)\n",
"\n",
"print_vectorized_sim_plots(simulated_results)\n",
"plt.title(\"Vectorized simulation until all objects hit the ground\", pad=10)\n",
"plt.show()"
Expand Down
Binary file added examples/distributions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b2c357e

Please # to comment.