Mech221ComputerLab6
Mech221ComputerLab6
y
x
L cos θ
θ L
L sin θ
Balancing torques around the pivot point leads to the equation of motion (EOM)
g
θ̈(t) + sin θ(t) = 0. (∗)
L
The sine function appearing here makes the differential equation (∗) nonlinear, and this puts it
outside the scope of the methods we have developed so far. In fact, no known methods will produce
an algebraic formula for solutions of (∗). But even without a formula, it is possible to compute
highly accurate approximations to θ(t) for various initial conditions. Matlab has built-in functions
for just this purpose, and this lab will introduce the most popular of them, ode45.
With the help of ode45, we will investigate a number of features of pendulum motion:
(i) conservation of energy;
(ii) discrepancy between the function θ(t) and a pure sinusoid; and
(iii) relationship of period to swing-amplitude.
Our baseline for comparison in these categories is the related ODE built from the small-angle
approximation:
sin θ ≈ θ for |θ| ≪ 1.
This approximate EOM, namely,
g
θ(t) = 0,
θ̈(t) + (†)
L
is linear, so we can write down its general solution immediately:
r
g
θlin (t) = C cos t+φ , C > 0, φ ∈ R.
L
File “todo6”, version of 24 November 2014, page 1. Typeset at 12:21 November 24, 2014.
2 MECH 221 Computer Lab 6: The Simple Pendulum
Each of these solutions is a pure sinusoid whose period is unrelated to the amplitude factor C; for
any choices of C and φ, we do have
1
2
mL2 θ̇(t)2 + 12 mgLθ(t)2 = const,
but the “conserved quantity” on the left is only an approximation to the energy of the true pen-
dulum. We can anticipate seeing something interesting in each of the three categories mentioned
above.
While we deepen our understanding of the physics of the true pendulum, we will also gain
experience with Matlab’s built-in ODE solver, ode45. This function solves systems of first order
differential equations. So before we can get started, we must define/invent the auxiliary function
ω(t) = θ̇(t) to express the EOM in the first-order form:
θ̇ ω
= . (∗∗)
ω̇ −(g/L) sin θ
For comparison, the small-angle approximation leads to a slightly different system:
θ̇ ω 0 1 θ
= = , (‡)
ω̇ −(g/L)θ −g/L 0 ω
Computer Lab 5 provides experience with linear systems like (‡): we know how to set up a phase
plane diagram and relate the solution function θlin (t) to the motion of a point in the (θ, ω)-plane.
Today’s project will extend this experience by filling in the phase plane trajectories for (∗∗) with
computed approximations, and creating plots that give different views of the information encoded
in the phase plane diagram. (For more discussion of the phase plane, see Appendix A.)
With the familiar definition ω = θ̇, it follows that the pendulum’s kinetic energy is
T (ω) = 21 mv 2 = 12 mL2 ω 2 .
Let’s choose the reference point for measuring potential energy at the bottom of the pendulum’s arc,
def
where y = y0 = −L. For any pendulum angle θ, the mass’s height above this point is h = y − y0 ,
so the pendulum’s potential energy is
V (θ) = mgh = mg ([−L cos θ] − [−L]) = mgL(1 − cos θ).
As the pendulum moves, both T and V will change. Their sum is the function
Etot (t) = T (ω(t)) + V (θ(t)) = 12 mL2 ω(t)2 + mgL(1 − cos θ(t)).
The derivative of this function contains useful information. Thanks to the EOM (∗), we have
dEtot 1 2
2
h g i
= 2 mL [2ω ω̇] + mgL 0 + sin θ θ̇ = mL θ̇ θ̈ + sin θ = 0.
dt L
This is important: for any unforced motion of the pendulum, the total energy must constant!
File “todo6”, version of 24 November 2014, page 2. Typeset at 12:21 November 24, 2014.
MECH 221 Computer Lab 6: The Simple Pendulum 3
Special Values. Let’s focus on a particular setup where the physical quantities m, g, and L have
convenient numerical values. Adjusting the units as needed, we take m = 1 and g = L. Then the
key ingredients of our study simplify as follows:
• the true equation of motion θ̈(t) + sin θ(t) = 0,
• the approximate equation of motion θ̈(t) + θ(t) = 0, useful when |θ(t)| ≪ 1,
• the coordinate relationships x = sin θ, y = − cos θ,
• the angular velocity definition ω(t) = θ̇(t),
• the scaled kinetic energy K(t) = L−2 T (ω(t)) = 21 ω(t)2 ,
• the scaled potential energy P (t) = L−2 V (θ(t)) = 1 − cos θ(t),
• the scaled total energy E(t) = L−2 Etot = K(t) + P (t).
Energy-Based Predictions. When the pendulum is at its lowest point, y = −L, its potential
energy is 0, thanks to our choice of reference level. The physical setup requires −L ≤ y ≤ L always,
so there is an absolute ceiling on the potential energy: in the notation above, the scaled potential
Force-Based Predictions. There is a perfect analogy between the approximate EOM (†) and the
equation of motion for a standard mass-spring system with m = 1:
ÿ + ky = 0.
In this system, the exact solutions really are pure sinusoids. Using a weaker spring corresponds to
using a smaller constant k, and the result is lower accelerations and a longer period of oscillation.
To compare with this, let’s rearrange our pendulum EOM (∗) as
sin θ
θ̈(t) + θ(t) = 0.
θ
This puts the ratio sin θ/θ in the role of the spring constant k above. Although this ratio is not
constant, the analogy can help us make some rough predictions. It’s a basic math fact that sin θ < θ
File “todo6”, version of 24 November 2014, page 3. Typeset at 12:21 November 24, 2014.
4 MECH 221 Computer Lab 6: The Simple Pendulum
whenever θ > 0, and the ratio sin θ/θ decreases as θ grows. Thus the force that acts to correct
positive deviations from θ = 0 is always weaker than the linear correction force one might expect
from an ordinary spring. The weakening is slight near θ = 0, but the restoring force gets smaller
as the angles get larger. Thus it makes sense to predict that the period of our ideal pendulum
will always be larger than 2π, and that the period might actually increase as the swing amplitude
grows.
Note that θlin (t)2 + ωlin (t)2 = C 2 , so the phase-plane path of the point (θ, ω) is a circle centred at
the origin. The radius of that circle equals the amplitude of the oscillation in θlin (t). Further, the
function θlin (t) is periodic with period 2π, and the value of C makes no difference to the period.
To get the computer to return a useful approximation to the solution y(t), we need to inform the
machine about the time interval of interest, the initial vector, and the function G. Here is a Matlab
command that does all this:
• ode45 is the name of Matlab’s built-in solver. Matlab has other solvers, too, but they are for
problems with particular features. Say doc ode45 at the Matlab prompt for more information.
• startvector is the Matlab version of y0 , presented as a column vector.
• tspan is a row vector listing the times t for which we are requesting computed values of y(t).
The first element, tspan(1), should be the initial time t0 . Matlab will solve (3.1) on the interval
[t0 , t1 ], where t1 is the user-provided number in the last element of vector tspan. (In this lab we
must make sure tspan contains more than 2 elements, because ode45 does something different
in that case.)
• Tcol is a column vector of times from t0 to t1 returned by ode45. It declares the t-values
for which computed values of y(t) are being provided. Typically, the elements in Tcol are
identical to the requested times in tspan. (In the special case where tspan contains just 2
elements, t0 and t1 , ode45 automatically chooses a sequence of points in the interval [t0 , t1 ]
using some sophisticated error-control techniques, and returns these points in Tcol. We will
File “todo6”, version of 24 November 2014, page 4. Typeset at 12:21 November 24, 2014.
MECH 221 Computer Lab 6: The Simple Pendulum 5
need equally-spaced times in Tcol, so we can’t rely on Matlab’s automated node generation
scheme.)
• Ycols is a matrix holding the computed values of the solution vector y(t) at particular instants.
Suppose t5 is the time value in Tcol(5). Then the entries in row 5 of Ycols will be the
components of the vector y(t5 ).
In another view, the desired solution y = (y1 , y2 , . . . , yN ) is a time-varying vector with N
components. Each component is a time-varying scalar function. Selected values of function
y1 (t) are returned in column 1 of Ycols; corresponding values for y2 (t) appear in column 2;
etc. Each column lines up properly with the t-values in Tcol, so we can draw the graph of
component function y2 (t) by saying
plot(Tcol,Ycols(:,2));
(This function actually makes no reference to the value of the input variable t, but ode45 is
designed always to apply G with the state vector in the second position, so the definition of G
must allow for a t-value even if it is never used.)
dothandle = plot(0,0,’r.’);
This puts a red dot at the origin, and returns a handle to that object. Saying get(dothandle) will
reveal a long list of property/value pairs, each of which we can change. Two interesting properties
are named XData and YData: these tell the dot’s current location. To move the same dot to (2, 1),
it suffices to say
set(dothandle,’XData’,2,’YData’,1);
drawnow;
To produce a figure where the dot seems to move, we just need a loop that alternately sets new
coordinates and then refreshes the view by giving commands like these. To regulate the speed of
the update process, we can insert a command like pause(0.1) in the loop. Generally, pause(t) will
pause for approximately t seconds, so pause(0.1) will give about 10 updates per second.
File “todo6”, version of 24 November 2014, page 5. Typeset at 12:21 November 24, 2014.
6 MECH 221 Computer Lab 6: The Simple Pendulum
In support of this lab, two functions are provided on Connect. The first one, newpendulum,
creates a new figure window containing three subplots that show the phase plane, a sketch of the
pendulum itself, and an energy monitor, like this:
5
3
0
2
−5
1
−10
0
−10 0 10
−1
−2
Energy of point mass in pendulum bob
−3
0.6
−4 0.4
0.2
−5
−5 0 5 0
Rod angle θ, radians CCW from straight down
−0.2
0 100 200
PE is red, KE is green, PE+KE is cyan
The input argument provides the text for the figure window: you will want to change Inigo Mon-
toya’s name to your own, and choose a more meaningful title. The output variable, here hlist,
will be assigned a collection of graphics handles, one for each of several elements in the new figure.
Any other script or function with access to these handles can make changes to the figure by using
the set command as described above. Reading the code in newpendulum.m may be educational, but
it should not be necessary to make any changes to this file during today’s lab.
The second given function, named newstate, responds to commands like this:
newstate(hlist,theta,omega);
The first input is the cell array of graphics handles produced by newpendulum. Then the real scalar
variables theta and omega provide the values for a pendulum angle θ and angular velocity ω to
be shown in the monitoring window. Knowing just these two scalars is enough to update (i) the
File “todo6”, version of 24 November 2014, page 6. Typeset at 12:21 November 24, 2014.
MECH 221 Computer Lab 6: The Simple Pendulum 7
position of the model pendulum, (ii) the values of the kinetic and potential energy, and (iii) the
current point in the (θ, ω)-phase plane. The handles that function newstate takes in through the
parameter hlist allow it to address the relevant elements of the pendulum monitoring window and
make all the required changes. It will be necessary to make some changes to this file during today’s
lab.
Details of the cell array that newpendulum builds for the use of newstate can be found by
reading the definitions of these two functions. This is encouraged, of course, but one entry will be
particularly important during the lab: to get it, say
phaseplane = hlist{2};
Now phaseplane is the handle for the object of type “axes” in which the monitoring figure shows
the (θ, ω)-phase plane. By giving the command axes(phaseplane), we can activate that subplot in
the pendulum figure to receive plotting commands. This will be the key to drawing a velocity field
during the lab period.
Every time we call newpendulum, we get a new figure window and a new cell array for the objects
in that window. So, by carefully giving distinctive names to each list of handles we receive, we can
easily have two or more pendulum windows open and accessible at the same time.
Matlab has a whole system for preparing stand-alone animations that can be shown without
recomputing each video frame in real time. This is nice to know, but not applicable in today’s lab.
File “todo6”, version of 24 November 2014, page 7. Typeset at 12:21 November 24, 2014.
8 MECH 221 Computer Lab 6: The Simple Pendulum
nice smooth circles they really are, even though the shapes we are actually drawing are
many-sided polygons.
⊔ Adapt your replacement for pendtemplate so that it will trace several different phase-plane
⊓
trajectories, corresponding to various choices for the parameter C. Suggested values, in
Matlab notation: C = 0.5:0.5:5.
With these enhancements in place, your script should produce a figure that contains useful
information even when frozen in time by printing it. Make sure the title mentions your name
and print a copy to hand in.
3. Get the script threeplots.m from Connect. As provided, this makes three separate figures,
each showing the graph of θlin (t) in the top pane and the graph of ωlin (t) in the bottom pane.
Except for the amplitudes, these figures are identical.
Modify the given script so that it uses ode45 to solve the nonlinear pendulum equations (∗∗)
and determine the true values of θ(t) and ω(t) at the time instants t already being used to
plot the linear graphs. Then, have the script overlay the true θ(t) and ω(t) plots on top of the
approximate graphs as provided. Draw the true curves in fat lines with a contrasting colour.
Your work should produce three plots to hand in, showing the relationship between the true
motion and its linear approximation for each set of initial conditions listed below:
(i) θ(0) = 1 rad, ω(0) = 0 rad/s
(ii) θ(0) = 2 rad, ω(0) = 0 rad/s
(iii) θ(0) = 3 rad, ω(0) = 0 rad/s
4. Use your skills from Activity 3 to produce an accurate alternative to the phase plane sketch
from Activity 2. In detail, make a fresh copy of your script from Activity 2, and improve it as
follows.
⊔ Change the velocity field from the approximate velocities in (‡) to the true velocities
⊓
in (∗∗).
⊔ Use pre-computed values of θ(t) and ω(t) from ode45 to control the animation, instead of
⊓
simple trigonometric formulas.
⊔ Make plenty of computed trajectories. Start some with ω(0) = 0 and θ(0) from the
⊓
Matlab-style vector 0.5:0.5:3.0. Then start some others with θ(0) = −π and ω(0) chosen
from 0.5:0.5:3.0. Then think carefully about what changes to make, if any, to fill the
whole window with representative trajectories.
Once you have a good phase portrait of the nonlinear pendulum, showing plenty of represen-
tative trajectories in every region, print it and hand it in.
File “todo6”, version of 24 November 2014, page 9. Typeset at 12:21 November 24, 2014.