pendulum

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 θ mg s tangent L The equation of motion (Newton's second law) for the pendulum is ds dt L d dt g 2 2 2 2 = =− θ θ sin (1) 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 dt g L 2 2 θ θ =− (2) 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: θ β φ θ ω β φ β = + = + = a t d dt a t g L sin( ), cos( ), (3) 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

Upload: lulu-tojeen

Post on 17-Sep-2015

1 views

Category:

Documents


0 download

DESCRIPTION

Pendulum program in c explanation

TRANSCRIPT

  • 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

    mg

    stangentL

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

    d sdt

    Lddt

    g2

    2

    2

    2= = sin (1) 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

    ddt

    gL

    2

    2

    = (2) 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:

    = + = + =a t ddt

    a tgL

    sin( ), cos( ), (3)

    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

  • ddt = , d

    dtf t

    = ( , , ) (4)

    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: (5) f t k A t( , , ) sin cos( ) = +2 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 .

    21

    21

    32

    32

    2

    2

    )(

    )(

    )(!2

    )),(),(()()()(

    )(!2

    )()()()(

    tOttOt

    tOttttfttttt

    tOtdt

    tdtdt

    tdttt

    nnn

    nnn

    +=++=

    +++=+

    +++=+

    +

    +

    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.

    Area under trapezoid = ( ) nntt

    t

    dtdtddt

    ttddt

    td

    ttt

    =++

    + ++ 12

    )()(

    dtttd )( +

    dttd )(

    Simple Euler

    Trapezoid

    t t+t

    2

  • ( )( )( )

    ( )),1,( 2

    221),1,(),,(

    2

    )()(2

    ),,(k2a 12

    21),,(2

    1),,()(

    )()()()(

    )()(2

    11

    111

    1

    1

    22

    2

    1

    ++

    +++

    +

    +

    +

    +=

    ++=+++

    ++++==

    ++=+++

    +=++++=+

    +++

    nnn

    nnnnnnnn

    nn

    nnnn

    nnnnnnn

    nnnn

    nn

    tbkftbk

    bkbktbkftftdt

    ttddt

    tdttttftak

    akakttft

    bkttfdt

    ttd

    tOtdt

    tddt

    tddt

    ttddt

    ttddt

    tdt

    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 # include /* needed for error function */ # include 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

  • 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 . 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.

    dt

    td

    2

    ( )dt

    td

    dtd )0(

    6

  • 0)

    2(

    2

    )2

    ()2

    ()(

    )2

    (2

    )2

    ()2

    ()(

    02

    2

    0

    22

    2

    +

    =

    +

    +

    +

    =

    tdttdtt

    dt

    tdt

    dt

    tddt

    dttd

    tOttdt

    td

    dt

    td

    dttd

    tt

    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.

    ttf

    ttf

    t

    tOttf

    tttf

    tOttf

    tOt

    nnnnn

    nnnnn

    nnn

    nnnnn

    nnnnnn

    nnnnn

    nnn

    +

    +

    +

    +

    ++

    ++

    +

    ++++

    +

    +

    ++++

    +

    +

    ++

    ),,(2

    ),,(

    2

    )2

    (),,(

    2),,(

    )2

    (2

    ),,(

    )2

    (

    2/12/12/11

    2/1

    2/1

    22/12/12/11

    1

    22/1

    22/11

    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

  • 8

    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