\(
\newcommand{\blah}{blah-blah-blah}
\newcommand{\eqb}[1]{\begin{eqnarray*}#1\end{eqnarray*}}
\newcommand{\eqbn}[1]{\begin{eqnarray}#1\end{eqnarray}}
\newcommand{\bb}[1]{\mathbf{#1}}
\newcommand{\mat}[1]{\begin{bmatrix}#1\end{bmatrix}}
\newcommand{\nchoose}[2]{\left(\begin{array}{c} #1 \\ #2 \end{array}\right)}
\newcommand{\defn}{\stackrel{\vartriangle}{=}}
\newcommand{\rvectwo}[2]{\left(\begin{array}{c} #1 \\ #2 \end{array}\right)}
\newcommand{\rvecthree}[3]{\left(\begin{array}{r} #1 \\ #2\\ #3\end{array}\right)}
\newcommand{\rvecdots}[3]{\left(\begin{array}{r} #1 \\ #2\\ \vdots\\ #3\end{array}\right)}
\newcommand{\vectwo}[2]{\left[\begin{array}{r} #1\\#2\end{array}\right]}
\newcommand{\vecthree}[3]{\left[\begin{array}{r} #1 \\ #2\\ #3\end{array}\right]}
\newcommand{\vecdots}[3]{\left[\begin{array}{r} #1 \\ #2\\ \vdots\\ #3\end{array}\right]}
\newcommand{\eql}{\;\; = \;\;}
\newcommand{\dv}[2]{\frac{#1}{#2}}
\newcommand{\half}{\frac{1}{2}}
\newcommand{\mmod}{\!\!\! \mod}
\newcommand{\ops}{\;\; #1 \;\;}
\newcommand{\implies}{\Rightarrow\;\;\;\;\;\;\;\;\;\;\;\;}
\definecolor{dkblue}{RGB}{0,0,120}
\definecolor{dkred}{RGB}{120,0,0}
\definecolor{dkgreen}{RGB}{0,120,0}
\)
Module 4: Simulating Continuous Systems
4.1 Simulating a simple chemical reaction
We'll start by modeling a simple reaction involving three molecules:
- One molecule of substance A reacts with one molecule of
substance B to produce one molecule of C.
- Similarly, a molecule of C can breakdown and produce
one molecule each of A and B.
- We will write this as two reactions:
- A + B → C
- C → A + B
Exercise 1:
Download and unpack this ZIP file into a
new directory. Upon unpacking, you will see that it
creates a new subdirectory called VirtualReactor.
Go into that subdirectory and execute
VirtualReactor. This will bring
up a window. Here's what you do:
- Note that the Model menu offers you the choice of
different models. Select the first one: standard simulation.
- In the section entitled "Initial numbers of molecules"
enter 100 in the box for A, 50 in the box
for B, and 0 for C. Then click "Change" in the same row.
- Hit the Reset button in the "Animation" section.
- Then hit the "Go" button, and simulate until the
time shows about 100 (this is in the rightmost label of
the "Stats" section).
- About time 100, hit "Stop" and then "Plot".
You should see the time-evolution of the concentrations
of each of A, B and C.
- Finally, compare how long it takes to simulate 100 time
units when there are large numbers of molecules (10000 of A, 5000
of B, 0 of C).
Now let's explain how the software works:
- It simulates molecules in two dimensions, with small blue
"A" molecules, slightly larger green "B" molecules, and much
larger yellow "C" molecules.
- The molecules move about randomly, simulating molecules
in a solution or in a gaseous state.
- At any given instant, exactly one of the two reactions
(A+B → C, or C → A+B) takes place.
- How do we decide which reaction? This is where we
try to approximate reality. Here's the reasoning:
- The likelihood of the first reaction is proportional
to the number of A molecules and the number of B molecules.
- This should make intuitive sense: if you have lots of both,
there'll be plenty of such A's and B's combining.
- Similarly, the more we have of C, the more likely we'll
have the second reaction.
- Next, let
$$\eqb{
N_A & \eql & \mbox{ number of A molecules }\\
N_B & \eql & \mbox{ number of B molecules }\\
N_C & \eql & \mbox{ number of C molecules }\\
}
$$
- And let
$$\eqb{
E_A & \eql & \mbox{ concentration of A }\\
E_B & \eql & \mbox{ concentration of B }\\
E_C & \eql & \mbox{ concentration of C }\\
}
$$
- To get the concentration (mass per unit volume), we'll
approximate the actual physical volume as:
$$
V_{total} \eql N_A + N_B + 2 N_C
$$
(Note: a more general and accurate formulation of volume would use
the sizes of the different molecules and weight accordingly. We are
simply assuming A and B molecules are similar, and C is twice as big).
- Thus,
$$\eqb{
E_A & \eql & \frac{N_A}{V_{total}} \\
E_B & \eql & \frac{N_B}{V_{total}} \\
E_C & \eql & \frac{N_C}{V_{total}} \\
}$$
- Let
$$\eqb{
R_1 & \eql & \mbox{ Probability that the first reaction takes place }\\
R_2 & \eql & \mbox{ Probability that the second reaction takes place }\\
}$$
- Now, our model is going to assume that the chances
of the first reaction taking place is going to be proportional
to both the number of A molecules (NA)
and the number of B molecules (NB).
Thus,
$$\eqb{
R_1 & \; \propto \; & N_A \; N_B\\
R_2 & \; \propto \; & N_C
}$$
- Because the numbers (of molecules), \(N_A,N_B,N_C\) are
proportional to concentrations \(E_A,E_B,E_C\),
$$\eqb{
R_1 & \; \propto \; & E_A \; E_B\\
R_2 & \; \propto \; & E_C
}$$
- In each case, the constant of proportionality may be
slightly different. That is, it may be that the first
reaction is generally more likely, all other things being equal.
- To model this, we'll use constants \(p\) and \(q\):
$$\eqb{
R_1 & \eql & p \; E_A \; E_B\\
R_2 & \eql & q\; E_C
}$$
- Thus, what happens at each step of the simulation is this:
- We randomly choose between doing the first or second reaction,
based on the relative proportion of \(R_1\) and \(R_2\).
- If it's the first, then we go ahead and remove one each
of an A and a B molecule, and create a new C molecule.
- If it's the second, we remove one C molecule and create
a new A and a new B molecule.
Exercise 2:
Suppose you have ten A molecules and five B molecules.
- Compute the concentrations (by hand) of A and B using the
reasoning above.
Now run the simulation very slowly by clicking on the "Step" button.
(Note: Each time you start, remember to hit the "Reset" button.)
- How long before you see the second reaction taking place?
- Just after you see the second reaction taking place
for the first time, re-compute (by hand) the concentrations and
verify that they match with the numbers in the "Stats"
section.
Next, we will examine long-term behavior:
- Initially, with 100 molecules of A, 50 molecules
of B, and none of C, the concentrations of A, B and C
are 0.667, 0.333 and 0, respectively.
- After just a few steps, we can't really expect the number of A
molecules to decrease drastically.
- It takes a while before the system of molecules
"settles down."
Exercise 3:
Study the long-term behavior (starting with 100 molecules of A, 50
molecules of B):
- How long (in time-steps shown) does it take for the
concentrations to "settle"?
- Do the concentrations ever stay fixed after a while?
Why or why not?
- What are the final concentrations of A, B, and C,
approximately?
Let's examine how the simulation was written:
- At the highest-level, the simulation looks like this:
for n = 1 to numSteps
draw () // Draw molecules (circles)
doReactions () // Perform reaction
collectStats ()
endfor
printStats ()
- The detail for each reaction:
for n = 1 to numSteps
// Draw molecules (circles)
draw ()
// Compute concentrations
V = (NA + NB + 2*NC)
EA = NA / V
EB = NB / V
EC = NC / V
// Randomly choose one reaction
R1 = EA EB p
R2 = EC q
if uniform() < R1/(R1 + R2)
// A + B → C
Remove one A and one B, make one C
else
// C → A + B
Remove one C, make one A and one C
endif
collectStats ()
endfor
printStats ()
Exercise 4:
- What is the purpose of uniform() above and how
does it ensure that the reactions are selected with appropriate
probabilities?
- Examine the code in method standard() in
MolecularSimPanel.java. What is the relevance of the
spatial distance between molecules?
4.2 A more detailed model
How closely does our model correspond to reality?
- In reality, molecules react when they sort-of bump into each other.
- We did not consider "spatial" closeness at all.
We'll now model spatial detail:
- Molecules A and B will react only if close enough.
- We'll use a distance parameter that we can vary to decide
what's "close enough".
Exercise 5:
Use the "Model" menu and switch to the spatial model.
Using the same numbers of molecules as above, see what
happens when you run for about 100 time units and plot.
- What do you observe? Why are there more fluctuations?
- Next, look at the last row of controls. Set EndTime=100
and NumRuns=10. Then click the "simulate" button. What do you observe?
- Try the same with NumRuns=100. What do you see?
- For comparison, try NumRuns=10 and NumRuns=100 in
the standard simulation.
On randomness in the model:
- The spatial model is more realistic, but needs more
averaging (and hence more time).
- The spatial model has introduced another parameter: distance.
- The spatial model does not use concentration in its model.
Exercise 7:
-
If the spatial model doesn't use concentration, why then is
the behavior like the standard model?
- Find the place in the code that has the "distance" parameter.
What is the distance parameter set at? Double it and see
what you get. Can you explain? What is "doubling" the
distance equivalent to in terms of using other values
for \(p\) and \(q\)?
4.3 A less detailed model
What we desire in a model:
- Accurately matches reality.
- Simple to implement, simple to understand.
- Fast execution time computationally.
- As little randomness as possible.
- As few parameters as possible.
Consider this deterministic model:
- Let
$$\eqb{
A(t) & \eql & \mbox{ concentration of A at time } t\\
B(t) & \eql & \mbox{ concentration of B at time } t\\
C(t) & \eql & \mbox{ concentration of C at time } t\\
}$$
- Now, focus on \(A(t)\):
- Consider the concentration a few moments later at time
\(A(t+s)\) where \(s\) is a small value.
- In the time period, \(s\), that has elapsed, what
affects the concentration of A?
- Let's consider the change in concentration,
\(A(t+s) - A(t)\).
- \(A(t+s) - A(t)\) could decrease because A molecules react with B molecules
\(\Rightarrow\)
the amount of this occuring depends on \(A(t)\)
and \(B(t)\)
\(\Rightarrow\)
The more of \(A(t)\) \(B(t)\), the more that reaction will happen.
- \(A(t+s) - A(t)\) could increase because of C molecules that break down.
\(\Rightarrow\)
the amount of this occuring depends on \(C(t)\)
\(\Rightarrow\)
The more of \(C(t)\), the more this break down occurs.
- Thus, in that small time period:
\(A(t+s) - A(t)\) decreases in proportion to \(A(t) B(t)\) (the product of these concentrations)
\(A(t+s) - A(t)\) increases in proportion to \(C(t)\)
- Now, the total amount of change also depends on the length
of the time-period, \(s\).
\(\Rightarrow\)
the more \(s\) is, the more the change
(because there's more time to allow more reactions).
- Putting this all together:
$$
A(t+s) - A(t) \; \propto \; s (C(t) - A(t)B(t))
$$
- We could write this as an equation:
$$
A(t+s) - A(t) \eql s (C(t) - A(t)B(t))
$$
- By the same reasoning,
$$
B(t+s) - B(t) \eql s (C(t) - A(t)B(t))
$$
- The change in concentration of C over this small
period is different:
- \(C(t+s) - C(t)\) increases with more \(A(t)\) and
more \(B(t)\).
- And it decreases in proportion to \(C(t)\).
Thus,
$$
C(t+s) - C(t) \eql s (A(t)B(t) - C(t))
$$
- One small but important modification:
- It may be that the the forward reaction (A+B → C) is more
likely than the reverse.
- To adjust, we'll introduce multipliers for the two terms.
-
Thus, our revised model is:
$$\eqb{
A(t+s) - A(t) & \eql & s \; (K_c C(t) - K_{ab} A(t)B(t)) \\
B(t+s) - B(t) & \eql & s \; (K_c C(t) - K_{ab} A(t)B(t)) \\
C(t+s) - C(t) & \eql & s \; (K_{ab} A(t)B(t) - K_c C(t))
}$$
This way, we can use a high value of the constant \(K_{ab}\)
to indicate that the forward reaction is more likely, or happens
at a faster rate.
- Thus, we have a way to calculate the
changes in concentrations as we go forward in time.
$$\eqb{
A(t+s) & \; \leftarrow \; & A(t) \; + \; s \; (K_c C(t) - K_{ab} A(t)B(t)) \\
B(t+s) & \; \leftarrow \; & B(t) \; + \; s \; (K_c C(t) - K_{ab} A(t)B(t)) \\
C(t+s) & \; \leftarrow \; & C(t) \; + \; s \; (K_{ab} A(t)B(t) - K_c C(t))
}$$
- How?
- Suppose we know the starting concentrations at
time \(t=0\), that is, we know \(A(0), B(0)\) and \(C(0)\).
- Suppose, for example, that \(s=0.01, K_{ab}=1.0, K_c=0.5\).
- We can now compute
$$\eqb{
A(0+0.01) \eql A(0) \; + \; 0.01 \; (0.5 C(0) - 1.0 A(0)B(0))\\
B(0+0.01) \eql B(0) \; + \; 0.01 \; (0.5 C(0) - 1.0 A(0)B(0))\\
C(0+0.01) \eql C(0) \; + \; 0.01 \; (1.0 A(0)B(0) - 0.5 C(0))
}$$
Exercise 8:
Suppose \(s=0.01, K_{ab}=1.0, K_c=0.5\),
and \(A(0)=3.0, B(0)=2.0, C(0)=1.5\). Compute the concentrations
of \(A(t), B(t), C(t)\) at times \(t=0.01, t=0.02, t=0.03\)
as follows:
- First, calculate \(A(0.01), B(0.01), C(0.01)\).
- Then, use these values to calculate \(A(0.02), B(0.02), C(0.02)\).
- Then, use the values at 0.02 to
calculate \(A(0.03), B(0.03), C(0.03)\).
Exercise 9:
Write a Java program to compute concentrations using this
model. Start by using this template.
Then, modify the code to change the end-time to 1.0.
What do you observe as the final concentrations of A, B and C?
Plot a graph of how the concentrations change
with time, and compare with previous models.
Exercise 10:
How would one go about writing the code so that it's recursive?
That is, our goal is to compute \(A(T), B(T), C(T)\) for
some large \(T\), which we could do recursively in
terms of \(A(T-s), B(T-s), C(T-s)\), which in turn can
computed from \(A(T-2s), B(T-2s), C(T-2s)\) ... etc.
Write a recursive version and
compare the number of arithmetic operations in the recursive
approach vs. the iterative one.
How is this simulation (model) different from the others?
- We have abstracted molecules away:
- We don't count numbers of molecules.
- Instead, there's this "quantity" called concentration,
defined by a single number.
- This shouldn't come as a surprise because, even though
we modeled individual molecules earlier, the "stats" panel
and the plots showed concentrations (one number each for
A, B and C).
- We have abstracted away all randomness:
- No longer do we worry about how molecules move.
- The concentrations change deterministically.
- This is of course a further approximation.
- Are there benefits to this?
- It's faster to compute. This works for any number of
molecules, including realistic values (trillions, gazillions).
- It's a simpler model. The code is only a few lines.
- Any downsides?
- There are new parameters, which might be hard to measure
in practice.
- There may be something to learn from the detailed
molecular level that might be lost in this model.
Exercise 11:
In general, how accurate should a model be? Consider this Physics
experiment:
- Suppose, from atop a 300-foot building on campus
you fix a point on a protruding
rod where you can drop a heavy (so that wind won't have an effect)
object to observe where it lands on the ground.
- From that same drop-point, you let down a plumb-line and mark
the spot where the plumb line touches the ground.
- Now you drop the heavy object.
- You expect the dropped object to hit the marked spot
dead on, right?
Discuss with your neighboring students/team, whether:
- The object hits the marked spot dead-on.
- The object lands a little to the West.
- The object lands a little to the East.
- The object lands a little to the North.
- The object lands a little to the South.
- Something else. (What?)
The answer to this question, it turns out, has had tremendous
historical significance.
4.4 What is a differential equation?
Consider our reaction example:
- Recall how we compute the concentrations evolving over time:
$$\eqb{
A(t+s) & \eql & A(t) \; + \; s \; (K_c C(t) - K_{ab} A(t)B(t)) \\
B(t+s) & \eql & B(t) \; + \; s \; (K_c C(t) - K_{ab} A(t)B(t)) \\
C(t+s) & \eql & C(t) \; + \; s \; (K_{ab} A(t)B(t) - K_c C(t))
}$$
- The form of these equations is
Next value \(A(t+s)\) = current value \(A(t)\) + \(s
\;\times \;\) (some
terms involving current values of various variables)
- Equivalently, the equations specify an iterative
algorithm to compute the functions \(A(t), B(t), C(t)\).
- The fact that we compute in the way time evolves makes
this a simulation.
Now consider the following:
- Rearrange the terms in this way:
$$\eqb{
\frac{A(t+s) - A(t)}{s} & \eql & K_c C(t) - K_{ab} A(t)B(t) \\
\frac{B(t+s) - B(t)}{s} & \eql & K_c C(t) - K_{ab} A(t)B(t) \\
\frac{C(t+s) - C(t)}{s} & \eql & K_{ab} A(t)B(t) - K_c C(t)
}$$
- This is merely a re-statement that doesn't change anything
Except: it is no longer suitable for computation.
- Next, consider the limit as \(s \to 0\):
- For fixed \(s\), \(\frac{A(t+s) - A(t)}{s} \) is a function.
- Let's give these a name: Define the function
$$A_s(t) = \frac{A(t+s) - A(t)}{s}$$
- Then the sequence of functions \(A_s\)
possibly has a limit as \(s \to 0\).
- That limit is itself a function.
- Let's give it a name: \(A^\prime(t)\)
\(\Rightarrow\)
Called the derivative (function) of function \(A(t)\)
- If we do this for each of \(A(t), B(t), C(t)\), we get
$$\eqb{
A^\prime(t) & \eql & K_c C(t) - K_{ab} A(t)B(t) \\
B^\prime(t) & \eql & K_c C(t) - K_{ab} A(t)B(t) \\
C^\prime(t) & \eql & K_{ab} A(t)B(t) - K_c C(t)
}$$
Which is sometimes written in slightly different notation as:
$$\eqb{
\frac{dA}{dt} & \eql & K_c C(t) - K_{ab} A(t)B(t) \\
\frac{dB}{dt} & \eql & K_c C(t) - K_{ab} A(t)B(t) \\
\frac{dC}{dt} & \eql & K_{ab} A(t)B(t) - K_c C(t)
}$$
- Notice the following:
- The equations specify complex relationships involving
six functions
\(\Rightarrow\)
Three derivative functions in addition to \(A(t), B(t), C(t)\).
- These relationships are free of \(s\).
\(\Rightarrow\)
We don't have to worry about whether \(s\) is small enough or too small.
- The left sides are all derivative functions.
- The right sides are all regular functions.
- Such a collection of equations is called a system of
differential equations.
- Note: we usually call these Ordinary Differential
Equations (ODEs) to distinguish them from other kinds (like partial
differential equations).
What good are they?
- In the days without computers, one had to reason about these
systems
\(\Rightarrow\)
Recall, limits usually result in simplification (no \(s\) term)
- ODE's are the basis for analyzing scores of real systems.
- Some ODE's can be solved analytically.
- Many more can be analyzed for properties (stability,
long-term behavior etc) even if they can't be explicitly solved.
- There are many powerful existence results that can be proved
with great generality.
Relationship to computation:
- Most ODE's are mathematically constructed with finite \(s\) and
then taken to the limit
- But leaving the equations with finite \(s\) gives us an
algorithm for solving the system
\(\Rightarrow\)
The Euler algorithm
- When the free variable \(t\) describes time, we could
call this a simulation.
\(\Rightarrow\)
A simulation computes a system evolving over time.
- Generally, for many systems, the Euler algorithm is sufficient
\(\Rightarrow\)
More accurate algorithms exist (e.g., the Runge-Kutta algorithm)
4.5 An unusual "reaction"
Consider the Rabbit-Lynx "reaction":
- Rabbit=prey, lynx=predator
- Although this is not a chemical reaction, it almost is (mathematically).
- We will model a population of rabbits and lynxes living in an
environment with sufficient grass for the rabbits to thrive.
The lynxes, of course, eat the rabbits to thrive.
- Already, we can sense the proportions involved:
- The rabbits grow at a certain rate.
- The rabbits die at a rate, proportional to how many rabbits
there are and how many are being devoured by lynxes.
- The lynxes proliferate based on their consumption of
rabbits, and die in numbers proportional to their population.
- We will model this system:
- Let
$$\eqb{
X(t) & \eql & \mbox{ the number of rabbits at time } t\\
Y(t) & \eql & \mbox{ the number of lynxes at time } t
}$$
- Now consider a particular time interval from \(t\) to \(t+s\):
- \(X(t+s) - X(t)\) is the change in the number of rabbits.
\(X(t+s) - X(t)\) will increase from new rabbits born
\(X(t+s) - X(t)\) will decrease from rabbits dying naturally
\(X(t+s) - X(t)\) will decrease from rabbits killed by lynxes
\(X(t+s) - X(t)\) is also proportional to \(s\).
- Thus, we can write
$$
X(t+s) - X(t) \eql s \; (k_1 X - k_2 X Y)
$$
- Similarly,
$$
Y(t+s) - Y(t) \eql s \; (k_2 X Y - k_3 Y)
$$
because the rabbits that are killed are the ones that "generate"
the lynxes.
- Note: we won't worry about the fact that actual numbers of
rabbits are integers, and that \(X(t)\) can take a value like
\(0.3\). Assume that we are counting in the "thousands",
in which case \(0.3\) makes sense.
- Now, we don't need anything further (other than the constants
\(k_1, k_2, k_3\) to solve this computationally.
- For illustration, let us write the differential equations:
$$\eqb{
X^\prime(t) & \eql & k_1 X - k_2 X Y \\
Y^\prime(t) & \eql & k_2 X Y - k_3 Y
}$$
Exercise 12:
Run the VirtualReactor program and select the "Rabbit-Lynx" model.
Use these values: \(X(0)=1.5, Y(0)=1.0, k_1=2.4,
k_2=4.2, k_3=5.1, endTime=10\).
(Ignore the \(k_4\) parameter.)
You should observe something quite interesting.
About the Rabbit-Lynx model:
- We see that there is a natural oscillation in the populations
of rabbits and lynxes:
- The populations don't ever "settle".
- Oscillation is "built in" to the model.
- We could hardly have predicted this by staring at
the equations or through intuition.
- The population growth of lynxes lags that of rabbits slightly
as intuition would suggest.
- If we think of rabbits and lynxes as interacting "molecules",
the "reactions" could be written as:
grass + rabbits → 2 rabbits
rabbits + lynxes → 2 lynxes
lynxes → dead lynxes (degradation)
- This model has been frequently studied, in all kinds of
variations, in ecology.
- The model is called a Lotka-Volterra population model.
One wonders: is there a chemical reaction that displays oscillation?
- Indeed, there is.
- The B-Z reaction :
- In 1951, the Russian chemist Belousov discovered a chemical
reaction involving citric acid, acidified bromate and ceric ions
displayed spectacular oscillations: turning yellow, then colorless,
then yellow, in turn.
- Belousov couldn't publish his result because nobody believed it.
- Later, a student called Zhabotinsky resurrected the work
and convinced others, too late unfortunately for Belousov,
who died in 1970 before receiving the prestigious Lenin Prize
posthumously in 1980.
- Type "BZ reaction" in Google to
see some pictures of the BZ reaction.
- The differential equations, after some simplification,
turn out to be:
$$\eqb{
X^\prime(t) & \eql & q Y(t) - X(t)Y(t) + X(t)(1 - X(t))\\
Y^\prime(t) & \eql & \frac{1}{e} (-q Y(t) - X(t)Y(t) + f Z(t))\\
Z^\prime(t) & \eql & X(t) - Z(t)
}$$
There are three variables, depicting three concentrations.
4.6 Even stranger behavior
Next, we'll look at a slightly different type of population model:
- The Ricker model is:
$$
x(n+1) \eql r \, x(n) \, e^{-x(n)}
$$
- Here, time is modeled as discrete time steps, where
$$
x(n) \eql \mbox{population at step \(n\)}
$$
- There is a growth part modeled on the right by
$$
x(n+1) \eql r \, {\bf x(n)} \, e^{-x(n)}
$$
and a decline modeled by
$$
x(n+1) \eql r \, x(n) \, \bf{ e^{-x(n)}}
$$
- Thus, one might expect the sequence
$$
x(1), x(2), x(3), \ldots,
$$
to converge to a "stable" population, called an attractor.
- For each \(r\), it seems plausible that we'll get a
different stable population for large enough \(n\).
- What we'll do:
- For a given \(r\), run the sequence and compute a histogram
of all the \(x(i)\) values obtained.
- If the sequence does in fact stabilize, we should see a
single convergence point show up in the histogram.
- We'll plot any convergence point that shows up more than
once, after ignoring \(x(1),\ldots, x(50)\) (to allow convergence).
- Thus, we will have a collection of attractor points for
any fixed \(r\).
- We'll plot the attractors for different values of
\(r\).
Exercise 13:
Download and execute
RickerModel.java.
You will also need
DrawTool.java.
What do we learn from these examples?
- A changing system can be mathematically modeled in
many ways, the most common of which are:
- Fully continuous: using differential equations.
- Fully discrete: for example, Boolean circuits and graphs.
- Mixed discrete and continuous, as in the Ricker model.
⇒
Typically, time is discrete
- Some systems have attractors or attractor cycles, while
others exhibit the phenomenon of chaos.
- In general, it is very hard to predict the behavior
of dynamical systems just by staring at the governing equations.
- For some parameters, a system might behave "well"
- For others, one might get wild oscillations or chaos.
4.7 More examples
Motion along a straight line:
- Let's revisit InclineSimulator.java:
while (true) {
// Update time.
t += delT;
// Increase velocity accordingly.
v = v + delT * a;
// Increase distance according to velocity.
d = d + delT * v;
if (t >= stopTime) {
break;
}
}
- Note: the straight line, in this case, is along the incline.
- Clearly, the differential equations are:
$$\eqb{
v^\prime(t) & \eql & a\\
d^\prime(t) & \eql & v
}$$
Exercise 14:
Download and execute Missile.java.
Examine the code and write down the differential equations.
How many variables (functions) are involved?
Next, let's write down the differential equations for the Dubin car:
- Recall: the Dubin car has two wheels, each of whose angular
velocity is separately controlled.
- Define the following:
\((x,y)\) = center of the axle between the two wheels.
\(\omega_L, \omega_R\) = angular
velocities of left and right wheels
\(R\) = radius of wheel
\(L\) = length of axle
\(\theta\) = current orientation
- Consider movement from center at \(C\) to \(C'\) below
in a small interval of time \(\Delta t\):
- The distance \(CC = \half (AA' + BB')\).
- Consider a small interval of time \(\Delta t\). Then,
$$\eqb{
AA^\prime & \eql & \omega_L R \Delta t \\
BB^\prime & \eql & \omega_R R \Delta t
}$$
- Thus,
$$\eqb{
x^\prime(t) & \eql & \half R (\omega_L + \omega_R) \cos(\theta)\\
y^\prime(t) & \eql & \half R (\omega_L + \omega_R) \sin(\theta)\\
}$$
Exercise 15:
Why is this true?
- The only other variable is the orientation \(\theta(t)\)
\(\Rightarrow\) We need a differential equation \(\theta^\prime(t) = ... \)
- Consider the case that only the left wheel moves:
- The distance moved
$$AA' \eql \omega_L R \Delta t$$
- Thus, the angle \(AB-AA'\) is:
$$ \Delta\theta \eql \frac{\omega_L R \Delta t}{L} $$
Exercise 16:
Why is this true?
- Thus,
$$
\theta^\prime(t) \eql \frac{\omega_L R}{L}
$$
- In general, with both wheels moving, the rate at which the
turn occurs in the clockwise direction is
$$
\theta^\prime(t) \eql \frac{(\omega_L - \omega_R) R}{L}
$$
Exercise 17:
Write pseudocode for a Dubin car simulator based on these equations.
Exercise 18:
What additional equation is needed for an accelerative version of the
Dubin car?
Exercise 19:
Write down the equations for the Simple-Car which has two controls:
forward-velocity \(v\) and steering angle \(\phi\).
Implement a simulator for the Simple-Car in CarSim.
(To draw one, merely draw a circle).
An example with rotational motion:
- Consider a weight attached to a winch:
- Define the following variables for the wheel:
\(R\) = radius of wheel
\(\tau\) = torque
\(\theta(t)\) = angular displacement at time \(t\)
\(\omega(t)\) = angular velocity
\(\mu(t)\) = angular acceleration
\(m_w\) = mass of wheel
- Define the following variables for the cable:
\(T\) = tension in the cable
- Define the following variables for the load:
\(m\) = mass of load
\(y(t)\) = vertical displacement of load
\(v(t)\) = vertical velocity of load
\(a(t)\) = vertical acceleration of load
- Now apply Newton's laws to each component. Let's start with
the wheel:
- The wheel has torque generated from the winch's motor: \(\tau\)
- The tension in the cable pulls the other way, and creates a
reverse torque of \(TR\)
- Thus, the difference is what determines the angular motion of
the wheel:
$$m_w R^2 \mu \eql \tau - TR$$
Note: we've used moment of inertia (mass analog for angular motion)
- Now the load:
- The upward force is the cable's tension: \(T\)
- The downward force is the weight \(mg\)
- The difference explains the motion:
$$ma = T - mg$$
- What are the differential equations?
- First, the obvious ones relating acceleration, velocity and
displacement for the wheel:
$$\eqb{
\omega^\prime(t) & \eql & \mu(t)\\
\theta^\prime(t) & \eql & \omega(t)
}$$
- The same for the load:
$$\eqb{
v^\prime(t) & \eql & a(t)\\
y^\prime(t) & \eql & v(t)
}$$
- Observe that \(a(t) = R\mu(t)\)
Exercise 20:
Why?
- Finally, let's relate these through the tension:
- From the wheel:
$$T \eql \frac{(\tau - m_w R^2 \mu)}{R} $$
- From the load:
$$ T \eql mR\mu - mg $$
- Equating the two and solving for \(\mu\):
$$
\mu \eql \frac{\tau - mgR}{mR^2 + m_w R^2}
$$
- Thus, the equations that describe the system are:
$$\eqb{
\mu(t) & \eql & \frac{\tau - mgR}{mR^2 + m_w R^2}\\
\omega^\prime(t) & \eql & \mu(t)\\
\theta^\prime(t) & \eql & \omega(t)\\
y(t) &\eql & R\theta(t)
}$$
- The equations provide a means of computing the
values of these variables in a simulation.
Exercise 21:
Download and examine Winch.java.
- Identify the code corresponding to the differential equations
we derived earlier.
- Handcode different values of torque to see what value is
sufficient to lift the load. Is there a value that will
lift the load and yet avert a collision with the winch?
Exercise 22:
Set isVertical=false in the program and execute.
You should see a version without gravity (as if the load
were on a frictionless surface). What value of torque is
sufficient to pull the load?
© 2008, Rahul Simha