FORTRAN 77 Lesson 7. Reese Haywood Ayan Paul

Similar documents
Consistency and Convergence

Physics 584 Computational Methods

Ordinary Differential Equations

Euler s Method, cont d

Numerical Solution of Differential Equations

Fourth Order RK-Method

Variable Step Size Differential Equation Solvers

Ph 22.1 Return of the ODEs: higher-order methods

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

EXAMPLE OF ONE-STEP METHOD

Ordinary Differential Equations

Math 128A Spring 2003 Week 12 Solutions

NUMERICAL SOLUTION OF ODE IVPs. Overview

Numerical methods for solving ODEs

Higher Order Taylor Methods

Ordinary Differential Equations

Initial value problems for ordinary differential equations

Chapter 9b: Numerical Methods for Calculus and Differential Equations. Initial-Value Problems Euler Method Time-Step Independence MATLAB ODE Solvers

Section 7.4 Runge-Kutta Methods

Numerical solution of ODEs

2.1 NUMERICAL SOLUTION OF SIMULTANEOUS FIRST ORDER ORDINARY DIFFERENTIAL EQUATIONS. differential equations with the initial values y(x 0. ; l.

Scientific Computing with Case Studies SIAM Press, Lecture Notes for Unit V Solution of

Numerical Differential Equations: IVP

Ordinary Differential Equations (ODEs)

Chapter 8. Numerical Solution of Ordinary Differential Equations. Module No. 1. Runge-Kutta Methods

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

DIFFERENTIAL EQUATIONS

Ordinary Differential Equations II

Astronomy 8824: Numerical Methods Notes 2 Ordinary Differential Equations

Mathematics for chemical engineers. Numerical solution of ordinary differential equations

Math 308 Week 8 Solutions

The Princeton Review AP Calculus BC Practice Test 2

Comparison of Numerical Ordinary Differential Equation Solvers

Ordinary Differential Equations (ODEs)

Runge-Kutta methods. With orders of Taylor methods yet without derivatives of f (t, y(t))

Differential Equations

Ordinary differential equations - Initial value problems

Taylor series. Chapter Introduction From geometric series to Taylor polynomials

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

Numerical Methods for Initial Value Problems; Harmonic Oscillators

Calculus for the Life Sciences

Simple ODE Solvers - Derivation

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

DIFFERENTIAL EQUATIONS

Numerical Methods for Initial Value Problems; Harmonic Oscillators

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

Ordinary Differential Equations II

MB4018 Differential equations

Chap. 20: Initial-Value Problems

Ordinary differential equation II

4 Stability analysis of finite-difference methods for ODEs

Initial Value Problems

Infinite series, improper integrals, and Taylor series

10 Numerical Solutions of PDEs

Numerical method for approximating the solution of an IVP. Euler Algorithm (the simplest approximation method)

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

Chapter 6 - Ordinary Differential Equations

Lecture 8: Calculus and Differential Equations

Lecture 8: Calculus and Differential Equations

Virtual University of Pakistan

INTRODUCTION TO COMPUTER METHODS FOR O.D.E.

Numerical Integration of Ordinary Differential Equations for Initial Value Problems

Solution of ordinary Differential Equations

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

Lecture 42 Determining Internal Node Values

Numerical Methods for the Solution of Differential Equations

Solutions of the Sample Problems for the First In-Class Exam Math 246, Fall 2017, Professor David Levermore

A First Course on Kinetics and Reaction Engineering Supplemental Unit S5. Solving Initial Value Differential Equations

Numerical Methods - Boundary Value Problems for ODEs

Initial value problems for ordinary differential equations

8.1 Introduction. Consider the initial value problem (IVP):

+ h4. + h5. 6! f (5) i. (C.3) Since., it yields

Computational Techniques Prof. Dr. Niket Kaisare Department of Chemical Engineering Indian Institute of Technology, Madras

Chapter 5 Exercises. (a) Determine the best possible Lipschitz constant for this function over 2 u <. u (t) = log(u(t)), u(0) = 2.

Outline Calculus for the Life Sciences II. Pollution in a Lake 1. Introduction. Lecture Notes Numerical Methods for Differential Equations

1 Error Analysis for Solving IVP

Numerical Methods in Physics and Astrophysics

FIRST-ORDER ORDINARY DIFFERENTIAL EQUATIONS II: Graphical and Numerical Methods David Levermore Department of Mathematics University of Maryland

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

Solution. Your sketch should show both x and y axes marked from 5 to 5 and circles of radius 1, 3, and 5 centered at the origin.

Chapter 11 ORDINARY DIFFERENTIAL EQUATIONS

1.4 Techniques of Integration

Math 216 Final Exam 14 December, 2012

MATH 100 Introduction to the Profession

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

Ordinary Differential Equations II: Runge-Kutta and Advanced Methods

KINGS COLLEGE OF ENGINEERING DEPARTMENT OF MATHEMATICS ACADEMIC YEAR / EVEN SEMESTER QUESTION BANK

F = ma, F R + F S = mx.

(A) Opening Problem Newton s Law of Cooling

Diff. Eq. App.( ) Midterm 1 Solutions

Lecture Notes on Numerical Differential Equations: IVP

Numerical and Statistical Methods

Day 2 Notes: Riemann Sums In calculus, the result of f ( x)

dt 2 The Order of a differential equation is the order of the highest derivative that occurs in the equation. Example The differential equation

THE UNIVERSITY OF NEW SOUTH WALES SCHOOL OF PHYSICS PHYS2020 COMPUTATIONAL PHYSICS FINAL EXAM SESSION I Answer all questions

Section 5.2 Series Solution Near Ordinary Point

Numerical and Statistical Methods

MA 102 Mathematics II Lecture Feb, 2015

Numerical method for approximating the solution of an IVP. Euler Algorithm (the simplest approximation method)

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

Transcription:

FORTRAN 77 Lesson 7 Reese Haywood Ayan Paul 1 Numerical Integration A general all purpose Numerical Integration technique is the Trapezoidal Method. For most functions, this method is fast and accurate. Other functions, may require more sofisticated methods, which can be found in Numerical Recipes, but we will stick to simple funcitons in this class. 1.1 Trapezoidal Rule If we are given the function that represents the curve, we can evaluate the function at points spaced along the interval of interest. If we join the points onthecurve withstraight linesegments, weformagroupoftrapezoidswhose combined areas approximate the are under the curve. The closer the points are together on the curve the more trapezoids there are in the interval, and thus the more accurate will be our approximation to the integral. Recall that the area of a trapezoid is equal to one-half the base times the sum of the two heights (or sides): area = 1 base(height1 +height2). Thus, 2 the area of the trapezoid is computed using the pair of points (x 1,y 1 ) and (x 2,y 2 ): A = 1(x 2 2 x 1 )(y 1 +y 2 ). If all of our points are equally spaced, then (x 2 x 1 ) is a constant, and if there are N trapezoids that represent the curve then the integral can be approximated by: base N 2 (y 1 +2 y k +y N+1 ) (1) k=2 When computing an integral with this technique, we need to remember that the data points on the curve could come from different sources. If we have the equation of the curve, we can choose the data points to be close 1

together or far apart. Another possibility is that the points are experimentally collected; in this case, we have discrete x and y values, and we cannot change the size of the base, it is fixed by x, but this does not keep us from using the trapezoid rule. 1.2 Simpson s Rule The Simpson s Rule (or Simpson s 1 Rule) makes the assumption that the 3 function can be approximated by a series of overlapping second order curves. Thus we always need even number of intervals to do an integral by this method. The detailed derivation of this method can be found in any book on Numerical Integration or Statistics. The formula for the integration is xn+1 y(x) = h x 1 3 [y 1 +4(y 2 +y 4 +...)+2(y 3 +y 5 +...)+y N+1 ] (2) with N odd. The Simpson s Rule is more accurate as it uses second order approximation as compared to the first order approximation used by the Trapezoidal Rule and thus uses more computing time and resources. For most problems the Trapezoidal Rule will be good enough, however, you might sometime have to appeal to the Simpson s Rule for more complicated integrals. 2 Solving Ordinary Differential Equations Before we can write a Fortran program to solve an Ordinary Differential Equation (ODE) we must learn a little about solving ODE s by hand. For this section I will quote from Introduction to Ordinary Differential Equations with Mathematica by Gray, Mezzino and Pinsky. Also, I will quote from Numerical Recipes, which we used last time. Second order ODE s can always be reduced to the study of sets of firstorder differential equations. For exmple d 2 y +q(x)dy = r(x) 2 (3) can be rewritten as two first-order equations dy = z(x) (4) dz = r(x) q(x)z(x) (5) 2

where z is a new variable. The generic problem in ordinary differential equations is thus reduced to the study of a set of N coupled first-order differential equations for the functions y i,i = 1,2,3,...,N, Having the general form dy i (x) = f i (x,y 1,...,y N ),i = 1,...,N (6) where the functions f i on the right-hand side are known. Most differential equations do not have exact solutions. Even if a differential equation has an exact solution, the solution may be so complicated that it is useless for practical purposes. On the other hand, we can try to find an approximation to the solution of a differential equation with an initial condition. For example, suppose we are given a first-order initial value problem y = f(t,y) (7) y(a) = Y 0 (8) onanintervala t b. Althoughwemaynotbeabletoobtainaformulafor the solution of eqn. s 5 and 6, we can subdivide the interval as a = t 0 < t 1 <... < t N = b and try to assign approximate values y(t n ) to t n for n = 1,...,N. Instead of a formula we will have an approximation to the solution expressed as a table of the y(t n ) s in terms of the t n s. By graphing the table we can visualize the solution. 2.1 The Euler Method The Euler method was the first method used to find numerical solutions to differential equaitons. In spite of the fact that it is rarely used in practice, we need to study it because it serves as a model for more complicated methods such as the Rung-Kutta method, an all purpose method we will write our program with. Suppose we want to construct a solution to equation (5) above. If we know the solution exactly, and that it is twice differentiable, we can write the Taylor expansion as y(t 1 ) = y(t 0 )+(t 1 t 0 )y (t 0 )+ (t 1 t 0 ) 2 y (t 0 ) (9) 2 3

Letting h = t 1 t 0 we can rewrite (7) as y(t 1 ) = y(t 0 )+hf(t 0,Y 0 )+ h2 2 y (t 0 ) (10) where we have just substituted equation (5) evaluated at (t 0,y 0 ) for y (t 0 ) and also substituted h for (t 1 t 0 ). With h small this becomes simply and, since y(t 0 ) = Y 0 we can define y(t 1 ) y(t 0 )+hf(t 0,Y 0 ) (11) Y 1 = Y 0 +hf(t 0,Y 0 ) (12) Then Y 1 is an approximation to y(t 1 ). More generally, we define Y n+1 = Y n +hf(t n,y n ) (13) for 0 n N 1. The Euler method, or tangent method, consists in approximating the solution to (5) by means of (11), which we call the Euler method formula. 2.2 The Runge-Kutta Method The Runge-Kutta method is an improvement of the Euler method, and is derived in much the same way. However, the Taylor series is truncated in the 5 th power of (t 1 t 0 ), so the order of the method is fourth-order, and errors are introduced on the order of h 5. A derivation of the Runge-Kutta method is in most Numerical Analysis book. I will only give the results of the derivation here. The general form of the Runge-Kutta method is where Y n+1 = Y n + h 6 (a 1,n +2a 2,n +2a 3,n +a 4,n ), (14) a 1,n = f(t n,y n ) a 2,n = f(t n + h 2,Y n + h 2 a 1,n) a 3,n = f(t n + h 2,Y n + h 2 a 2,n) a 4,n = f(t n +h,y n +h a 3,n ) (15) 4

2.3 Coding the Equations Whether we are going to code the Euler or Runge-Kutta method we need to stop and think about what needs to happen throughout the code. We should ask ourselves the following kinds of questions, do we only want to write the answers y(t n ) at each t n to a file to view them graphically later? Do we need an array that holds the values of the function at each t n so that we can use it in another program, or as a comparision to what we think the answer should be? Once you have decided on all of the factors, you can successfully write your differential equation solver. Be warned though, it will not be easy to make a very general program, most will be specific to the problem at hand. 3 Your next task 1. Write a program to use the trapezoid method and integrate the following function: 0 (1 exp( τ 0e x2 )). You should first write you trapezoid function or subroutine and check it with an easy function, i.e. x 2 from 0,1. Then modify the function to be the function given above andcheck toseeifyougetthefollowinganswers, forτ 0 = 1,2,10,10000, your answer for the integral should be: INT = 0.6425, 0.986344, 1.65688, 3.12214. 2. Write a program that will solve the following differential equations: you should start by writing a mini-program that contains all of the elements of the solver, then you can copy and paste the main components into each program. Consider the problem dy = t+y, y(0) = 1. (16) use the Runge-Kutta method, with h = 0.1 to find a solution from t = 0,1. Compare the results you get with the actual solution, given in tabular form below. 5

t y = 2e t 1 t 0.0 1.00000 00 0.1 1.11034 18 0.2 1.24280 55 0.3 1.39971 76 0.4 1.58364 94 0.5 1.79744 25 0.6 2.04423 76 3. Writing the the acceleration of a mass under the influence of Hooke s Law we get the following second order differential equation. x +ω 2 x = 0 (17) where ω 2 = k. We already know the answer, upto normalization constants and phase angles. The general solution m is x = C 1 cosωt+c 2 sinωt (18) The initial conditions determine which function cos or sin gives the final answer. We will let ω = 1. Then we have the equation d ( x +x = 0 ) +x = 0 (19) Here I have rewitten the equation to show the dependence on t. To use the Euler or Runge-Kutta method, we need to make this second order equation into a first order equation. So, let s define y = /. Then we get the new set of equations, dy = ωx = y (20) Now we have a set of two coupled first order differential equations, which we can solve using the Runge-Kutta method. You should be able to modify the program you wrote to solve number (1) to solve this problem. Use h = 0.1,0.01 with y(0) = 1, x(0) = 0 over the range t = 0,2π, and compare the answer you get to (sint) evaluated at each t. 6

I have included my programs in my website, named rungtest.f and rung.f, that solve problems (1) and (2) respectively, along with the data files that were output by the programs, rungtest.dat and rung.dat, respectively. Feel free to look at them and get ideas on how to write your programs. 7