Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/notebooks/kitten.ipynb
Views: 531
Modeling and Simulation in Python
Case study.
Copyright 2017 Allen Downey
Unrolling
Let's simulate a kitten unrolling toilet paper. As reference material, see this video.
The interactions of the kitten and the paper roll are complex. To keep things simple, let's assume that the kitten pulls down on the free end of the roll with constant force. Also, we will neglect the friction between the roll and the axle.
This figure shows the paper roll with , , and . As a vector quantity, the direction of is into the page, but we only care about its magnitude for now.
We'll start by loading the units we need.
And a few more parameters in the Params
object.
make_system
computes rho_h
, which we'll need to compute moment of inertia, and k
, which we'll use to compute r
.
Testing make_system
Here's how we compute I
as a function of r
:
When r
is Rmin
, I
is small.
As r
increases, so does I
.
Exercises
Write a slope function we can use to simulate this system. Here are some suggestions and hints:
r
is no longer part of theState
object. Instead, we computer
at each time step, based on the current value ofy
, using
Angular velocity,
omega
, is no longer constant. Instead, we compute torque,tau
, and angular acceleration,alpha
, at each time step.I changed the definition of
theta
so positive values correspond to clockwise rotation, sodydt = -r * omega
; that is, positive values ofomega
yield decreasing values ofy
, the amount of paper still on the roll.Your slope function should return
omega
,alpha
, anddydt
, which are the derivatives oftheta
,omega
, andy
, respectively.Because
r
changes over time, we have to compute moment of inertia,I
, at each time step.
That last point might be more of a problem than I have made it seem. In the same way that only applies when is constant, only applies when is constant. When varies, we usually have to use a more general version of Newton's law. However, I believe that in this example, mass and moment of inertia vary together in a way that makes the simple approach work out. Not all of my collegues are convinced.
Test slope_func
with the initial conditions.
Run the simulation.
And look at the results.
Check the results to see if they seem plausible:
The final value of
theta
should be about 220 radians.The final value of
omega
should be near 4 radians/second, which is less one revolution per second, so that seems plausible.The final value of
y
should be about 35 meters of paper left on the roll, which means the kitten pulls off 12 meters in two minutes. That doesn't seem impossible, although it is based on a level of consistency and focus that is unlikely in a kitten.Angular velocity,
omega
, should increase almost linearly at first, as constant force yields almost constant torque. Then, as the radius decreases, the lever arm decreases, yielding lower torque, but moment of inertia decreases even more, yielding higher angular acceleration.
Plot theta
Plot omega
Plot y