Goals for This Lecture:

Similar documents
Introduction to Scientific Computing Lecture 8

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

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

Physics Tutorial 2: Numerical Integration Methods

Mass-Spring Systems. Last Time?

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

Differential equations for chemical kinetics

Goals for This Lecture:

Introduction to Scientific Computing Lecture 8

MATH2071: LAB 2: Explicit ODE methods

Finite difference methods

Application 7.6A The Runge-Kutta Method for 2-Dimensional Systems

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

37 Self-Assessment. 38 Two-stage Runge-Kutta Methods. Chapter 10 Runge Kutta Methods

AMath 483/583 Lecture 8

dy = y, dt y(0) = 1, y(t) = e t.

Review More Arrays Modules Final Review

Goals for This Lecture:

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

An Introduction to Flow Visualization (1) Christoph Garth

Modeling Cloth Using Mass Spring Systems

Ordinary Differential Equations

Subroutines and Functions

Physically Based Simulation

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

Physically Based Simulation

CS 342 Lecture 6 Scheme Procedures and Closures By: Hridesh Rajan

ExactPack: A Master Code of Exact Solutions for Code Verification

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

7. Procedures and Structured Programming

Numerical Modelling in Fortran: day 6. Paul Tackley, 2017

Goals for This Lecture:

This lecture presents ordered lists. An ordered list is one which is maintained in some predefined order, such as alphabetical or numerical order.

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

Mathematics for chemical engineers

Epidemic spreading on networks

An introduction to Fortran. Daniel Price School of Physics and Astronomy Monash University Melbourne, Australia

Part I: Theoretical Background and Integration-Based Methods

Structure and Synthesis of Robot Motion

16.1 Runge-Kutta Method

Skåne University Hospital Lund, Lund, Sweden 2 Deparment of Numerical Analysis, Centre for Mathematical Sciences, Lund University, Lund, Sweden

AN INTRODUCTION TO FORTRAN 90 LECTURE 2. Consider the following system of linear equations: a x + a x + a x = b

Numerical Integration

Design patterns and Fortran 2003

Example 1: DC/DC Converter

CS205b/CME306. Lecture 9

Chapter 6 Visualization Techniques for Vector Fields

Num. #1: Recalls on Numerical Methods for Linear Systems and Ordinary differential equations (ODEs) - CORRECTION

Module 29.1: nag tsa identify Time Series Analysis Identification. Contents

Introduction to Fortran Programming. -Internal subprograms (1)-

Numerical Modelling in Fortran: day 2. Paul Tackley, 2017

Modeling Discrete and Continuous Processes

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

GPUs and Einstein s Equations

Goals for This Lecture:

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

Goals for This Lecture:

Computational Astrophysics AS 3013

Homework Set 5 (Sections )

Module 5.5: nag sym bnd lin sys Symmetric Banded Systems of Linear Equations. Contents

Computational Methods of Scientific Programming

There are functions to handle strings, so we will see the notion of functions itself in little a detail later. (Refer Slide Time: 00:12)

Data Visualization. Fall 2017

Introduction to Modern Fortran

CS1 Lecture 22 Mar. 6, 2019

Lecture V: Introduction to parallel programming with Fortran coarrays

PACKAGE SPECIFICATION HSL 2013

SUBPROGRAMS AND MODULES

Module 28.3: nag mv rotation Rotations. Contents

Cloth Simulation. COMP 768 Presentation Zhen Wei

The jello cube. Undeformed cube. Deformed cube

PROBLEM SOLVING WITH FORTRAN 90

Mathematical Computing

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

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

Math 125 Little Book Homework Chapters 7, 10, 11, and 12

Lecture 19: Functions, Types and Data Structures in Haskell

Discrete Element Method

Compiling, running the code, generating output. patrick]$ ifort -o prog1 prog1.f patrick]$ prog1

Subroutines, Functions and Modules

INF 212 ANALYSIS OF PROG. LANGS PROCEDURES & FUNCTIONS. Instructors: Kaj Dreef Copyright Instructors.

An Interesting Way to Combine Numbers

Dynamic Computation of Runge-Kutta s Fourth-Order Algorithm for First and Second Order Ordinary Differential Equation Using Java

4 Visualization and. Approximation

Minimization algorithm in the simulation of the Wall Touching Kink Modes

1. User-Defined Functions & Subroutines Part 2 Outline

Cardinality of Sets. Washington University Math Circle 10/30/2016

Lecture Notes for Chapter 2: Getting Started

Intro to Igor Pro: Beyond Getting Started (Adapted from Ingrid Ulbrich 2009 lecture)

Manual for explicit parallel peer code EPPEER

Precision Spin Tracking for Electric Dipole Moment Searches

Math 225 Scientific Computing II Outline of Lectures

The Level Set Method. Lecture Notes, MIT J / 2.097J / 6.339J Numerical Methods for Partial Differential Equations

Allocating Storage for 1-Dimensional Arrays

Physically based modelling Computer Graphics I February 27, 2003

6.001 Notes: Section 4.1

NO CALCULATOR ALLOWED!!

5.5 Completing the Square for the Vertex

1. User-Defined Functions & Subroutines Part 1 Outline

Nested Loops. A loop can be nested inside another loop.

Transcription:

Goals for This Lecture: Understand why Euler s method sometimes gives inaccurate answers Develop some more accurate ODE integration methods Develop general purpose ODE codes Compare these methods on the orbital problem

Euler s Method for Coupled Systems of ODEs Let us develop a shorthand notation for the time at the nth step t n we will denote y i (t n ) as y i n Then we can approximate the derivatives as: Thus Euler s method for a set of couple ODEs is: Exercise: Convince yourself that the orbital equations fit the form described in the previous slide and that the numerical method we used fits into the form shown above

Implementing a problem independent Euler s Method Code We can implement a generic Euler s method code that will work on any system of coupled first order ODEs if we undertake the following steps Treat the solution {y i, i=1 N} as a 1-D array (a vector) of length N Have the user supply the number of equations N=neqns to the code via a subroutine named prob_size(neqns) Have the user supply the initial conditions including the initial time t, the timestep size, dt, the time to stop integrating, tstop, and the initial value of the solution vector, y, to the code via a subroutine named prob_init_conds(t,dt,tstop,y) Have the user supply the right-hand-side for each of the equations via a subroutine that takes in t & y as inputs and returns a vector rhs containing the right-hand-sides. We will call this subroutine prob_rhs(t,y,rhs)

Implementing a problem independent Euler s Method Code! Purpose: Integrate a set of coupled ODEs with Euler's method! Note: User must supply subroutines prob_size,prob_init_conds, and prob_rhs! program ode_euler implicit none integer :: neqns! Number of equations integer :: i! Implicit DO-loop index real(kind=kind(1.0d0)), allocatable :: y(:), rhs(:)! y & r.h.s. real(kind=kind(1.0d0)) :: t, tstop, dt! Time, Stopping time, & timestep call prob_size(neqns)! Get # of equations allocate(y(neqns))! Allocate y & ynew arrays to correct size allocate(rhs(neqns)) call prob_init_conds(t,tstop,dt,y)! Get initial conditions open(unit=17,file='results.dat')! Open a file for output do while(t < tstop)! Integrate forward in time call prob_rhs(t,y,rhs)! Evaluate right-hand-side of ODEs y = y+dt*rhs! Apply Euler's method t = t+dt! Increment time write(17,'(t2,20(1x,es12.5))') t,(y(i),i=1,neqns)! Write out results enddo close(unit=17) stop end program ode_euler

The prob_size subroutine! Purpose: Return the number of equations for! the orbital problem (through the argument! neqns) subroutine prob_size(neqns)! Number of equations implicit none integer, intent(out) :: neqns neqns = 4! Initialize this to 4 return end subroutine prob_size

The prob_init_conds subroutine! Purpose: Return the initial conditions for! the orbital problem subroutine prob_init_conds(t,tstop,dt,y) implicit none real(kind=kind(1.0d0)), intent(out) :: t, tstop, dt, y(4) t = 0.0d0! Initialize t to zero tstop = 2.0d0! Initialize tstop to two years write(*,'(t2,a20,$)') "Enter timestep size:" read(*,*) dt y(1) = 1.0d0! Initialize x coordinate y(2) = 0.0d0! Initialize y coordinate y(3) = 0.0d0! Initialize x velocity y(4) = 6.29d0! Initialize y velocity return end subroutine prob_init_conds

The prob_rhs subroutine! Purpose: Return the right-hand-side for! the orbital problem subroutine prob_rhs(t,y0,rhs) implicit none real(kind=kind(1.0d0)), intent(in) :: t, y0(4) real(kind=kind(1.0d0)), intent(out) :: rhs(4) real(kind=kind(1.0d0)) :: x, y, vx, vy! Constants in units of solar masses and AU real(kind=kind(1.0d0)), parameter :: grav=39.47, msun=1.0 x = y0(1)! Unpack y0 vector y = y0(2) vx = y0(3) vy = y0(4) rhs(1) = vx! r.h.s. of x position equation rhs(2) = vy! r.h.s. of y position equation rhs(3) = -grav*msun*x/(x**2 + y**2)**1.5d0! r.h.s. of X vel. equation rhs(4) = -grav*msun*y/(x**2 + y**2)**1.5d0! r.h.s. of X vel. equation return end subroutine prob_rhs

More Accurate Methods The problem with Euler s method is that the right-hand-side of the equations is evaluated at the beginning of the timestep. The right-hand-side usually changes over the course of each timestep and we may be getting an inaccurate answer as a result. It would be better if we could evaluate the right-hand-side in the middle of the timestep. However, we can t do that unless we know the solution in advance Strategy: Use Euler s method to estimate the solution at the midpoint of the timestep. And then use this estimate to evaluate the right-hand-side This is called a second order Runge-Kutta method

Coupled Systems of ODEs in Vector Notation In order to simplify the description of the second order Runge-Kutta algorithm we use the following vector notation to simplify the equations Using this notation the original set of ODEs is: In this notation Euler s method is:

The Second-order Runge-Kutta Method In vector notation we can easily describe the second-order Runge-Kutta algorithm as follows: Step 1: Step 2: Code for this algorithm is an easy modification from our Euler s method code

The Second Order Runge-Kutta Code program ode_rk2 implicit none integer :: neqns! Number of equations integer :: i! Implicit DO-loop index real(kind=kind(1.0d0)), allocatable :: y(:), k1(:), rhs(:)! y, y1, & r.h.s real(kind=kind(1.0d0)) :: t, tstop, dt! Time, Stopping time, & timestep call prob_size(neqns)! Get # of equations allocate(y(neqns))! Allocate y & ynew arrays to correct size allocate(k1(neqns)) allocate(rhs(neqns)) call prob_init_conds(t,tstop,dt,y)! Get initial conditions open(unit=17,file='results.dat')! Open a file for output do while(t < tstop)! Integrate forward in time call prob_rhs(t,y,rhs)! Evaluate right-hand-side of ODEs k1 = dt*rhs! Apply first step call prob_rhs(t+0.5d0*dt,y+0.5d0*k1,rhs)! Evaluate right-hand-side at midpoint y = y+dt*rhs! Apply first step t = t+dt! Increment time write(17,'(t2,20(1x,es12.5))') t,(y(i),i=1,neqns)! Write out results enddo close(unit=17) stop end program ode_rk2

Second-order Runge-Kutta vs. Euler s Method Let s now compare the second-order Runge-Kutta and Euler s Method on the orbital problem Use a time step of t = 0.003 years Second-order RK Euler s Method Clearly the 2 nd -order Runge-Kutta does a lot better

The Fourth-order Runge-Kutta Method In practice, the workhorse algorithm for first-order sets of ODEs is the fourth-order Runge-Kutta algorithm which we state here without derivation Step 1: Step 2: Step 3: Step 4: Step 5: Code for this algorithm is an easy modification from our 2ndorder Runge-Kutta code

The Fourth Order Runge-Kutta Code program ode_rk4 implicit none integer :: neqns! Number of equations integer :: i! Implicit DO-loop index real(kind=kind(1.0d0)), allocatable :: y(:), rhs(:)! y, & r.h.s real(kind=kind(1.0d0)), allocatable :: k1(:), k2(:), k3(:), k4(:) real(kind=kind(1.0d0)) :: t, tstop, dt! Time, Stopping time, & timestep call prob_size(neqns)! Get # of equations allocate(y(neqns))! Allocate y & k arrays to correct size allocate(k1(neqns), k2(neqns), k3(neqns),k4(neqns),rhs(neqns)) call prob_init_conds(t,tstop,dt,y)! Get initial conditions open(unit=17,file='results.dat')! Open a file for output do while(t < tstop)! Integrate forward in time call prob_rhs(t,y,rhs)! Evaluate right-hand-side of ODEs for k1 k1 = dt*rhs call prob_rhs(t+0.5d0*dt,y+0.5d0*k1,rhs)! Evaluate right-hand-side of ODEs for k2 k2 = dt*rhs call prob_rhs(t+0.5d0*dt,y+0.5d0*k2,rhs)! Evaluate right-hand-side of ODEs for k3 k3 = dt*rhs call prob_rhs(t+dt,y+k3,rhs)! Evaluate right-hand-side of ODEs for k4 k4 = dt*rhs y = y+(k1+2.0d0*k2+2.0d0*k3+k4)/6.0d0! Apply rk4 method t = t+dt! Increment time write(17,'(t2,20(1x,es12.5))') t,(y(i),i=1,neqns)! Write out results enddo close(unit=17) stop end program ode_rk4

A Comparison of 4 th -order RK, 2 nd -order RK, and Euler s Method Let s now compare the two R-K Methods and Euler s Method on the orbital problem Use a time step of t = 0.05 years Fourth-order RK Second-order RK Euler s Method The superiority of the 4th-order Runge-Kutta is apparent We ll code this in C++ soon!

Assignment Read sections 3.1-3.2 of Savitch