Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
PHY321F — cp 2005 24<br />
<strong>Topic</strong> 2:<br />
<strong>The</strong> <strong>pendulum</strong><br />
Here we enlarge upon the simple solution of a differential equation in <strong>Topic</strong> 1,<br />
and apply it to a simple mechanical system. This solution, of course, depends<br />
on knowledge of an initial position and velocity: it is an initial value problem.<br />
2.1 Description of problem<br />
<strong>The</strong> simple <strong>pendulum</strong> is usually treated in first-year textbooks: the equation<br />
of motion of a <strong>pendulum</strong> of length L (with gravitational acceleration g) is<br />
d 2 θ<br />
dt 2 = −αθ = − g L θ<br />
where θ is the angular position and α is the angular acceleration.<br />
<strong>The</strong> well-known solution is<br />
where Ω = √ g/L.<br />
θ i = θ m cos(Ω t + φ)<br />
A real <strong>pendulum</strong> with finite amplitude is better represented as the mathematical<br />
<strong>pendulum</strong><br />
d 2 θ<br />
dt + 2 Ω2 sin θ = 0<br />
Now the solution is only found in terms of elliptic integrals, with the period<br />
T being<br />
where<br />
T = 4<br />
K(x) =<br />
√<br />
∫ π/2<br />
0<br />
L<br />
g K(sin θ m<br />
2 )<br />
dz<br />
√<br />
1 − x2 sin 2 z<br />
is the complete elliptic integral of the first kind.<br />
<strong>The</strong> angular position is<br />
θ = 2 arcsin (k sn(Ω t + φ, k))<br />
where k = sin(θ m /2) and sn(u, k) is a Jacobian elliptic function.
PHY321F — cp 2005 25<br />
<strong>The</strong> problem of determining the position as a function of time, or the period,<br />
is now a matter of finding the value of some elliptic integrals. As there is<br />
no ‘calculator key’ for these, we either have to interpolate from tables or<br />
evaluate the integrals numerically. <strong>The</strong>se are both computational problems.<br />
We can, of course, also solve the differential equation on the computer.<br />
We can make the <strong>pendulum</strong> model more realistic by including damping. <strong>The</strong><br />
linearly damped harmonic oscillator and its solution are well-known:<br />
d 2 θ<br />
dt + 2 Ω2 θ + β dθ<br />
dt = 0<br />
where β = b/(mL) with b a damping parameter and m the bob mass. <strong>The</strong>n<br />
the solution is<br />
θ = θ m e − β 2 t cos(Ω t + φ)<br />
Of course we should really do this for the mathematical <strong>pendulum</strong> — then<br />
there is no analytical solution. In addition, the damping term is inappropriate<br />
for a <strong>pendulum</strong> bob moving at a typical speed in air. <strong>The</strong> damping force is<br />
better described by<br />
F d = 1 2 CρAv|v|<br />
(where ρ is the density of air, C is a drag coefficient, A an effective crosssectional<br />
area and v is the speed L ˙θ of the bob) rather than F d = bv. C can<br />
depend on the speed as well. This gives a drag term in the above equation<br />
C dθ<br />
ρAL<br />
2m dt<br />
dθ<br />
∣ dt ∣ = γ dθ<br />
dθ<br />
dt ∣ dt ∣<br />
Let us see how to solve this problem computationally.<br />
2.2 Numerical methods<br />
2.1 Solving the differential equation<br />
While there exist techniques for integrating second-order differential equations,<br />
it is more convenient to exploit first-order integrators. We start by<br />
writing the basic equation (without damping) we want to solve as a system
PHY321F — cp 2005 26<br />
of two coupled first order equations:<br />
dω<br />
dt = −Ω2 sin θ (5)<br />
dθ<br />
dt = ω (6)<br />
where ω is the angular velocity and θ is the angular position.<br />
<strong>The</strong>se equations can now be integrated using the Euler method.<br />
Suppose we know the position and velocity at a time t = i∆t. <strong>The</strong>n the<br />
above pair of coupled equations can be rewritten as<br />
ω i+1 = ω i − ∆t Ω 2 sin θ i (7)<br />
θ i+1 = θ i + ∆t ω i (8)<br />
(Note that this method can obviously be generalised to solve an arbitrary<br />
system of equations<br />
dy i<br />
dt = f i(y 1 , y 2 , . . . , y N , t)<br />
i = 1, . . . , N<br />
which can be written in a vector notation<br />
dy<br />
dt<br />
= f(y, t)<br />
This is the equation typically solved by a real ODE integrator.)<br />
Write a program (see example) to solve equations (3) and (4), and check that<br />
the result for small initial angular displacement is what you expect.<br />
This solution suffers from a defect: the computed solution does not conserve<br />
the energy E = 1 2 mL2 ω 2 +mgL(1−cos θ). Change your program to calculate<br />
the energy and show this.<br />
A slight modification, however, gives a system which does conserve energy.<br />
ω i+1 = ω i − ∆t Ω 2 sin θ i (9)<br />
θ i+1 = θ i + ∆t ω i+1 (10)<br />
This Euler-Cromer method is a first order area-preserving mapping. Such<br />
mappings exploit the symplectic structure of Hamilton’s equations — they<br />
preserve the canonical structure of these equations.
PHY321F — cp 2005 27<br />
However, this cannot be so easily generalised. <strong>The</strong> Euler method is an example<br />
of an explicit method. <strong>The</strong> new value can be determined by stepping<br />
from the old value. By contrast, the Euler-Cromer method is implicit — the<br />
expression for the new value contains the new value, and thus constitutes<br />
a system of equations to be solved for that value, rather than a simple assignment.<br />
This solution is trivial in the Euler-Cromer case, but is not so in<br />
general.<br />
Including the air drag term in, say, the Euler solution gives:<br />
ω i+1 = ω i − ∆t(Ω 2 sin θ i + γ ω i |ω i |) (11)<br />
θ i+1 = θ i + ∆t ω i (12)<br />
2.3 Computation<br />
Here is a simple Python program using the Euler method.<br />
from Numeric import *<br />
g=9.81 # m/s**2<br />
l=1.00 # length in m<br />
Nsteps = 100<br />
w0sq = g/l<br />
dt=0.1<br />
vn=0.0<br />
xn=30.0*pi/180.0<br />
# time step<br />
# initial speed zero at extreme posn.<br />
# initial angular position 30 degrees<br />
for i in range(Nsteps+1):<br />
print i, xn, vn<br />
vnp1 = vn - w0sq*sin(xn)*dt<br />
xnp1 = xn + vn*dt # Euler<br />
xn = xnp1<br />
vn = vnp1
PHY321F — cp 2005 28<br />
2.4 Higer-order methods<br />
Better accuracy is achieved in a single time-step by using a higher-order<br />
integration method. <strong>The</strong>se typically use more derivative information to gain<br />
accuracy.<br />
A simple example is (velocity) Verlet method. (<strong>The</strong>re are other Verlet methods<br />
equivalent mathematically although not necessarily numerically).<br />
θ i+1 = θ i + ω i ∆t + 1 2 a i (∆t) 2<br />
ω i+1 = ω i + 1 2 (a i + a i+1 ) ∆t<br />
This method also has the advantage of preserving energy conservation.<br />
Aother second order method is the Euler-Richardson method which uses the<br />
slope in the middle of the time-step for extrapolation to the next point. This<br />
results in O(∆t) 2 accuracy, at the expense of a second force evaluation.<br />
a i = α(θ i , ω i , t i )<br />
ω mid = ω i + a i<br />
∆t<br />
2<br />
θ mid = θ i + ω i<br />
∆t<br />
2<br />
a mid = α(θ mid , ω mid , t i + ∆t<br />
2 )<br />
ω i+1 = ω i + a mid ∆t<br />
θ i+1 = θ i + ω mid ∆t<br />
This is essentially a second order Runge-Kutta method and can be derived<br />
in several ways. It can be combined with the standard Euler step to obtain<br />
information on the truncation error. With this the method can easily be<br />
extended to offer adaptive control: the step-size can be varied to maintain<br />
an error limit.<br />
2.5 Exercises<br />
1. Compare the Euler and Euler-Cromer methods of integration.<br />
2. Determine the dependence of the frequency of the <strong>pendulum</strong> on its<br />
amplitude. One possible means of analysis is to determine the power<br />
spectrum of the oscillations from the Fourier transform of the solution.
PHY321F — cp 2005 29<br />
You will need to compute the solution as an array of values and take<br />
the FFT of the result.<br />
3. Include the drag force term in your solution. Assume the <strong>pendulum</strong><br />
consists of a 1.0 cm diameter steel ball bearing suspended 1.0 m from<br />
the pivot, and is started from an angle of 40.0 ◦ . How long will it take<br />
for the amplitude to decrease to 5.0 ◦ ? (See e.g. Halliday and Resnick<br />
for drag coefficients and air density). Interestingly, the Euler solution<br />
might be better than Euler-Cromer for this case.<br />
4. Program the case of linear damping. Compare the decrease in energy<br />
of the <strong>pendulum</strong> using the Euler and Euler-Cromer methods. How do<br />
they agree with the analytical solution?<br />
2.6 Literature<br />
1. Stable solutions using the Euler approximation. A. Cromer, Am. J.<br />
Phys. 49 455-459 (1981).<br />
2. <strong>The</strong> <strong>pendulum</strong> — rich physics from a simple system. R.A. Nelson and<br />
M.G. Olsson, Am. J. Phys. 54 112-121 (1986).<br />
3. Numerical integration of Newton’s equations including velocity-dependent<br />
forces. I.R. Gatland, Am. J. Phys. 62 259-265 (1994).
PHY321F — cp 2005 30<br />
D<br />
A digression: Ordinary differential equations<br />
D.1 Explicit and implicit methods<br />
Consider an equation of the form<br />
dy(x)<br />
dx<br />
<strong>The</strong> explicit Euler-method solution is<br />
= −λ y(x)<br />
y(x + h) = (1 − h λ)y(x)<br />
(where we use now a generic variable x and stepsize h).<br />
This solution is unstable if h > 2/λ. We can see this by considering the error<br />
e(x), i.e. the difference between the true solution ŷ(x) and the computed<br />
solution y(x), <strong>The</strong>n<br />
e(x + h) ≈ e(x)(1 − h λ)<br />
This error term grows if |1 − h λ| > 1.<br />
This can be compared to an implicit scheme<br />
y(x + h) = y(x) − h λ y(x + h)<br />
= y(x)<br />
1 + h λ<br />
Note that we have to solve an equation in order to obtain y(x + h). In this<br />
case, the solution is trivial, but in more complicated cases, the solution could<br />
involve substantial numerical effort.<br />
However, the stability is now assured as<br />
e(x + h) ≈<br />
e(x)<br />
1 + h λ<br />
so that the error term decreases for all h > 0.<br />
Implicit equations are important in the solution of stiff systems, where a<br />
rapidly decaying part of the solution dominates the stability.
PHY321F — cp 2005 31<br />
D.2 Runge-Kutta methods<br />
<strong>The</strong> simple methods we have considered are only correct to first order in h.<br />
Obviously, higher order methods can be expected to offer benefits (although<br />
this might not extend to accuracy).<br />
One way of generating such methods leads to a class of Runge-Kutta methods.<br />
By way of example, suppose we solve a system<br />
by the Euler method to get<br />
dy(x)<br />
dx<br />
= f(x, y)<br />
y(x + h) = y(x) + h f(x, y)<br />
<strong>The</strong> derivative over the interval of size h is taken from the initial point. A<br />
better value, perhaps, would be to take the value at the centre of the interval.<br />
<strong>The</strong> value of y at the centre of the interval can be estimated from the above<br />
Euler step This then gives a solution<br />
k 1 = h f(x, y(x))<br />
k 2 = h f(x + h 2 , y(x) + 1 2 k 1)<br />
y(x + h) = y(x) + k 2<br />
This method turns out to be second order, so that the error term is O(h 3 )<br />
This process can be continued: the classic Runge-Kutta scheme is the fourth<br />
order formula<br />
k 1 = h f(x, y(x))<br />
k 2 = h f(x + h 2 , y(x) + 1 2 k 1)<br />
k 3 = h f(x + h 2 , y(x) + 1 2 k 2)<br />
k 4 = h f(x + h 2 , y(x) + k 3)<br />
y(x + h) = y(x) + k 1<br />
6 + k 2<br />
3 + k 3<br />
3 + k 4<br />
6<br />
This is easily programmed, and thus is often used as a workhorse fixedstepsize<br />
integration formula.
PHY321F — cp 2005 32<br />
More control over the process is achieved if the error can be monitored. Suppose<br />
we have two Runge-Kutta formulae, with solutions y(x + h) of order<br />
h n+1 and ŷ(x + h) of order h n , <strong>The</strong>n the difference y − ŷ gives an estimate<br />
of the error in y. This is especially useful if we can obtain both y and ŷ<br />
from the same set of Runge-Kutta steps. (Such formulae are known as embedded<br />
Runge-Kutta formulae; the additional work does not take additional<br />
expensive evaluations of the function f).<br />
Once we have an estimate of the error, it can be used to control the stepsize<br />
so that some maximum error per step is not exceeded. This leads to a set<br />
of adaptive Runge-Kutta methods. A modern adaptive RK code is rksuite<br />
(or rksuite90), available from netlib.<br />
D.3 Other methods<br />
D.3.1<br />
Predictor-corrector methods<br />
<strong>The</strong> Euler and Runge-Kutta methods extrapolate from one point to the next.<br />
Higher accuracy can be obtained by using several previous steps. <strong>The</strong>se multistep<br />
methods are usually implemented as predictor-corrector schemes. An<br />
extrapolation is made to the next point using an explicit multistep formula.<br />
<strong>The</strong> value obtained is then used in an implicit multistep formula to correct<br />
this prediction. This scheme also permits error control by adaptive step<br />
sizing. A standard PC code is vode (and related codes), available from<br />
netlib.<br />
<strong>The</strong>se methods give high accuracy integrators, but are more complicated and<br />
fussy to program than RK.<br />
D.3.2<br />
Extrapolation methods<br />
Burlisch-Stoer methods (see, e.g. Numerical Recipes) use Richardson extrapolation<br />
to the limit h → 0 in order to improve accuracy. Press. et. al seem to<br />
think that these methods are about to replace PC methods (if they haven’t<br />
already). Others disagree.
PHY321F — cp 2005 33<br />
D.3.3<br />
Symplectic methods<br />
<strong>The</strong>se are special methods for integrating Hamilton’s equations of motion.<br />
<strong>The</strong>y preserve the structure of these equations (or at least something close<br />
by) and are much in vogue in the study of dynamical systems (e.g. chaos),<br />
tracking codes for charged particle optics, etc. <strong>The</strong> Euler-Cromer method is<br />
a simple example.
PHY321F — cp 2005 34<br />
E<br />
Fourier transforms<br />
Fourier analysis forms the foundation of many powerful computational techniques.<br />
This section is only a bare introduction.<br />
E.1 Fourier series<br />
Any periodic function f(t) = f(t + T ) can be expanded in the Fourier series:<br />
∞∑<br />
f(t) = c n e inωt<br />
n=−∞<br />
where ω = 2π/T .<br />
<strong>The</strong> Fourier coefficients c n are obtained from<br />
c n = 1 T<br />
∫ T/2<br />
−T/2<br />
f(t) e −inωt dt<br />
E.2 Fourier transform<br />
Under fairly general conditions function f(t) can be expressed as a Fourier<br />
transform:<br />
f(t) = √ 1 ∫ ∞<br />
F (ω) e iωt dω<br />
2π<br />
where<br />
−∞<br />
F (ω) = 1 √<br />
2π<br />
∫ ∞<br />
−∞<br />
f(t) e −iωt d t<br />
This may be written F (ω) = F[f(t)] and f(t) = F −1 [F (ω)]<br />
One speaks of transforming between the time and frequency domains.<br />
E.3 Discrete Fourier transform<br />
Let ∆ω = 2π/T .
PHY321F — cp 2005 35<br />
<strong>The</strong> Fourier transform can be approximated by a discrete Fourier transform<br />
(DFT). <strong>The</strong> time or frequency axes are replaced by a finite set of N points<br />
on a grid and the integral performed using the trapezoidal rule:<br />
F (n∆ω) ≈<br />
=<br />
N−1<br />
∑<br />
m=0<br />
N−1<br />
∑<br />
m=0<br />
f(m∆t) e −in∆ωm∆t<br />
f(m∆t) e −i2πnm/N<br />
<strong>The</strong> inverse transform is then<br />
f(m∆t) = 1 N<br />
N−1<br />
∑<br />
n=0<br />
F (n∆ω) e i2πmn/N<br />
Aliasing occurs for frequencies higher than the Nyquist frequency ω ny =<br />
N∆ω/2 = (2π/∆t)/2<br />
E.4 <strong>The</strong> fast Fourier transform<br />
<strong>The</strong> importance of Fourier methods in computation derives from the existence<br />
of the fast Fourier transform (FFT) algorithm, which is, well, a fast<br />
way of computing the DFT. (What does fast mean? <strong>The</strong> DFT requires<br />
O(N 2 ) calculations to compute the transform of N values; the FFT requires<br />
O(N log N) — when N is of the order of 10 3 , a typical value, the FFT is<br />
about 1000 times faster than the DFT. . . )<br />
<strong>The</strong> FFT is a non-trivial algorithm and we won’t discuss it here — we’ll just<br />
make use of a packaged version.<br />
E.5 <strong>The</strong> fast Fourier transform in Python<br />
In Python we can use:<br />
from Numeric import *<br />
from FFT import * # In windows : from Fft
PHY321F — cp 2005 36<br />
...<br />
F=fft(f) # f is a Numeric array<br />
g=inverse_fft(F)<br />
Note that F and g will be complex.<br />
Application of the FFT is complicated slightly by the conventions used to<br />
structure the data (Note that these are not peculiar to Python).<br />
Suppose that the initial data f(t) is in the time domain, with N points at<br />
a spacing δt, with T = Nδt. <strong>The</strong> transformed data F (ω) is (a) complex,<br />
and (b) runs over the range −(N/2)δω . . . (N/2)δω, with δω = 2π/T . In<br />
addition, the values coresponding to positive frequencies are returned in the<br />
first half of the array, while those at negative frequencies in the second half of<br />
the array. If the initial data has negative times, the same convention should<br />
be applied.<br />
E.6 Uses of the FFT<br />
<strong>The</strong>re are a number of applications of the FFT, based on various theorems<br />
on Fourier transforms.<br />
E.6.1<br />
Power spectrum<br />
<strong>The</strong> power spectrum gives essentially the intensity of the time-domain data<br />
as a function of frequency.<br />
It is defined by<br />
and may be computed by<br />
P (ω) = |F (ω)| 2<br />
transform=fft(data) # transform is complex<br />
powerspectrum=abs(transform*conjugate(transform))/len(data)**2<br />
A single frequency in the data will result in a delta function in the power<br />
spectrum. However, the FFT is always applied to a finite domain, and this<br />
causes ‘power’ to leak to adjacent frequencies. Hence the delta function is
PHY321F — cp 2005 37<br />
spread into a peak of finite width. <strong>The</strong> spread can be shaped by ‘windowing’<br />
the data. See, e.g. Numerical Recipes for further information.<br />
E.6.2<br />
Convolution<br />
<strong>The</strong> convolution f ∗ g of f(t) and g(t) is defined by<br />
f ∗ g =<br />
∫ ∞<br />
−∞<br />
f(τ) g(t − τ)dτ<br />
By the convolution theorem,<br />
F[f ∗ g] = F[f]F[g]<br />
This provides a fast convolution algorithm via the FFT.<br />
E.6.3<br />
Correlation<br />
<strong>The</strong> correlation corr(f, g) of f(t) and g(t) is defined by<br />
corr(f, g) = 1 √<br />
2π<br />
∫ ∞<br />
−∞<br />
f ∗ (τ) g(t + τ)dτ<br />
From the Fourier transforms F (ω) = F[f(t)] and G(ω) = F[g(t)],<br />
F[corr(f, g)] = F ∗ (ω)G(ω)<br />
<strong>The</strong> Wiener-Khinchine theorem relates the autocorrelation of a function to<br />
the power spectrum:<br />
F[corr(f, f)] = |F (ω)| 2