Anda di halaman 1dari 8

2 MOTION OF A SIMPLE PENDULUM

This practical introduces the following:

• The equation of motion of a simple pendulum.


• Numerical solution of differential equations using the Runge-Kutta method.
• Writing output data to a file in C programming.
• Using GNUPLOT to create graphs from datafiles.

2.1 The Simple Pendulum

s
L tangent
θ
mg

The equation of motion (Newton's second law) for the pendulum is

d 2s d 2θ
= L = − g sin θ (1)
dt 2 dt 2

where the bob moves on the arc of a circle of radius L, s is parallel to the tangent to this arc
when the angular displacement of the bob from its equilibrium position is θ, and −g sin θ is
the component of the acceleration due to gravity in the s direction. (N.B. The positive s
direction is chosen to be away from the equilibrium position, in the direction which makes θ
increase.)

For small angles sin θ ≈ θ , and equation (1) may be approximated by

d 2θ g
2 = − θ (2)
dt L

This is a linearised version of equation (1), since the right hand side is linear in θ whereas
sinθ in equation (1) is nonlinear. Equation (2) can be solved analytically, giving the usual
periodic solution:
dθ g
θ = a sin( β t + φ ), = aω cos( β t + φ ), β= (3)
dt L

where a is the amplitude and φ is the (constant) initial phase.

To solve a second order differential equation numerically we introduce a new variable and
transform the second order problem into two first order problems. Introduce the variable
ω=dθ/dt which is the angular velocity of the bob, and write

1
dθ dω
=ω , = f (θ , ω , t ) (4)
dt dt

where f(ω, θ, t) = −β2 sin θ in the case of the pendulum equation (1), and f(ω, θ, t) = −β2θ in
the case of the linearised equation (2). In the case of a damped pendulum, with damping
force -mkω, the function f depends on ω as well as θ. If the pendulum is also forced by an
external time-dependent force mA cos(Ωt) the function on the right hand side of (4) becomes
a function of the three variables θ , ω and t:

f (θ , ω , t ) = − β 2 sin θ − kω + A cos(Ωt ) (5)

To solve these equations numerically we need to choose initial values for θ and ω and then
calculate a series of values for (θn, ωn), n = 1, 2, 3, … We step forward in time using Taylor
series expansions for θ and ω.

dθ (t ) d 2θ (t ) Δt 2
θ (t + Δt ) = θ (t ) + Δt + 2
+ O(Δt ) 3
dt dt 2!
Δt 2
θ (t + Δt ) = θ (t ) + ω (t )Δt + f (θ (t ), ω (t ), t ) + O(Δt ) 3
2!
θ n +1 = θ n + ω n Δt + O(Δt ) 2
ω n +1 = ω n − θ n Δt + O(Δt ) 2

This is the simple Euler method for integrating the simple pendulum differential equations.
The notation O(Δt)2 means that there are error terms of order Δt squared and higher. The
equation for updating the angular frequency is obtained using a Taylor expansion in the same
way that the equation for updating the angle was obtained.

The trapezoid rule makes improved approximations for updating the angular frequency and
angle.

Trapezoid

Simple
Euler

dθ (t ) dθ (t + Δt )
dt dt

t t+Δt

dθ (t ) dθ (t + Δt )
+ t + Δt

Area under trapezoid = (t + Δt − t ) dt dt ≅ ∫ dt = θ n +1 − θ n
2 t
dt

2
Δt ⎛ dθ (t ) dθ (t + Δt ) ⎞
θ n +1 ≅ θ n + ⎜ + ⎟
2 ⎝ dt dt ⎠
dθ (t + Δt ) dθ (t ) d 2θ (t )
= + 2
Δt + O(Δt ) 2
dt dt dt
dθ (t + Δt )
≅ ω n + f (θ n , ω n , t )Δt = ω n + k1b
dt
Δt k1a + k 2a
θ n +1 ≅ θ n + (ω n + (ω n + f (θ n , ω n , t ) )Δt ) = θ n +
2 2
k1a = ω n Δt k2a = (ω n + f (θ n , ω n , t )Δt )Δt
Δt ⎛ dω (t ) dω (t + Δt ) ⎞
ω n +1 ≅ ω n + ⎜ + ⎟
2 ⎝ dt dt ⎠
Δt k1b + k 2b
ω n +1 ≅ ω n + ( f (θ n , ω n , t ) + f (θ n +1 , ω n + k1b, t n +1 ) ) = ω n +
2 2
k 2b = Δt f (θ n +1 , ω n + k1b, t n +1 )

2.2 A program to solve the pendulum equation


The program pendulum.c (provided electronically and on page 4) solves the pair of
equations (4) using the Trapezoid rule:

• The program calculates the angle θ and angular velocity ω at n+1 instants of time, starting
from t=0 and increasing in steps of equal size h until t=nh. The output is a 3-column
table, with values of t in the first column, θ in the second and ω in the third. h is used
instead of Δt in the actual programme.
• The function f(ω, θ, t) is calculated in a separate C function, so that it is easy to alter as
necessary.
• At each step the integration of the two first order equations is performed using the
trapezoid rule. The variable k1a is h times the value of the derivative of θ at t, the start of
the step. k2a is h times the first order approximation to the derivative of θ at t+h. The
trapezoid rule then gives θ(t+h)= θ(t)+(k1a+k2a)/2. Similarly k1b and k2b are h times
the values for the derivative of ω at each end of the step, and the value of ω at t+h is
calculated using the trapezoid rule.
• To make it easy to show the results graphically, the output is written to a datafile as well
as to the screen. The datafile can then be used with GNUPLOT to show graphs of θ and
ω against t.

2.3 Writing to a file in C


Examine the program pendulum.c to see how data is sent to a file. As well as the statement
commanding the computer to write to the file, the file must be declared, opened before use
and closed after use. (Programming in C section 5). Each of these commands is discussed
below:

2.3.1 The fprintf statement

Compare the statements printf("%lf\t%lf\t%lf\n", t, theta, omega);


and fprintf(Oscp,"%lf\t%lf\t%lf\n", t, theta, omega);
The first will print on the screen

3
• a double precision floating point number taking the current value of t,
• a tab space,
• a double precision floating point number taking the current value of theta,
• another tab space,
• a double precision floating point number taking the value of omega,
• a carriage return (invisible of course).
The second command prints exactly the same thing to the file whose address in the computer
memory is Oscp.
In general, the command to print to a file is of the form:

fprintf(file address, "control string", other parameters) (6)

2.3.2 File declaration

Remember that when declaring variables their type (int, double, char etc.) must be stated. A
file is a new object of the FILE type. (Note the capitals). The computer must be told where in
its memory the file is saved, so the declaration contains not the file name, but its address. For
example, the declaration for the file with address Oscp is FILE *Oscp . The address is
preceded by an asterisk to indicate that this is a pointer to an area of memory rather than a
name. The declaration for a file is of the general form

FILE *filepointer (7)

(For further details on pointers see Programming in C section 8.)

2.3.3 File opening and closing


Before data can be written to a file it must be opened. The open file statement has to relate
the file name (as it appears when you look at the contents of the directory in which it resides)
with the address at which it is stored. The statement

Oscp=fopen("pendulum.dat", "w")

opens the file named pendulum.dat. The "w" means that this file is being opened so that it
may be written to. If you wanted to open a file and read from it you would have to use "r",
and if it was a file that already contained data and you wanted to append more data to it you
would use "a". If the file doesn't already exist, the open command creates it.

The file opening statement in pendulum.c is more complicated. Firstly it requests a filename
for the data file. (This makes it easy to ensure that each time you run the program you save
data to a different file). The name is read into the variable filedat as a string of characters.
The declaration of this variable is char filedat[20]; which identifies the input as a character
string of maximum length 20 characters. So in the file opening statement the variable filedat
appears instead of "filename". For use with GNUPLOT the datafile name should have the
extension .dat. (e.g name.dat ).

Secondly the program checks whether it is possible to open the file, and exits with an error
message if it is not. This is good programming style. The file opening statement is of the
following general form:

if ((*filepointer = fopen("filename", "w")) = = NULL)


{ printf("\nCannot open file\n");
exit(1); } (8)
To use the exit function you need to include the header file stdlib.h.

4
When you have finished using a file you must close it with a statement of the form

fclose(filepointer); (9)

/* pendulum.c Solution of the pendulum equation using improved Euler


method */
# include <stdio.h>
# include <stdlib.h> /* needed for error function */
# include <math.h>
double f(double theta, double omega, double t); /* function prototype */
main()
{
int i, n;
double h, k1a, k1b, k2a, k2b, theta, omega, t;
FILE *Oscp; /* declare file */
char filedat[20]; /* declare string of characters for filename */

h=0.1; /* set time step */


n=100; /* set number of steps */
theta=0; /* set initial angle */
omega=1; /* set initial angular velocity */
t=0; /* set initial time */
/* Enter datafile name, and open it for writing */
printf("Enter the name of the data file to be written\n");
scanf("%s", filedat);
if ((Oscp = fopen(filedat,"w")) == NULL)
{
fprintf(stderr,"Error opening file %s",filedat);
exit(1);
}
printf("%lf\t%lf\t%lf\n", t, theta, omega);
fprintf(Oscp,"%lf\t%lf\t%lf\n", t, theta, omega);
/* Implement the algorithm: */
for(i=1;i<=n;i++){
k1a=h*omega;
k1b=h*f(theta,omega,t);
k2a=h*(omega+k1b);
k2b=h*f(theta+k1a,omega+k1b,t+h);
theta = theta + (k1a + k2a)/2;
omega = omega + (k1b+k2b)/2;
t=t+h;

printf("%lf\t%lf\t%lf\n", t, theta, omega);


fprintf(Oscp,"%lf\t%lf\t%lf\n", t, theta, omega);
}
fclose(Oscp); /* close file */
}
double f(double theta, double omega, double t) /* function header */
{
double deriv;
deriv = -theta;

5
return deriv;
}

EXERCISES 1

Investigate the motion of a simple pendulum in the particular cases listed below. In each case
write the data to a datafile, and use GNUPLOT to plot graphs of the data.

To plot a datafile, open GNUPLOT. Type plot at the prompt, and pull down the PLOT menu
from the top bar. Click on Data filename … and a dialog box will allow you to find any data
files you have saved. Click on the file you want and its name will appear after plot. Now
type using 1:2 and press <RETURN>. GNUPLOT will use the first column of your datafile
for the horizontal axis, and the second column for the vertical axis. To plot the third column
instead of the second, replace 1:2 by 1:3. If you use GNUPLOT with a file pendulum.dat to
plot the angle and angular velocity as functions of time on the same graph, the command line
will be gnuplot> plot 'pendulum.dat' using 1:2, 'pendulum.dat' using 1:3

1. Run pendulum.c as it stands to solve the linearised equation with β2=1 and initial values
θ=0 and ω=1. Run the program again with the pendulum initially at rest and the
following set of initial positions: θ(0)= 0.2 rad, 1.0 rad and 3.124 rad. (The last value is
approximately equal to 179o). In each case plot graphs of θ and ω against t. Describe
what the pendulum is doing in each of these cases.

2. Replace the sine function so that the program solves the nonlinear equation (1) for the
same sets of initial values as in question 1. Compare the results with those of question 1.
To see how they differ as the displacement of the pendulum increases, plot the angles
calculated in this question and those found in question 1 as functions of time on the same
graph.

2.4 The Runge-Kutta method

The trapezoid method is accurate to 2nd order in timestep h, which is not sufficient for
integrating the equation for the damped driven pendulum, where the function for the
derivative of ω is given by equation (5). The second order Runge-Kutta method is derived
below and the 4th order Runge-Kutta algorithm is given for use in your programme. The
second order Runge-Kutta method is based on a Taylor series expansion of the function to be
integrated about the mid-point of the time step.

dθ (Δt )
dt
d θ ( 0) ⎛ Δt ⎞
dθ ⎜ ⎟
dt ⎝ 2⎠
dt

6
Δt Δt
dθ (
) d 2θ ( )
dθ (t ) 2 + 2 ⎛⎜ t − Δt ⎞⎟ + O( Δt ) 2
=
⎝ 2⎠
2
dt dt dt 2
Δt Δt Δt
Δt dθ ( ) d 2θ ( ) Δt dθ ( )
dθ (t ) ⎛ Δ t ⎞
∫0 dt dt ≅ dt Δt + dt 2 ∫0 ⎜⎝ t − 2 ⎟⎠ = dt Δt + 0
2 2 2

The second term in the series above is equal to zero when we choose the mid-point of the time
step for the Taylor expansion. To use the algorithm we need approximate values for θ and ω
at the mid-point of the time step which are generated using the Simple Euler method.

Δt 2
θ n +1 − θ n ≅ ω n +1 / 2 Δt + O( )
2
Δt Δt
ω n +1 / 2 ≅ ω n + f (θ n , ω n , t n ) + O( ) 2
2 2
⎛ Δt ⎞
θ n +1 ≅ θ n + ⎜ ω n + f (θ n , ω n , t n ) ⎟Δt
⎝ 2⎠
Δt 2
ω n +1 − ω n ≅ f (θ n +1 / 2 , ω n +1 / 2 , t n +1 / 2 )Δt + O( )
2
Δt
θ n +1 / 2 ≅ θ n + ω n
2
Δt
ω n +1 / 2 ≅ ω n + f (θ n , ω n , t n )
2
ω n +1 ≅ ω n + f (θ n +1 / 2 , ω n +1 / 2 , t n +1 / 2 )Δt

The algorithm for second order Runge-Kutta integration has been derived; formulae for the
4th order Runge-Kutta method are given below.

k1a = h ω k1b = h f(θ, ω, t)


k2a = h (ω+k1b/2) k2b = h f(θ+k1a/2, ω+k1b/2, t+h/2)
k3a = h (ω+k2b/2) k3b = h f(θ+k2a/2, ω+k2b/2, t+h/2)
k4a = h (ω+k3b) k4b = h f(θ+k3a, ω+k3b, t+h)

θ(t+h) = θ(t) + (k1a+ 2 k2a + 2 k3a + k4a)/6


ω(t+h) = ω(t) + (k1b+ 2 k2b + 2 k3b + k4b)/6

EXERCISES 2
1. Edit pendulum.c so that it uses the 4th order Runge-Kutta algorithm in the for loop.

2. Check your program by running it for the case of the nonlinear equation (1), with
several different initial values of θ and ω, and comparing the results with those
obtained in question 2 of exercise 1.

7
3. Examine the behaviour of a damped pendulum, equation (5), using β2=1, k = 0.5, A=0
and a range of initial positions and velocities. Plot graphs of θ against t to compare
the nonlinear case with the linearised version. Your graphs should show that the
linear approximation becomes very good after a sufficiently long time. Explain why.

4. Change the function in your program to solve the equation for the damped driven
pendulum, using β2=1, k = 0.5, Ω = 0.6667, and a range of drive amplitudes:
A = 0.90, 1.07, 1.35, 1.47 and 1.5. The best way to understand the behaviour of the
pendulum is to plot graphs of ω against θ, and restrict θ to the range [-π, π]:
• To restrict the range of θ, add the following if statement in the for loop where
theta is calculated:

if (fabs(theta)>PI)
{
theta = theta - 2*PI*fabs(theta)/theta;
}

• To use PI instead of having to type in the numerical value, define it at the very
top of your program with the statement # define PI 3.14159265 (N.B. no
semicolon at the end).
• It is convenient to divide the 'drive period' 2π/Ω into an integer number N of
steps (e.g. N=1000), and calculate values of θ and ω at each of these times over a
small number of drive cycles (e.g. 5).
• Approximately the first 500 iteration points represent an initial transient
trajectory before the motion settles down. Do not plot the first 500 points
(include a statement in your program to prevent it writing the initial 500 points to
the datafile).

Plot ω against θ for initial positions and velocities in the ranges -3.0<θ(0)<-1.0,
-0.5<ω(0)<0.5. For A = 0.90 you should be able to see that the motion is periodic,
while for A = 1.07 it is a combination of oscillations at two different periods - period
doubling has occurred. For A = 1.5 the motion is chaotic.

Anda mungkin juga menyukai