Queens College, CUNY, Department of Computer Science Numerical Methods CSCI 361 / 761 Spring 2018 Instructor: Dr. Sateesh Mane.

Similar documents
Euler s Methods (a family of Runge- Ku9a methods)

over The idea is to construct an algorithm to solve the IVP ODE (9.1)

Queens College, CUNY, Department of Computer Science Numerical Methods CSCI 361 / 761 Fall 2018 Instructor: Dr. Sateesh Mane

over The idea is to construct an algorithm to solve the IVP ODE (8.1)

2 Homework: Bond class, fair value, duration & yield

Finite difference methods

ODE IVP. An Ordinary Differential Equation (ODE) is an equation that contains a function having one independent variable:

Mathematics for chemical engineers

Differentiation. The Derivative and the Tangent Line Problem 10/9/2014. Copyright Cengage Learning. All rights reserved.

Ordinary Differential Equations

Queens College, CUNY, Department of Computer Science Numerical Methods CSCI 361 / 761 Spring 2018 Instructor: Dr. Sateesh Mane.

Module 2: Single Step Methods Lecture 4: The Euler Method. The Lecture Contains: The Euler Method. Euler's Method (Analytical Interpretations)

A Study on Numerical Exact Solution of Euler, Improved Euler and Runge - Kutta Method

Direction Fields; Euler s Method

Simulation in Computer Graphics. Particles. Matthias Teschner. Computer Science Department University of Freiburg

4 Visualization and. Approximation

4 Generating functions in two variables

MATH2071: LAB 2: Explicit ODE methods

Review Initial Value Problems Euler s Method Summary

Computer Simulations

Queens College, CUNY, Department of Computer Science Computational Finance CSCI 365 / 765 Fall 2018 Instructor: Dr. Sateesh Mane

1. Suppose that the equation F (x, y, z) = 0 implicitly defines each of the three variables x, y, and z as functions of the other two:

The Jello Cube Assignment 1, CSCI 520. Jernej Barbic, USC

(Part - 1) P. Sam Johnson. April 14, Numerical Solution of. Ordinary Differential Equations. (Part - 1) Sam Johnson. NIT Karnataka.

ODEs occur quite often in physics and astrophysics: Wave Equation in 1-D stellar structure equations hydrostatic equation in atmospheres orbits

Lecture 8: Euler s Methods

16.1 Runge-Kutta Method

Module1: Numerical Solution of Ordinary Differential Equations. Lecture 6. Higher order Runge Kutta Methods

Ordinary differential equations solving methods

Inverse and Implicit functions

1 Motivating Questions. 2 An Illustrating Example. 3 Software Quality. 4 An Example of OOP

Discontinuous Galerkin Sparse Grid method for Maxwell s equations

The jello cube. Undeformed cube. Deformed cube

Asymptotic Error Analysis

and F is an antiderivative of f

Study Guide for Test 2

The Rectangular Coordinate System and Equations of Lines. College Algebra

Double Pendulum. Freddie Witherden. February 10, 2009

CS205b/CME306. Lecture 9

Definition. A Taylor series of a function f is said to represent function f, iff the error term converges to 0 for n going to infinity.

REVIEW I MATH 254 Calculus IV. Exam I (Friday, April 29) will cover sections

x n x n stepnumber k order r error constant C r+1 1/2 5/12 3/8 251/720 abs. stab. interval (α,0) /11-3/10

Homework Set #2-3, Math 475B

ChE 400: Applied Chemical Engineering Calculations Tutorial 6: Numerical Solution of ODE Using Excel and Matlab

Project 1. due date Sunday July 8, 2018, 12:00 noon

Math 133A, September 10: Numerical Methods (Euler & Runge-Kutta) Section 1: Numerical Methods

Mathematical Methods and Modeling Laboratory class. Numerical Integration of Ordinary Differential Equations

Math 225 Scientific Computing II Outline of Lectures

Serial Manipulator Statics. Robotics. Serial Manipulator Statics. Vladimír Smutný

Objectives. Materials

Reals 1. Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method.

Math Analysis Chapter 1 Notes: Functions and Graphs

Objectives. Materials

LECTURE 13, THURSDAY APRIL 1, 2004

Calculus I Review Handout 1.3 Introduction to Calculus - Limits. by Kevin M. Chevalier

Scientific Computing: Interpolation

Let denote the number of partitions of with at most parts each less than or equal to. By comparing the definitions of and it is clear that ( ) ( )

Physics Tutorial 2: Numerical Integration Methods

Background for Surface Integration

ME 261: Numerical Analysis. ME 261: Numerical Analysis

Computers in Engineering Root Finding Michael A. Hawker

Euler s Method for Approximating Solution Curves

Introduction to Programming, Aug-Dec 2008

Classes of Real Numbers 1/2. The Real Line

fractional quantities are typically represented in computers using floating point format this approach is very much similar to scientific notation

METU Mechanical Engineering Department ME 582 Finite Element Analysis in Thermofluids Spring 2018 (Dr. C. Sert) Handout 12 COMSOL 1 Tutorial 3

main() { double gm; gm = (sqrt((double)5) - 1)/2; printf (" The Golden Mean is %11.8f \n", gm); } OUTPUT The Golden Mean is 0.

The Euler and trapezoidal stencils to solve d d x y x = f x, y x

MATH. 2153, Spring 16, MWF 12:40 p.m. QUIZ 1 January 25, 2016 PRINT NAME A. Derdzinski Show all work. No calculators. The problem is worth 10 points.

Functions of Several Variables

13.1. Functions of Several Variables. Introduction to Functions of Several Variables. Functions of Several Variables. Objectives. Example 1 Solution

Math Analysis Chapter 1 Notes: Functions and Graphs

Solution for Euler Equations Lagrangian and Eulerian Descriptions

. Tutorial Class V 3-10/10/2012 First Order Partial Derivatives;...

CS 450 Numerical Analysis. Chapter 7: Interpolation

Hyperbola for Curvilinear Interpolation

Chapter 5 Partial Differentiation

Rendering. A simple X program to illustrate rendering

DOWNLOAD PDF BIG IDEAS MATH VERTICAL SHRINK OF A PARABOLA

Rendering. A simple X program to illustrate rendering

7.1 First-order initial-value problems

Chemical Reaction Engineering - Part 4 - Integration Richard K. Herz,

Chapter 3 Numerical Methods

On the thickness of discontinuities computed by THINC and RK schemes

An Educational Rigid-Body Dynamics Physics Engine TJHSST Senior Research Project Proposal Computer Systems Lab

Finite Element Analysis Prof. Dr. B. N. Rao Department of Civil Engineering Indian Institute of Technology, Madras. Lecture - 24

Constrained Optimization

College Technical Mathematics 1

Math 5BI: Problem Set 2 The Chain Rule

Approximate Solutions to Differential Equations Slope Fields (graphical) and Euler s Method (numeric) by Dave Slomer

First of all, we need to know what it means for a parameterize curve to be differentiable. FACT:

Calculus III. Math 233 Spring In-term exam April 11th. Suggested solutions

Discrete mathematics , Fall Instructor: prof. János Pach

Solution of final examination

CALCULUS MADE EASY - FUNCTIONALITY for the TiNspire CAS

AP Calculus BC Course Description

Surfaces and Integral Curves

Sparse Grids. Lab 1. Discretization

Polynomial Approximation and Interpolation Chapter 4

Scan Conversion. Lines and Circles

Transcription:

Queens College, CUNY, Department of Computer Science Numerical Methods CSCI 361 / 761 Spring 2018 Instructor: Dr. Sateesh Mane c Sateesh R. Mane 2018 16 Lecture 16 May 3, 2018 Numerical solution of systems of ordinary differential equations In this lecture we continue the study of initial value problems. In this lecture we present some higher order numerical integration algorithms. The fourth order Runge Kutta algorithm is one of the most popular of all numerical integration algorithms. 1

16.1 Basic notation We repeat the basic definitions from the previous lecture. Let the system of coupled differential equations be dy dx There are m unknown variables y j, j = 1,..., m. The starting point is x = x 0, and the initial value y 0 is given. = f(x, y). (16.1.1) The above is called the Cauchy problem or initial value problem. Our interest is to integrate eq. (16.1.1) numerically, using steps h i, so x i+1 = x i + h i. The steps h i need not be of equal size. We define y i = y(x i ). We employ the notation y ex i We employ the notation y num i = y ex (x i ) to denote the exact solution. = y num (x i ) to denote the numerical solution. 2

16.2 General remarks on explicit and implicit methods The system of equations is given by eq. (16.1.1). The explicit Euler integration formula for eq. (16.1.1) is given by y exp i+1 = yexp i + h i f(x i, y exp i ). (16.2.1) The implicit Euler integration formula for eq. (16.1.1) is given by y imp i+1 h if(x i+1, y imp i+1 ) = yimp i. (16.2.2) Here we remark on the general structure of higher order integrators. A general higher order explicit integration method has a formula of the form y exp i+1 = yexp i + h φ(x i, y exp i ). (16.2.3) For the explicit Euler method, φ = f, but for a higher order method φ is more complicated than simply f. Nevertheless, in an explicit method φ is a function only of known quantities, i.e. x i and y exp i. A general higher order fully implicit integration method has a formula of the form y imp i+1 h φ(x i+1, y imp i+1 ) = yimp i. (16.2.4) For the implicit Euler method, φ = f, but for a higher order method φ is more complicated than simply f. Nevertheless, in a fully implicit method φ is a function only of quantities at the step i + 1, i.e. x i+1 and y imp i+1. 3

16.3 Local truncation error As surprising as it may sound, let us substitute the exact solution into the numerical integration formula and examine what happens. Logically, we would expect to do things the other way around. We would like to know by how much the numerical solution fails to satisfy the exact system of differential equations. However, we do not know how to calculate the derivative dy num (x)/dx for the numerical solution. However, the numerical integration formula contains only finite differences, i.e. function evaluations and no derivatives. We define the local truncation error u i as follows: u i = y i+1 y i h i φ(x i, y i ). (16.3.1) The definition in eq. (16.3.1) applies for all numerical integration methods. Since we are substituting the exact solution into the numerical integration formula, there is no need to worry about explicit or implicit methods. For an integration method of order k, then u i = O(h k+1 i ). For the explicit Euler method, we saw above that Note that authors vary in their notations and definitions. 1. Some authors shift the index and define the above as u i+1. u i = h2 i 2 y (x i ). (16.3.2) 2. Other authors divide by h and define the local truncation error as u i /h, so they say the magnitude of the local truncation error is O(h k i ). Of course we do not know the exact solution y ex (x). Nevertheless, eq. (16.3.1) can be employed to derive useful theorems. 4

16.4 Midpoint method The midpoint method is our first example of a higher order integration algorithm. In the explicit Euler method, the slope (derivative) dy/dx was estimated numerically by using the value of f at the start of the step x = x i, i.e. f(x i, y i ). In the implicit Euler method, the slope (derivative) dy/dx was estimated numerically by using the value of f at the end of the step x = x i+1, i.e. f(x i+1, y i+1 ). In the midpoint method, the slope (derivative) dy/dx is estimated numerically by using the value of f at the midpoint of the step x = x i + 1 2 h i, i.e. f(x i + 1 2 h i, y(x i + 1 2 h i)). The midpoint integration formula is y mid i+1 = y mid i + h f(x i + 1 2 h i, y(x i + 1 2 h i)). (16.4.1) 1. However, eq. (16.4.1) cannot be used directly, because what is the value of y(x i + 1 2 h i)? 2. We only know the value of y up to x = x i but not at x = x i + 1 2 h i. Hence we perform a prelimary explicit Euler integration step up to the midpoint to obtain y(x i + 1 2 h i) y i + 1 2 h i f(x i, y i ). (16.4.2) We employ eq. (16.4.2) to substitute for y(x i + 1 2 h i) in eq. (16.4.1). This yields the formula for the midpoint method for one integration step. It requires two function evaluations. g 1 = f(x i, y i ), (16.4.3a) ( g 2 = f x i + h i 2, y i + h ) i 2 g 1, (16.4.3b) y i+1 = y i + h i g 2. (16.4.3c) The midpoint method is an explicit integration algorithm. The midpoint method is a second order integration method. The midpoint method is actually a second order Runge Kutta method. We shall discuss Runge Kutta methods below. 5

16.5 Trapezoid method The trapezoid method is another explicit second order numerical integration algorithm. In the trapezoid method, the slope (derivative) dy/dx is estimated numerically as the average of the values of f at the two endpoints of the integration step. This formula for the trapezoid method is as follows. It requires two function evaluations. g 1 = f(x i, y i ), (16.5.1a) g 2 = f(x i + h i, y i + h i g 1 ), (16.5.1b) y i+1 = y i + h i ( ) g1 + g 2. 2 (16.5.1c) The trapezoid method is an explicit integration algorithm. The trapezoid method is a second order integration method. The trapezoid method is also a second order Runge Kutta method. In fact there is a family of second order Runge Kutta methods, parameterized by λ as follows. [ ( y i+1 = y i + h i 1 1 ) f(x i, y i ) + 1 2λ 2λ f( x i + λh i, y i + λh i f(x i, y i ) ) ]. (16.5.2) The value λ = 1 2 yields the midpoint method. The value λ = 1 yields the trapezoid method. 6

16.6 Fourth order Runge Kutta RK4 The fourth order Runge Kutta method is a fourth order explicit integration method. It is one of the most popular, if not the most popular, of all numerical integration methods. It is so important it has its own acronym: RK4. The fourth order Runge Kutta method for one integration step is as follows. It requires four function evaluations. RK4 is an explicit integration algorithm. RK4 is a fourth order integration method. g 1 = f(x i, y i ), (16.6.1a) ( g 2 = f x i + h i 2, y i + h ) i 2 g 1, (16.6.1b) ( g 3 = f x i + h i 2, y i + h ) i 2 g 2, (16.6.1c) g 4 = f(x i + h i, y i + h i g 3 ), (16.6.1d) y i+1 = y i + h i ( ) g1 + 2g 2 + 2g 3 + g 4. 6 (16.6.1e) Compared to the Euler method, it requires only four function evaluations (as opposed to one), but the accuracy is fourth order (as opposed to first order). 7

16.7 More on Runge Kutta Runge Kutta is in fact an infinite family of numerical integraton algorithms. Terminology: A general Runge Kutta method has s stages. Here s is positive integer. Let a be an s s matrix and b and c be arrays of length s. The entries in a, b and c are all constant coefficients. Then we compute a set of s intermediate function valuations as follows ( s ) g j = f x i + c j h i, y i + h i a jk g k. (16.7.1) The integration step is given by the following formula y i+1 = y i + h i k=1 s b j g j. (16.7.2) j=1 The values of the b j sum to unity: s b j = 1. (16.7.3) j=1 The value of c j is given by the consistency condition c j = s a jk. (16.7.4) In general, all practical Runge Kutta methods are required to be consistent. Terminology: explicit, semi-explicit, implicit. k=1 1. A Runge-Kutta method is explicit if a jk = 0 for all k j. 2. A Runge-Kutta method is semi-explicit if a jk = 0 for k > j but there are nonzero coefficients for some values where k = j. 3. A Runge-Kutta method is implicit if there are coefficients a jk 0 for k > j. 4. There are implicit Runge Kutta methods of all orders. 5. The value of a jk need not be positive. Negative values are allowed. The fourth order RK4 method is the best known and most widely used member of the family. It is explicit and has four stages. Historical footnote: 1. Karl Runge was a 19 th century German mathematician. 2. Runge was the father-in-law of Richard Courant, of the Courant Institute of Mathematical Sciences in New York City. 8

16.8 Special case m = 2 The vector notation may be too compressed and difficult to understand. Here are the equations for the case m = 2. To clarify the notation, let the variables be u and v, so ( ) ( ) y1 u y = =. (16.8.1) v Then the coupled first order ordinary differential equations are y 2 du dx = f 1(x, u, v), dv dx = f 2(x, u, v). (16.8.2) Note that both f 1 and f 2 depend on both u and v, in general. If this were not so, eq. (16.8.2) would separate into two disconnected equations. explicit Euler method u i+1 = u i + h f 1 (x i, u i, v i ), v i+1 = v i + h f 2 (x i, u i, v i ). (16.8.3) implicit Euler method u i+1 h f 1 (x i+1, u i+1, v i+1 ) = u i, v i+1 h f 2 (x i+1, u i+1, v i+1 ) = v i. (16.8.4) explicit midpoint method g1 u ( ) = f 1 xi, u i, v i, g1 v ( ) = f 2 xi, u i, v i, g2 u ( = f 1 xi + 1 2 h, u i + 1 2 hgu 1, v i + 1 2 1) hgv, g2 v ( = f 2 xi + 1 2 h, u i + 1 2 hgu 1, v i + 1 2 1) hgv, (16.8.5) u i+1 = u i + h g u 2, v i+1 = v i + h g v 2. 9

explicit trapezoid method g1 u ( ) = f 1 xi, u i, v i, g1 v ( ) = f 2 xi, u i, v i, g2 u ( = f 1 xi + h, u i + hg1 u, v i + hg1) v, g2 v ( = f 2 xi + h, u i + hg1 u, v i + hg1) v, (16.8.6) u i+1 = u i + h 2 (gu 1 + g u 2 ), v i+1 = v i + h 2 (gv 1 + g v 2). explicit Runge Kutta RK4 g1 u ( ) = f 1 xi, u i, v i, g1 v ( ) = f 2 xi, u i, v i, g2 u ( = f 1 xi + 1 2 h, u i + 1 2 hgu 1, v i + 1 2 1) hgv, g2 v ( = f 2 xi + 1 2 h, u i + 1 2 hgu 1, v i + 1 2 1) hgv. g3 u ( = f 1 xi + 1 2 h, u i + 1 2 hgu 2, v i + 1 2 2) hgv, g3 v ( = f 2 xi + 1 2 h, u i + 1 2 hgu 2, v i + 1 2 2) hgv, g4 u ( = f 1 xi + h, u i + hg3 u, v i + hg3) v, g4 v ( = f 2 xi + h, u i + hg3 u, v i + hg3) v, (16.8.7) u i+1 = u i + h 6 (gu 1 + 2g u 2 + 2g u 3 + g u 4 ), v i+1 = v i + h 6 (gv 1 + 2g v 2 + 2g v 3 + g v 4). 10

16.9 C++ code: explicit Euler method 16.9.1 One unknown // f is a function declared elsewhere // y_in is the input value at the start of the integration step // y_out is the output value at the end of the integration step int f(double x, double y, double & g) // compute value of g int Euler_explicit(double x, double h, double y_in, double & y_out) y_out = y_in; double g = 0; int rc = f(x, y_in, g); y_out = y_in + h*g; 11

16.9.2 Multiple unknowns // f is a function declared elsewhere // y_in is the input value at the start of the integration step // y_out is the output value at the end of the integration step int f(int m, double x, const std::vector<double> & y, std::vector<double> & g) // compute value of g int Euler_explicit(int m, double x, double h, std::vector<double> & y_in, std::vector<double> & y_out) int i = 0; y_out = y_in; if ((m < 1) (y_in.size() < m) (y_out.size() < m)) return 1; // fail std::vector<double> g(m, 0.0); int rc = f(m, x, y_in, g); for (i = 0; i < m; ++i) y_out[i] = y_in[i] + h*g[i]; 12

16.10 C++ code: midpoint method 16.10.1 One unknown // f is a function declared elsewhere // y_in is the input value at the start of the integration step // y_out is the output value at the end of the integration step int f(double x, double y, double & g) // compute value of g int midpoint(double x, double h, double y_in, double & y_out) int i = 0; int rc = 0; y_out = y_in; double g1 = 0; double g2 = 0; rc = f(x, y_in, g1); rc = f(x+0.5*h, y_in + 0.5*h*g1, g2); y_out = y_in + h*g2; 13

16.10.2 Multiple unknowns int f(int m, double x, const std::vector<double> & y, std::vector<double> & g) // compute value of g int midpoint(int m, double x, double h, std::vector<double> & y_in, std::vector<double> & y_out) int i = 0; int rc = 0; y_out = y_in; if ((m < 1) (y_in.size() < m) (y_out.size() < m)) return 1; // fail std::vector<double> g1(m, 0.0); std::vector<double> g2(m, 0.0); std::vector<double> y_tmp(m, 0.0); // temporary storage rc = f(m, x, y_in, g1); for (i = 0; i < m; ++i) y_tmp[i] = y_in[i] + 0.5*h*g1[i]; rc = f(m, x+0.5*h, y_tmp, g2); for (i = 0; i < m; ++i) y_out[i] = y_in[i] + h*g2[i]; 14

16.11 C++ code: trapezoid method 16.11.1 One unknown // f is a function declared elsewhere // y_in is the input value at the start of the integration step // y_out is the output value at the end of the integration step int f(double x, double y, double & g) // compute value of g int trapezoid(double x, double h, double y_in, double & y_out) int i = 0; int rc = 0; y_out = y_in; double g1 = 0; double g2 = 0; rc = f(x, y_in, g1); rc = f(x+h, y_in + h*g1, g2); y_out = y_in + 0.5*h*(g1 + g2); 15

16.11.2 Multiple unknowns int f(int m, double x, const std::vector<double> & y, std::vector<double> & g) // compute value of g int trapezoid(int m, double x, double h, std::vector<double> & y_in, std::vector<double> & y_out) int i = 0; int rc = 0; y_out = y_in; if ((m < 1) (y_in.size() < m) (y_out.size() < m)) return 1; // fail std::vector<double> g1(m, 0.0); std::vector<double> g2(m, 0.0); std::vector<double> y_tmp(m, 0.0); // temporary storage rc = f(m, x, y_in, g1); for (i = 0; i < m; ++i) y_tmp[i] = y_in[i] + h*g1[i]; rc = f(m, x+h, y_tmp, g2); for (i = 0; i < m; ++i) y_out[i] = y_in[i] + 0.5*h*(g1[i] + g2[i]); 16

16.12 C++ code: RK4 16.12.1 One unknown // f is a function declared elsewhere // y_in is the input value at the start of the integration step // y_out is the output value at the end of the integration step int f(double x, double y, double & g) // compute value of g int RK4(double x, double h, double y_in, double & y_out) int i = 0; int rc = 0; y_out = y_in; double g1 = 0; double g2 = 0; double g3 = 0; double g4 = 0; rc = f(x, y_in, g1); rc = f(x+0.5*h, y_in + 0.5*h*g1, g2); rc = f(x+0.5*h, y_in + 0.5*h*g2, g3); rc = f(x+h, y_in + h*g3, g4); y_out = y_in + (h/6.0)*(g1 + 2.0*g2 + 2.0*g3 + g4); 17

16.12.2 Multiple unknowns int f(int m, double x, const std::vector<double> & y, std::vector<double> & g) // compute value of g int RK4(int m, double x, double h, std::vector<double> & y_in, std::vector<double> & y_out) int i = 0; int rc = 0; y_out = y_in; if ((m < 1) (y_in.size() < m) (y_out.size() < m)) return 1; // fail std::vector<double> g1(m, 0.0); std::vector<double> g2(m, 0.0); std::vector<double> g3(m, 0.0); std::vector<double> g4(m, 0.0); std::vector<double> y_tmp(m, 0.0); // temporary storage rc = f(m, x, y_in, g1); for (i = 0; i < m; ++i) y_tmp[i] = y_in[i] + 0.5*h*g1[i]; rc = f(m, x+0.5*h, y_tmp, g2); for (i = 0; i < m; ++i) y_tmp[i] = y_in[i] + 0.5*h*g2[i]; rc = f(m, x+0.5*h, y_tmp, g3); for (i = 0; i < m; ++i) y_tmp[i] = y_in[i] + h*g3[i]; rc = f(m, x+h, y_tmp, g4); for (i = 0; i < m; ++i) y_out[i] = y_in[i] + (h/6.0)*(g1[i] + 2.0*g2[i] + 2.0*g3[i] + g4[i]); 18