2nd Year Computational Physics Week 4 (standard): Ordinary differential equations

Similar documents
The Nonlinear Pendulum

Physics 200 Lecture 4. Integration. Lecture 4. Physics 200 Laboratory

Euler s Method, cont d

Consistency and Convergence

Numerical integration for solving differential equations

Lecture 19: Calculus of Variations II - Lagrangian

Computational Physics (6810): Session 8

Numerical Methods for Initial Value Problems; Harmonic Oscillators

the EL equation for the x coordinate is easily seen to be (exercise)

Numerical Methods for Initial Value Problems; Harmonic Oscillators

Fourth Order RK-Method

Physics 584 Computational Methods

Updated 2013 (Mathematica Version) M1.1. Lab M1: The Simple Pendulum

Ph 22.1 Return of the ODEs: higher-order methods

You may not use your books, notes; calculators are highly recommended.

Numerical Differential Equations: IVP

Chaotic motion. Phys 420/580 Lecture 10

Chapter 6 - Ordinary Differential Equations

INTRODUCTION TO COMPUTER METHODS FOR O.D.E.

Review Higher Order methods Multistep methods Summary HIGHER ORDER METHODS. P.V. Johnson. School of Mathematics. Semester

The Nonlinear Pendulum

Physics 6010, Fall Relevant Sections in Text: Introduction

Project 3: Pendulum. Physics 2300 Spring 2018 Lab partner

Second quantization: where quantization and particles come from?

P321(b), Assignement 1

Initial value problems for ordinary differential equations

2tdt 1 y = t2 + C y = which implies C = 1 and the solution is y = 1

Oscillatory Motion. Simple pendulum: linear Hooke s Law restoring force for small angular deviations. small angle approximation. Oscillatory solution

Theoretical physics. Deterministic chaos in classical physics. Martin Scholtz

Oscillatory Motion. Simple pendulum: linear Hooke s Law restoring force for small angular deviations. Oscillatory solution

PH 120 Project # 2: Pendulum and chaos

Numerical Methods for Differential Equations

Lecture V : Oscillatory motion and spectral analysis

Do not turn over until you are told to do so by the Invigilator.

Numerical Methods - Initial Value Problems for ODEs

Applied Math for Engineers

Simulation of Simple Pendulum

Computational project: Modelling a simple quadrupole mass spectrometer

Ordinary Differential Equations

2 Canonical quantization

Chapter 11 ORDINARY DIFFERENTIAL EQUATIONS

Modeling and Experimentation: Compound Pendulum

Numerical Analysis Exam with Solutions

Constrained motion and generalized coordinates

Introduction. Pre-Lab Questions: Physics 1CL PERIODIC MOTION - PART II Fall 2009

Nonlinear Oscillators: Free Response

Ordinary Differential Equations (ODEs)

Chaotic motion. Phys 750 Lecture 9

[ ] is a vector of size p.

Numerical solution of ODEs

Notes on numerical solution of differential equations

Introduction. Pre-Lab Questions: Physics 1CL PERIODIC MOTION - PART II Spring 2009

FORTRAN 77 Lesson 7. Reese Haywood Ayan Paul

8 Numerical Integration of Ordinary Differential

Math 128A Spring 2003 Week 12 Solutions

Introduction to standard and non-standard Numerical Methods

28. Pendulum phase portrait Draw the phase portrait for the pendulum (supported by an inextensible rod)

Lab M1: The Simple Pendulum

Ordinary Differential Equations

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University

MAS212 Assignment #2: The damped driven pendulum

AN OVERVIEW. Numerical Methods for ODE Initial Value Problems. 1. One-step methods (Taylor series, Runge-Kutta)

Parametric Resonance and Elastic Pendulums

M2A2 Problem Sheet 3 - Hamiltonian Mechanics

Lecture 13: Forces in the Lagrangian Approach

Chapter 6. Second order differential equations

Systems of Linear ODEs

Ordinary Differential Equations

ODE Runge-Kutta methods

PHYSICS 210 SOLUTION OF THE NONLINEAR PENDULUM EQUATION USING FDAS

Initial Value Problems

Example 2.1. Draw the points with polar coordinates: (i) (3, π) (ii) (2, π/4) (iii) (6, 2π/4) We illustrate all on the following graph:

Analytical Mechanics - Extra Problems

Chap. 20: Initial-Value Problems

for changing independent variables. Most simply for a function f(x) the Legendre transformation f(x) B(s) takes the form B(s) = xs f(x) with s = df

Basic Theory of Differential Equations

NUMERICAL SOLUTION OF ODE IVPs. Overview

Computer Problems for Taylor Series and Series Convergence

Ordinary differential equation II

Lab 10: Harmonic Motion and the Pendulum

Coordinate Curves for Trajectories

In the presence of viscous damping, a more generalized form of the Lagrange s equation of motion can be written as

Second Order ODEs. CSCC51H- Numerical Approx, Int and ODEs p.130/177

Figure 12.1: A simple pendulum

A Brief Introduction to Numerical Methods for Differential Equations

Assignments VIII and IX, PHYS 301 (Classical Mechanics) Spring 2014 Due 3/21/14 at start of class

Ordinary Differential equations

Classical Mechanics Comprehensive Exam Solution

Multistep Methods for IVPs. t 0 < t < T

Mon Jan Improved acceleration models: linear and quadratic drag forces. Announcements: Warm-up Exercise:

Higher Order Taylor Methods

Solution: (a) Before opening the parachute, the differential equation is given by: dv dt. = v. v(0) = 0

Euler s Method, Taylor Series Method, Runge Kutta Methods, Multi-Step Methods and Stability.

AIMS Exercise Set # 1

CS520: numerical ODEs (Ch.2)

Solution: (a) Before opening the parachute, the differential equation is given by: dv dt. = v. v(0) = 0

PreLab 2 - Simple Harmonic Motion: Pendulum (adapted from PASCO- PS-2826 Manual)

(f(x) P 3 (x)) dx. (a) The Lagrange formula for the error is given by

EXAMPLE OF ONE-STEP METHOD

Introductory Numerical Analysis

Transcription:

2nd Year Computational Physics Week 4 (standard): Ordinary differential equations Last compiled September 28, 2017 1

Contents 1 Introduction 4 2 Prelab Questions 4 3 Simple Differential Equations 5 3.1 Background............................... 5 3.1.1 Ordinary Differential Equations................ 5 3.1.2 Euler s Method......................... 5 3.1.3 Taylor Expansion Method................... 6 3.2 Procedure................................ 6 3.3 Example Code............................. 7 4 Coupled and Higher-Order Differential Equations 10 4.1 Background............................... 10 4.1.1 Coupled ODEs and Reduction of Order........... 10 4.1.2 Runge-Kutta Method..................... 10 4.1.3 Applying the Runge-Kutta Method With Reduction of Order for Coupled ODEs..................... 12 4.2 Procedure................................ 13 4.3 Example Code............................. 14 5 Bonus Section: The Transition from Order to Chaos: Plotting the Motion of a Double Pendulum 18 5.1 Background............................... 18 5.1.1 Chaotic Systems........................ 18 5.1.2 Describing Complex Systems: Lagrange s Equations and Generalised Coordinates.................... 18 2

5.1.3 Double Pendulum....................... 19 5.1.4 Initial Conditions and Energy Domains............ 22 5.2 Procedure (One Bonus Mark)..................... 22 5.3 Example Code............................. 23 3

1 Introduction Differential equations are at the heart of physics. They are used in almost all areas of physics, from classical equations of motion to thermodynamics to quantum mechanics. It cannot be stressed enough how important they are, or how difficult they can be to solve. Even seemingly simple systems like the double pendulum we will cover in this lab can lead to equations that are challenging to find analytic solutions for. In this lab we will learn some important numerical techniques for solving such equations. We will begin with the simplest methods for solving ordinary differential equations, namely the Euler and Taylor methods, then move on to the more accurate Runge-Kutta method, which we will use to solve the motion of a single pendulum. The culmination of the lab will be to calculate the motion of a double pendulum, and show that it displays chaotic behaviour. 2 Prelab Questions 1. Draw a flowchart showing how you use the Euler Method to solve an ODE as in question one of procedure 3.2. 2. Draw a flowchart showing how you would use the Runge-Kutta method at 4th order, to find a solution to coupled ODEs with reduction of order, as in question 1 of procedure 4.2. You can do this generally (without specifying the code for exact form of z (t) or the defining explicit initial conditions) or for the specific case of the pendulum as in question 2 of 4.2. 4

3 Simple Differential Equations 3.1 Background 3.1.1 Ordinary Differential Equations The general form of an ordinary differential equation (ODE) is given by y N (t) = f(t, y(t), y (t),..., y (N 1) (t)). (1) In order to solve this ODE we need N initial conditions, i.e for N = 1 we need y(t 0 ) = y 0, for N = 2 we need y(t 0 ) = y 0 and y (t 1 ) = y 1, etc. The methods outlined in this section are applicable to 1st order (N = 1) ODEs of the form dy(t) = f(t, y(t)). (2) dt with the single initial condition y(t 0 ) = y 0. Note that these techniques are not restricted to linear ODEs, that is function f(t.y(t),..., y (N 1) (t)) does not need to be linear in t, y(t),..., y (N 1) (t). 3.1.2 Euler s Method The Euler Method is a method for solving 1st order ODEs of the form Eq. 2. The basic premise is to decompose the solution y(t) into a series of equally spaced trapezoids of width h, in a similar fashion to the trapezoidal integration method in the last lab. Explicitly where n is an integer. t t n = nh (3) y(t) y n = y(t n ) (4) Making this approximation the gradient at t n becomes y n = (y n+1 y n )/h, which causes the Eq. 2 to simplify to y n+1 = y n + hf(t n, y n ). (5) An approximation of the solution y(t) can therefore be built up by iterating Eq. 5 for 1 n N, where Nh is the upper range of y(t). Note that that the first value in the iteration is defined by the provided initial condition, i.e. y 0 = y(t 0 ). 5

We can assess the local error of this method by calculating which for this method is of O(h 2 ). LT E(t) = y n+1 y n h f(t n, y n ), (6) 3.1.3 Taylor Expansion Method Similarly to numerical differentiation techniques we can improve on the accuracy of the Euler method by taking a Taylor expansion of the solution y(t) about the discretization scale h, i.e. y(t + h) = y(t) + hy (t) + 1 2 h2 y (t) + 1 3! h3 y (t) +... (7) We can then use Eq. 2 to express this in terms of f(t, y(t)). Using the notation y(t + h) y n+1, y(t) y n and f n f(t n, y n ), y n+1 = y n + hf n + 1 2 h2 f n + 1 3! h3 f n +... (8) Truncating at O(h 3 ), we get the iterative solution y n+1 = y n + hf n, (9) where F n f n + 1 2 hf n + 1 3! h2 f n. (10) Applying Eq. 6 one can easily show that the Taylor Method has a local error or O(h 4 ). This is clearly more accurate then than the Euler Method. In fact one can consider the Euler Method to be the Taylor Method truncated at O(h). 3.2 Procedure 1. Write an algorithm for solving an ODE using the Euler Method. 2. Consider the ODE y (x) = xy with y(0) = 1, with exact solution y(x) = e x2 /2. Use both numerical techniques to find approximate solutions to this equation. You can copy the algorithm for the Taylor Method directly from the example code. 3. Construct a table of values and investigate accuracy of each method. 6

3.3 Example Code // Example Code for Ordinary Differential Equations // Author: Chia-Ling Hsu // Date: 14/03/2016 #include <stdio.h> #include <stdlib.h> #include <math.h> double dfun(double, double); // y'(x) double ddfun(double, double); // y''(x) double dddfun(double, double); // y'''(x) double sol(double); // Solution of y(x) double taylor(double, double, double); // Taylor's method int main() double x = 0.; double h = 0.01; // Step double x_f = 1.; // double exact = 1.; // Variable for exact solution double y_t = 1.; // solution for Taylor's method double err_t = 0; // error for Taylor's method // Open a file for saving results FILE *fp = fopen("ode.dat","w"); // Input steps and end point of x printf("\nenter h,x_f: "); scanf("%lf%lf",&h,&x_f); printf("x \t y_t \t y(x) - y_t \n"); printf("%lf %lf %E\n", x, y_t, err_t); // Loop Starts while (x<x_f) // Assign values to variables y_t = taylor(x,y_t,h); // Increase x by h x += h; exact = sol(x); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 7

// Calculate the error for different methods // Here error is defined as the absolute value of the difference between exact and numerical solutions. err_t = fabs(exact - y_t); // Print out the results for each step printf("%lf %lf %E\n", x, y_t, err_t ); // End of while loop // Close the file fclose(fp); return 0; // Exact solution of the given question double sol(double x) return exp(x*x*0.5); // Define first order differential equation double dfun(double x, double y) // y' = x*y return x*y; // Define second order differential equation (Taylor's only) double ddfun(double x, double y) // y'' = y + x*y' return y+x*dfun(x, y); // Define third order differential equation (Taylor's only) double dddfun(double x, double y) // y''' = 2*y' + x*y'' return 2*dfun(x,y)+x*ddfun(x,y); // Taylor method starts double taylor(double x, double y, double h) // Calculate till 3rd order 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 8

y = y + h*(dfun(x, y) + h/2.*(ddfun(x,y) + (h/3.*dddfun(x,y)))); // Return result return y; 84 85 86 87 9

4 Coupled and Higher-Order Differential Equations 4.1 Background 4.1.1 Coupled ODEs and Reduction of Order The methods above are useful for solving first order ODEs but how do we deal with higher orders? The solution is to reduce and Nth order ODE into a system of N coupled first order ODEs, which we can solve using 1st order methods. To explain further we will consider the example of a second order ODE, though of course the same rationale can be applied up to any order. Let us begin with y (t) = f(t, y(t), y (t)), (11) which has initial conditions y(t 0 ) = y 0 and y (t 1 ) = y 1. We can now define z(t) y (t). We now have 2 coupled first order ODEs y (t) = g(t, y(t), z(t)), (12) z (t) = f(t, y(t), z(t)), (13) where g(t, y(t), z(t)) z, and y(t 0 ) = y 0 and z(t 1 ) = y 1 z 1. We can now use our usual 1st order techniques to solve first for z(t) and then for y(t). For example, if we were to use Euler s Method, we would iterate over starting from initial conditions y 0 and z 0. z n+1 = z n + hf(t n, y n, z n ), (14) y n+1 = y n + hg(t n, y n, z n ), (15) 4.1.2 Runge-Kutta Method The next step up in accuracy from the previous two methods discussed is the Runge-Kutta Method. Where the last two algorithms used 2 sample points per step in our iteration, namely y(t n ) and y(t n + h), the Runge-Kutta Method uses the average of 4 weighted points, specifically y n+1 = y n + h 6 (k 1 + 2k 2 + 2k 3 + k 4 ), (16) 10

where k 1 = f(t n, y n ), (17) k 2 = f(t n + h 2, y n + h 2 k 1), (18) k 3 = f(t n + h 2, y n + h 2 k 2), (19) k 4 = f(t n + h, y n + hk 3 ). (20) k 1 and k 4 are increments based on the slope at the beginning and endpoints of the slice, which k 2 and k 3 are increments based on the slope at the midpoint of the slice. The middle points are weighted more heavily than the endpoints in this algorithm. This method is more accurate than both the Euler and Taylor methods, having a local truncation error of O(h 5 ). This method is actually the 4th order of a class of methods collectively known as Runge-Kutta Methods, defined by where y n+1 = y n + h s b i k i, (21) i=1 k 1 = f(t n, y n ), (22) k 2 = f(t n + c 2 h, y n + (a 21 k 1 )), (23) k 3 = f(t n + c 3 h, y n + (a 31 k 1 + a 32 k 2 )), (24). k s = f(t n + c s h, y n + h(a s1 k 1 + a s2 k 2 +... + a s,s 1 k s 1 )). (25) The weighting coefficients a b and c are obtained from standard tables known as Butcher Tableau, usually represented like 0 c 2 a 21 c 3 a 21 a 32..... c s a s1 a s2... a s,s 1 b 1 b 2... b s 1 b s Table 1: General format of Butcher Tableau. 11

For example the Butcher Tableau for the 4th order Runge-Kutta Method is given by Table 2, and that of the less accurate 2nd order Runge-Kutta Method is given by Table 3. 0 1/2 1/2 1/2 0 1/2 1 0 0 1 1/6 1/3 1/3 1/6 Table 2: RK4 Butcher Tableau. 0 1/2 1/2 0 1 Table 3: RK2 Butcher Tableau. 4.1.3 Applying the Runge-Kutta Method With Reduction of Order for Coupled ODEs When applying the Runge-Kutta method with coupled ODEs we have: z n+1 = z n + h 6 (j 1 + 2j 2 + 2j 3 + j 4 ), (26) y n+1 = y n + h 6 (k 1 + 2k 2 + 2k 3 + k 4 ). (27) 12

To get the k and j values we have to calculate the following (in order) like so: j 1 = f(t n, y n, z n ), (28) k 1 = g(t n, y n, z n ), (29) j 2 = f(t n + h 2, y n + h 2 k 1, z n + h 2 j 1), (30) k 2 = g(t n + h 2, y n + h 2 k 1, z n + h 2 j 1), (31) j 3 = f(t n + h 2, y n + h 2 k 2, z n + h 2 j 2), (32) k 3 = g(t n + h 2, y n + h 2 k 2, z n + h 2 j 2), (33) j 4 = f(t n + h, y n + hk 3, z n + hj 3 ), (34) k 4 = g(t n + h, y n + hk 3, z n + hj 3 ). (35) Where f and g are those from section 4.1.1. 4.2 Procedure 1. Write a routine for finding solutions to coupled ODEs using Runge-Kutta method at 4th order. Assume that you know the analytic form of z (t) = f(t, y(t), z(t)), and the initial conditions z 0 and y 0. We know that g(t n, y n, z n ) = z n so for example; g(t n + h 2, y n + h 2 k 1, z n + h 2 j 1) = z n + h 2 j 1. 2. Consider a swinging pendulum, whose angle to the horizontal is given by θ(t). The equation of motion for this system is given by 0 = θ (t) + g sin θ/l θ (t) + gθ/l if we make the small angle approximation. Express this system as a set of coupled ODEs and solve for θ(t) using a 4th order Runge-Kutta routine up to t = 1s (choose h 1s), using the initial conditions θ(0) = π/6 and θ (0) = 0. Plot the resulting angle as a function of time. (l = 0.35m, g = 10m/s 2 ) 3. Repeat the above procedure, varying the initial conditions, graphing the results. What do you notice as the initial angle decreases/increases. 4. Increase the maximum time to t = 60s. Is the solution stable? 5. Extension: Repeat the above without assuming the small angle approximation, i.e. using the equation of motion 0 = θ (t) + g sin θ/l θ (t) + gθ/l. Comment on any differences in the angular trajectory. 13

4.3 Example Code // Example Code for Coupled ODEs and Reduction of Order // Author: Julia McCoey // Date: 17/03/2016 // Example Code for Runge-Kutta method. // Author: Julia McCoey // Date: 16/03/2016 #include <stdio.h> #include <stdlib.h> #include <math.h> typedef float (function)(float,float); // Declare function signatures float differential(float, float); // y'(x). This is the differential equation. float exactsolution(float); // y(x). This is the equation we approximate with Runge Kutta float RungeKutta(function, float, float, float); // Runge-Kutta average of 4 points float k1(function, float, float); float k2(function, float, float, float); // equations for Runge-Kutta method float k3(function, float, float, float); float k4(function, float, float, float); int main() float h = 0.01; // Step size float x0 = 0; // This is the initial x value float xf = 1.; // This is the final x value to calculate the approximation up to float y0 = 0; // This is the initial condition, i.e., y(x0) // Input step size and initial conditions 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 14

printf("\nplease enter step size: "); scanf("%f",&h); printf("\nplease enter the first x value you would like to calculate from, i.e. x0: "); scanf("%f",&x0); printf("\nplease enter the last x value you would like to calculate up to: "); scanf("%f",&xf); printf("\nplease enter the initial condition for y, i.e. y(x0): "); scanf("%f",&y0); int arraysize=((xf-x0)/h)+2; // size of arrays for storing the x values and the y values float xvalues[arraysize]; // array for storing x values; pgplot can plot these. float yvaluesrk[arraysize]; // array for storing y values; pgplot can plot these. float yvaluesexact[arraysize]; // array for storing the exact values xvalues[0]=x0; //start the x array at the first x value yvaluesrk[0]=y0; //start the y array with the given initial condition yvaluesexact[0]=y0; //fill the first value of the exact y values // This loop fills the x array with values increasing by h, // and fills the Runge-Kutta y array with values approximated by the Runge-Kutta method. // It also fills yvaluesexact with y values calculated with an exact solution to // the differential equation. int i; for (i=1; i<arraysize ; i++) //Use the Runge Kutta method to approximate the next y value yvaluesrk[i]=rungekutta( differential,xvalues[i-1],yvaluesrk[i-1],h); //Increase x by h (make the next x value be the previous one plus h) xvalues[i]=xvalues[i-1]+h; //Fill in exact solutions (to compare to the approximation) 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 15

yvaluesexact[i] = exactsolution(xvalues[i]); //Print results. They are in arrays of floats, so you can plot them with PGPlot. printf("\n x Runge Kutta y exact y"); for (i=0; i<arraysize-1 ; i++) printf("\n%4f\t%4f\t%4f",xvalues[i],yvaluesrk[i],yvaluesexact[i]); printf("\n"); return 0; // The first order differential equation float differential(float x, float y) // y' = x*y return x*y; // Exact solution of the differential equation, i.e., the equation that // we try to approximate with Runge-Kutta method float exactsolution(float x) return exp(x*x*0.5); //The Runge Kutta method (technically the 4th order Runge Kutta method) float RungeKutta(function f, float t, float yn, float h) return yn + (h/6)*( k1(f,t,yn) + (2*k2(f,t,yn,h)) + (2*k3(f,t,yn,h)) + k4(f,t,yn,h) ); //Equations used in Runge Kutta float k1(function f, float t, float yn) return f(t,yn); 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 16

float k2(function f, float t, float yn, float h) return f(t,yn+((h/2)*k1(f,t,yn))); float k3(function f, float t, float yn, float h) return f(t,yn+((h/2)*k2(f,t,yn,h))); float k4(function f, float t, float yn, float h) return f(t,yn+(h*k3(f,t,yn,h))); 114 115 116 117 118 119 120 121 122 123 124 125 17

5 Bonus Section: The Transition from Order to Chaos: Plotting the Motion of a Double Pendulum 5.1 Background In this section we will look at a system with a set of equations of motion that are not analytically tractable, i.e. the motion of a double pendulum. This system is of particular interest because it can display chaotic behaviour, with the paths of the pendula tracing out unpredictable complex paths which belie the seemingly simple nature of the set up. 5.1.1 Chaotic Systems A system is said to behave chaotically if it is extremely sensitive to the initial conditions. It may be deterministic, i.e. it follows well defined classical equations of motion, but it can be unpredictable in its long term behaviour. If the initial conditions of a chaotic system are not known to infinite precision, then it is impossible to predict what will happen to the system in the long term. The transition from order to chaos in a given system can be as simple as choosing a set of initial conditions in a given range. Let us illustrate this with an example. Consider an object undergoing simple harmonic motion. We begin with some initial conditions, and the object will oscillate with a well defined frequency and period. If we were to perturb those initial conditions slightly, the resulting amplitude and period would be perturbed by a small predictable amount. In a chaotic system like the one covered in this section (a double pendulum), a small perturbation in the initial conditions, (say the initial angles of the pendula) can lead to paths which in no way resemble those traced out without the perturbation. 5.1.2 Describing Complex Systems: Lagrange s Equations and Generalised Coordinates While Newtons equations of motion (F x = mẍ and F y = mÿ) are very useful for describing the motion of an object with time given a force (for example gravity), they can become extremely messy in some situations. In particular, you may be looking at a system where a Cartesian coordinate system is not the best choice 18

(such as for a double pendulum), what then do you do? The answer lies in the Lagrangian formalism of classical mechanics. Instead of writing down Newtons equations, we postulate the existence of an object known as the Lagrangian of a system L = T V, where T and V are the kinetic and potential energies of the system respectively. The Lagrangian can then be used to calculate the equations of motion by using the Euler Lagrange equations d dt ( ) L q i L q i = 0. (36) q i are known as a generalised coordinates, they can be any set of independent coordinates, for example q 1 = x, q 2 = y in a Cartesian basis, or q 1 = r, q 2 = θ in a polar basis. This is quite powerful, as given a Lagrangian in any coordinate system we can determine the equations of motion without the necessity of using a Cartesian basis. As an example let us consider the Lagrangian for a point mass m confined to the x direction, which experience potential V (x), which is given by L = 1 2 mẋ2 V (x). V (x) V (x) Using this L, Eq. 36 becomes mẍ =. Recalling that F (x), we x x return Newtons equation of motion. You will learn more about the Lagrangian formulation of classical mechanics in your classical physics course. 5.1.3 Double Pendulum θ 1 l 1 m 1 θ 2 l 2 m 2 Figure 1: Function f(x) broken into equally spaced differentiation/integration regions of width h. 19

The setup of a double pendulum system can be seen in Fig. 1. For two pendulums of fixed length and mass, allowed to swing in a 2D plane, there are 2 independent degrees of freedom, θ 1 and θ 2, defined in Fig. 1. The Lagrangian of this system can be easily derived to be the following L = T V = 1 2 (m 1 + m 2 )l 2 1 θ 2 1 + 1 2 m 2l 2 2 θ 2 2 + m 2 l 1 l 2 θ1 θ2 cos(θ 1 θ 2 ). (37) From this we can use the Euler-Lagrange equations in Eq. 36 to derive the equations of motion for θ 1 and θ 2, which turn out to be 0 = (m 1 + m 2 )l 1 θ1 + m 2 l 2 θ2 cos(θ 1 θ 2 ) + m 2 l 2 θ2 2 sin(θ 1 θ 2 ) + g(m 1 + m 2 ) sin θ 1 0 = m 2 l 2 θ2 + m 2 l 1 θ1 cos(θ 1 θ 2 ) m 2 l 1 θ2 1 sin(θ 1 θ 2 ) + m 2 g sin θ 2 (39) We are left with two second order ODEs, the two solutions to which are θ 1 (t) and θ 2 (t). To obtain these solutions we require 4 initial conditions, for example values for θ 1 (t 0 ), θ 2 (t 0 ), θ 1 (t 0 ), and θ 2 (t 0 ). We can then apply reduction of order to these equations to reduce these 2 second order equations to a set of 4 first order ODEs. We make the substitution ϕ 1 θ 1 and ϕ 2 θ 2, which leave us with the following 4 first order coupled ODEs θ 1 = a(θ 1, θ 2, ω 1, ω 2 ), (40) ω 1 = b(θ 1, θ 2, ω 1, ω 2 ), (41) θ 2 = c(θ 1, θ 2, ω 1, ω 2 ), (42) ω 2 = d(θ 1, θ 2, ω 1, ω 2 ), (43) where a(θ 1, θ 2, ω 1, ω 2 ) ω 1, b(θ 1, θ 2, ω 1, ω 2 ) ω 2, and b(θ 1, θ 2, ω 1, ω 2 ) and d(θ 1, θ 2, ω 1, ω 2 ) can be determined by solving Eqs. 38 and 39 for ω 1 and ω 2. They are messy so we will not provide them in the text, please refer to the example code for their explicit forms. We can now express these equations in Runge-Kutta form, such that we can find numerical solutions for θ 1 (t) and θ 2 (t). Extending the logic of Section 4.1.2 we obtain the iterative equations: θ 1, n+1 = θ 1, n + h 6 (j 1 + 2j 2 + 2j 3 + j 4 ), (44) ω 1, n+1 = ω 1, n + h 6 (k 1 + 2k 2 + 2k 3 + k 4 ), (45) θ 2, n+1 = θ 2, n + h 6 (l 1 + 2l 2 + 2l 3 + l 4 ), (46) ω 2, n+1 = ω 2, n + h 6 (m 1 + 2m 2 + 2m 3 + m 4 ), (47) (38) 20

where analogously to before j 1 = a(t n, θ 1 n, θ 2 n, ω 1 n, ω 2 n ), (48) k 1 = b(t n, θ 1 n, θ 2 n, ω 1 n, ω 2 n ), l 1 = c(t n, θ 1 n, θ 2 n, ω 1 n, ω 2 n ), m 1 = d(t n, θ 1 n, θ 2 n, ω 1 n, ω 2 n ), j 2 = a(t n + h 2, θ 1 n + h 2 j 1, θ 2 n + h 2 l 1, ω 1 n + h 2 k 1, ω 2 n + h 2 m 1), (49) k 2 = b(t n + h 2, θ 1 n + h 2 j 1, θ 2 n + h 2 l 1, ω 1 n + h 2 k 1, ω 2 n + h 2 m 1), l 2 = c(t n + h 2, θ 1 n + h 2 j 1, θ 2 n + h 2 l 1, ω 1 n + h 2 k 1, ω 2 n + h 2 m 1), m 2 = d(t n + h 2, θ 1 n + h 2 j 1, θ 2 n + h 2 l 1, ω 1 n + h 2 k 1, ω 2 n + h 2 m 1), j 3 = a(t n + h 2, θ 1 n + h 2 j 2, θ 2 n + h 2 l 2, ω 1 n + h 2 k 2, ω 2 n + h 2 m 2), (50) l 3 = c(t n + h 2, θ 1 n + h 2 j 2, θ 2 n + h 2 l 2, ω 1 n + h 2 k 2, ω 2 n + h 2 m 2), k 3 = b(t n + h 2, θ 1 n + h 2 j 2, θ 2 n + h 2 l 2, ω 1 n + h 2 k 2, ω 2 n + h 2 m 2), m 3 = d(t n + h 2, θ 1 n + h 2 j 2, θ 2 n + h 2 l 2, ω 1 n + h 2 k 2, ω 2 n + h 2 m 2), j 4 = a(t n + h, θ 1 n + hj 3, θ 2 n + hl 3, ω 1 n + hk 3, ω 2 n + hm 3 ), (51) k 4 = b(t n + h, θ 1 n + hj 3, θ 2 n + hl 3, ω 1 n + hk 3, ω 2 n + hm 3 ), l 4 = c(t n + h, θ 1 n + hj 3, θ 2 n + hl 3, ω 1 n + hk 3, ω 2 n + hm 3 ), m 4 = d(t n + h, θ 1 n + hj 3, θ 2 n + hl 3, ω 1 n + hk 3, ω 2 n + hm 3 ). We can now start with 4 initial conditions θ 1 0, θ 2 0, ω 1 0, and ω 2 0, and iterate over n to determine solutions to θ 1 (t) and θ 2 (t). 21

5.1.4 Initial Conditions and Energy Domains The initial conditions of the system determine the amount of energy available to it. For example the initial angles and velocities θ 1 /2(0), θ1 /2(0) will determine how much potential and kinetic energy is available to the pendula from the beginning. Given that the the pendulum system is closed, this energy is conserved, implying that the initial the amount of energy injected has a great effect on extent of motion on the system. Specifically, one can determine a-priori whether or not it is possible for either pendulum to flip completely around, based on the amount of energy initially available. Though the derivation is beyond the scope of this course, it is possible to show that if the initial conditions satisfy θ 1 (0) = θ 2 (0) = 0, and 3 cos θ 1 + cos θ 2 > 2, (52) then it is impossible for either pendulum to flip completely, there is simply not enough energy available. We can use this information when choosing our initial conditions, to better understand the resulting angular paths. 5.2 Procedure (One Bonus Mark) 1. Write a routine which finds solutions for θ 1 (t) and θ 2 (t) using the Runge- Kutta technique. The example code does this using the Euler Method. You may use this as a guide, even copying the relevant pieces of code directly. Note that the number of steps is N = 10000. 2. As a test of your routine, assume θ 1 = θ 2, and use the initial conditions in Section 4.2 (θ 1 (0) = θ 2 (0) = π/6 and ω 1 (0) = ω 2 (0) = 0) to find the solution for θ 1 (t). This should just be the same solution as for a single pendulum. Plot up until t = 10s. 3. Use these same initial conditions and plot angular trajectories up to t = 10s for general solution θ 1 θ 2 0. Compare this with the result for a single pendulum and discuss. 4. Choose the initial condition θ 1 (0) = θ 2 (0) = π/3 and ω 1 (0) = ω 2 (0) = 0 and plot angular trajectories up to t = 10s. Change the values of the initial angles slightly. How does the long term behaviour change? Are you seeing evidence of chaotic behaviour? 22

5. Extension: Vary your initial conditions θ 1 (0) and θ 2 (0), choosing a few values which satisfy Eq. 52, and a few which do not. Plot your trajectories up to t = 10s. Comment on any differences in the long term behaviour of the two classes of initial condition. Varying these slightly from their initial values, do you see evidence of more/less chaotic behaviour in the different energy domains? 5.3 Example Code // Example Code for The Transition from Order to Chaos // Author: Anton Hawthorne // Date: 16/03/2016 #include <stdlib.h> #include <stdio.h> #include <math.h> #include <cpgplot.h> //Define G as +9.81m/s, the differential equations assume g is a +ve value. #define G 9.81 #define PI 3.14159 //Throughout: the variables labelled 1 belong to the first pendulum, and those labelled 2 correspond to the pendulum that is attached to the first. //so total extended length is l1+l2. the angle theta 2 is the angle from vertical, does not depend on theta1 inherently (although its dynamics do) //The right hand side of the second order differential equation: returns d(omega1)/dt = d2(theta1)/dt2 given the input masses, lengths, angles and angular velocities double f1(double m1, double m2, double l1, double l2, double theta1, double theta2, double omega1, double omega2) double a = m2*l2*pow(omega2,2)*sin(theta2-theta1); double b = -(m1+m2)*g*sin(theta1); double c = m2*cos(theta2-theta1)*(l1*pow(omega1,2)*sin(theta2-theta1) -G*sin(theta2)); double d = m1 + m2*pow(sin(theta2-theta1),2); return (a + b + c) / d; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 23

//returns d(omega2)/dt = d2(theta2)/dt2 given the input masses, lengths, angles and angular velocities double f2(double m1, double m2, double l1, double l2, double theta1, double theta2, double omega1, double omega2) double a = -(m1+m2)*(l1*pow(omega2,2)*sin(theta2-theta1) + G*sin(theta2)); double b = cos(theta2-theta1)*((m1+m2)*g*sin(theta1) - m2*l2*pow(omega2,2)*sin(theta2-theta1)); double c = l2*(m1+m2*pow(sin(theta2-theta1),2)); return (a + b) / c; //return the next omega (timestep h later) given the input masses, lengths, angles, angular velocities and the timestep: h double omega1iterate(double m1, double m2, double l1, double l2, double theta1, double theta2, double omega1, double omega2, double h) return omega1 + h*f1(m1,m2,l1,l2,theta1,theta2,omega1,omega2); //the next value is the input value, plus the gradient multiplied by the width //same again for the angular velocity of the second pendulum double omega2iterate(double m1, double m2, double l1, double l2, double theta1, double theta2, double omega1, double omega2, double h) return omega2 + h*f2(m1,m2,l1,l2,theta1,theta2,omega1,omega2); //find the next theta1 double theta1iterate(double theta1, double omega1, double h) return theta1 + h * omega1; //The next theta one is the input + the angular velocity * timestep double theta2iterate(double theta2, double omega2, double h) return theta2 + h * omega2; //Function to run the pendulums and plot the trajectories void runpendula(double m1, double m2, double l1, double l2, double theta1initial, double theta2initial, double omega1initial, double omega2initial, double time, int stepno) 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 24

double h = time/stepno; //timestep is the total run time/number of steps double theta1 = theta1initial; double theta2 = theta2initial; double omega1 = omega1initial; double omega2 = omega2initial; //store the x and y values of each pendulum for each time step float xvalues1[stepno]; float yvalues1[stepno]; float xvalues2[stepno]; float yvalues2[stepno]; double omega1new;//temporary placeholder for omega1 int i = 1; for (i = 1; i<=stepno; i++) //calculated the next iterations of the angular velocities with the previous omega values omega1new = omega1iterate(m1,m2,l1,l2,theta1,theta2,omega1,omega2,h); omega2 = omega2iterate(m1,m2,l1,l2,theta1,theta2,omega1,omega2,h); //after omegas have been calculated, set omega1 to be omega1new. This way is needed so that both input omegas are used in calculating omega1 and omega2 omega1 = omega1new; //now calculate the next theta values theta1 = theta1iterate(theta1,omega1,h); theta2 = theta2iterate(theta2,omega2,h); //angle check for the cases where the pendulum might flip, if either pendulum does a full rotation then reset the angles if (theta1 > 2*PI) theta1 = theta1-2*pi; if (theta1 < -2*PI) theta1 = theta1 + 2*PI; if (theta2 > 2*PI) theta2 = theta2-2*pi; if (theta2 < -2*PI) theta2 = theta2 + 2*PI; //add the x and y positions of pendulum 1 at this time 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 25

xvalues1[i-1] = l1*sin(theta1); yvalues1[i-1] = -l1*cos(theta1); //add the x and y positions of pendulum 2 xvalues2[i-1] = l1*sin(theta1)+l2*sin(theta2); yvalues2[i-1] = -l1*cos(theta1)-l2*cos(theta2); //plot the results! cpgbeg(0,"/xs",1,1); cpgsls(1); cpgsch(1.); cpgswin(-1,1,-1,1); cpgbox("bcnst", 0.0, 0, "BCNST", 0.0, 0); cpgmtxt("b",3.,.5,.5,"x axis"); cpgmtxt("l",3,.5,.5,"y axis"); cpgsch(2.); cpgmtxt("t",1,.5,.5,"title"); cpgsci(7); cpgsci(4);//set colour and print pendulum 1 trajectory cpgline(stepno,xvalues1,yvalues1); cpgsci(2);//set a different colour and print pendulum 2 trajectory cpgline(stepno,xvalues2,yvalues2); cpgend(); 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 int main(void) //the initial masses, lengths, angles and angular velocities double m1 = 1;//in kg double m2 = 1; double l1 = 0.5;//in m double l2 = 0.5; double theta1initial = -PI/6;//in radians double theta2initial = PI/6; double omega1initial = 0;//in radians per sec double omega2initial = 0; double timeinterval = 2;//in seconds int stepno = 10000; runpendula(m1,m2,l1,l2,theta1initial,theta2initial,omega1initial, omega2initial,timeinterval,stepno); 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 26

return 1; 137 138 27