Research Computing with Python, Lecture 7, Numerical Integration and Solving Ordinary Differential Equations

Similar documents
Fourth Order RK-Method

Solving PDEs with PGI CUDA Fortran Part 4: Initial value problems for ordinary differential equations

ECE257 Numerical Methods and Scientific Computing. Ordinary Differential Equations

Chapter 6 - Ordinary Differential Equations

Numerical integration and differentiation. Unit IV. Numerical Integration and Differentiation. Plan of attack. Numerical integration.

Physics 584 Computational Methods

Differential Equations

Lecture 8: Calculus and Differential Equations

Lecture 8: Calculus and Differential Equations

Numerical Methods - Initial Value Problems for ODEs

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

Lecture Notes to Accompany. Scientific Computing An Introductory Survey. by Michael T. Heath. Chapter 9

Ordinary Differential Equations II

AM205: Assignment 3 (due 5 PM, October 20)

Ordinary Differential Equations I

Ordinary Differential Equations II

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

Initial value problems for ordinary differential equations

CS520: numerical ODEs (Ch.2)

Consistency and Convergence

Scientific Computing: An Introductory Survey

Integration of Ordinary Differential Equations

Mathematics for chemical engineers. Numerical solution of ordinary differential equations

Introductory Numerical Analysis

HIGHER ORDER METHODS. There are two principal means to derive higher order methods. b j f(x n j,y n j )

Department of Applied Mathematics and Theoretical Physics. AMA 204 Numerical analysis. Exam Winter 2004

Numerical Methods for Initial Value Problems; Harmonic Oscillators

Integration, differentiation, and root finding. Phys 420/580 Lecture 7

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

Differentiation and Integration

Ch. 03 Numerical Quadrature. Andrea Mignone Physics Department, University of Torino AA

In numerical analysis quadrature refers to the computation of definite integrals.

CS 450 Numerical Analysis. Chapter 9: Initial Value Problems for Ordinary Differential Equations

Multistep Methods for IVPs. t 0 < t < T

Ordinary Differential Equations

Lecture 4: Numerical solution of ordinary differential equations

NUMERICAL SOLUTION OF ODE IVPs. Overview

Numerical Analysis. A Comprehensive Introduction. H. R. Schwarz University of Zürich Switzerland. with a contribution by

Numerical Differential Equations: IVP

CS 257: Numerical Methods

Review. Numerical Methods Lecture 22. Prof. Jinbo Bi CSE, UConn

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

Virtual University of Pakistan

Math 128A Spring 2003 Week 12 Solutions

Graded Project #1. Part 1. Explicit Runge Kutta methods. Goals Differential Equations FMN130 Gustaf Söderlind and Carmen Arévalo

Chapter 10. Initial value Ordinary Differential Equations

NUMERICAL METHODS FOR ENGINEERING APPLICATION

Section 6.6 Gaussian Quadrature

CS 450 Numerical Analysis. Chapter 8: Numerical Integration and Differentiation

Applied Numerical Analysis

Ordinary Differential Equations. Monday, October 10, 11

Ordinary Differential Equations: Initial Value problems (IVP)

2.29 Numerical Fluid Mechanics Fall 2011 Lecture 20

Numerical Methods for Initial Value Problems; Harmonic Oscillators

Ordinary Differential Equations

Differential Equations

Ordinary Differential Equations (ODEs)

Part IB Numerical Analysis

Ordinary differential equations - Initial value problems

Chapter 8. Numerical Solution of Ordinary Differential Equations. Module No. 2. Predictor-Corrector Methods

Name of the Student: Unit I (Solution of Equations and Eigenvalue Problems)

Bindel, Fall 2011 Intro to Scientific Computing (CS 3220) Week 12: Monday, Apr 18. HW 7 is posted, and will be due in class on 4/25.

Linear Multistep Methods I: Adams and BDF Methods

Ordinary Differential Equations I

Introduction to ODEs in Matlab. Jeremy L. Marzuola Charles Talbot Benjamin Wilson

Exam in TMA4215 December 7th 2012

Numerical solution of ODEs

Unit I (Testing of Hypothesis)

I. Numerical Computing

Ordinary Differential Equations I

Math 411 Preliminaries

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

The Chemical Kinetics Time Step a detailed lecture. Andrew Conley ACOM Division

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

Fixed point iteration and root finding

SYSTEMS OF ODES. mg sin ( (x)) dx 2 =

Bindel, Spring 2012 Intro to Scientific Computing (CS 3220) Week 12: Monday, Apr 16. f(x) dx,

COSC 3361 Numerical Analysis I Ordinary Differential Equations (II) - Multistep methods

Solution of ordinary Differential Equations

COURSE Numerical integration of functions (continuation) 3.3. The Romberg s iterative generation method

Lecture V: The game-engine loop & Time Integration

APPLICATIONS OF FD APPROXIMATIONS FOR SOLVING ORDINARY DIFFERENTIAL EQUATIONS

16.7 Multistep, Multivalue, and Predictor-Corrector Methods

Multistage Methods I: Runge-Kutta Methods

The family of Runge Kutta methods with two intermediate evaluations is defined by

MATHEMATICAL METHODS INTERPOLATION

Computational Methods CMSC/AMSC/MAPL 460. Ordinary differential equations

NATIONAL UNIVERSITY OF SINGAPORE PC5215 NUMERICAL RECIPES WITH APPLICATIONS. (Semester I: AY ) Time Allowed: 2 Hours

Ordinary differential equation II

Simple ODE Solvers - Derivation

Lecture 9 Approximations of Laplace s Equation, Finite Element Method. Mathématiques appliquées (MATH0504-1) B. Dewals, C.

NUMERICAL MATHEMATICS AND COMPUTING

M.SC. PHYSICS - II YEAR

Logistic Map, Euler & Runge-Kutta Method and Lotka-Volterra Equations

Review for Exam 2 Ben Wang and Mark Styczynski

Index. higher order methods, 52 nonlinear, 36 with variable coefficients, 34 Burgers equation, 234 BVP, see boundary value problems

A Variable-Step Double-Integration Multi-Step Integrator

Numerical Integration

Numerical integration and importance sampling

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

Transcription:

Research Computing with Python, Lecture 7, Numerical Integration and Solving Ordinary Differential Equations Ramses van Zon SciNet HPC Consortium November 25, 2014 Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, and 2014Solving1 Ordinar / 30

Today s Lecture Numerical Integration Ordinary Differential Equations Little bit of theory How to do this in Python (spoiler: use scipy.integrate) Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, and 2014Solving2 Ordinar / 30

Numerical Integration Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, and 2014Solving3 Ordinar / 30

Numerical Integration I = D f(x) d d x amses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, and 2014Solving4 Ordinar / 30

Numerical Integration Methods If our integral cannot be computed exactly, what options do we have? Method depends on dimension d, function f(x), and x-domain. I D f(x)d d x d=1: d small: d >> 1: Regular grid Gaussian Quadrature Regular grid Recursive Quadrature Monte Carlo amses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, and 2014Solving5 Ordinar / 30

Regularly spaced grid methods Problem: A curve is given by an function y=f(x). The area under the curve is required, between a and b. Numerical approach: Compute the value of y at equally space points x Using an interpolation function between those points, compute area In the figure: Linear interpolation: trapezoidal rule The shaded area is returned by this approach This is an approximation to the actual area. amses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, and 2014Solving6 Ordinar / 30

Equally spaced grid approach Compute the value of y at equally space points x Trapezoidal rule: Example I = 10 0 cos x 9 sin2 x dx [ n 1 1 I = 2 y 1 + y i + 1 ] 2 y n x i=2 def f(x): return cos(x/9)*sin(x)**2 a=0 b=10 x=linspace(a,b,40) dx=x[1]-x[0] y=f(x) I1=(y[0]+y[-1])/2+sum(y[1:-1]) I1=I1*dx print I1 3.93845493792 amses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, and 2014Solving7 Ordinar / 30

Different evenly spaced grid approaches Trapezoidal Simpson a+2h a a+h a f(x) dx h [f(a) + f(a + h)] 2 [ 1 f(x) dx h 3 f(a) + 4 3 f(a + h 2 ) + 1 ] f(a + h) 3 Bode, Backward differentiation,... Different prefactors, different orders, different points What you use is the extension of these rules to multiple intervals. Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, and 2014Solving8 Ordinar / 30

Unevenly spaced grids Gaussian quadrature Based on orthogonal polynomials on the interval. E.g. Legendre, Chebyshev, Hermite, Jacobi polynomials Compute and y i = f(x i ) then b a f(x) dx n v i f i i=1 x i and v i from polynomial properties Tend to be more accurate than equally spaced approaches # nth order Gauss-Legendre quadrature: from scipy.integrate import fixed_quad I2=fixed_quad(f,a,b,n=20)[0] print I2 3.9363858769075524 Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, and 2014Solving9 Ordinar / 30

Accuracy Was this the right value? Always an approximation More points means better approxiation If curve is smooth, better interpolation means better approximation (why unevenly spaced points helps) But how close are we? Adaptive Integration Rather than choosing a safe large number of points, we should increase number of points until a given accuracy is achieved Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 10 Ordinar / 30

Adaptive Integration #Adaptive Gauss-Legendre integration from scipy.integrate import quad I3=quad(f,a,b,epsrel=0.001) print I3 (3.936385876907544, 0.0009622632189420763) Arguments of interest for quad f: The function a,b: The x limits epsabs: Absolute error tolerance. epsrel: Relative error tolerance. limit : An upper bound on the number of subintervals used in the adaptive algorithm. Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 11 Ordinar / 30

Numerical Integration in d > 1 but small Why multidimensional integration is hard: Requires O(n d ) points if its 1d counterpart requires n. A function can be peaked, and peak can easily be missed. The domain itself can be complicated. Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 12 Ordinar / 30

Numerical Integration in d > 1 but small So what should you do? If you can reduce the d by exploiting symmetry or doing part of the integral analytically, do it! If you know the function to integrate is smooth and its domain is fairly simple, you could do repeated 1d integrals (fixed-grid or Gaussian quadrature) Otherwise, you ll have to consider Monte Carlo. Example 3.14 3.14 x=0 y=0 xy dx dy from scipy.integrate \ import dblquad def f(x,y): return x*y def y1(x): return 0 def y2(x): return 3.14 a=0; b=3.14 I4=dblquad(f,a,b,y1,y2) print I4[0] 24.30292804 amses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 13 Ordinar / 30

Monte Carlo Integration Use random numbers to pick points at which to evaluate integrand. Convergence always as 1/ n regardless of d. Simple and flexible. Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 14 Ordinar / 30

Monte Carlo Integration You can find python packages for MC (not in scipy, though) But the essence is the same: 1 Use random numbers to generate points in your domain 2 Evaluate the function on those points 3 Average them and compute standard deviation for error. One variation is to use a bias in step 1 to focus on regions of interest. Bias can be undone in averaging step Another variation is to have each point generated from the previous one plus a random component: MC chain. Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 15 Ordinar / 30

Ordinary Differential Equations Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 16 Ordinar / 30

Ordinary Differential Equations (ODE) Lotka Volterra dx dt = x(α βy) dy = y(γ δx) dt Preditor-pray model Rate equations dx dt = 2k 1x 2 y + 2k 2 z 2 Harmonic oscillator Pendulum Lorenz system dx dt = y dy dt = x dy dt = k 1x 2 y + k 2 z 2 dy = x(ρ z) y dt dz dt = 2k 1x 2 y 2k 2 z 2 dz = xy βz dt Chemical reactions Simplified atmospheric convection amses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 17 Ordinar / 30 dx dt = σ(y x)

Mathematical Details General form: N n=0 N=order a n (t, y) dn y = f(t, y) dtn Boundary conditions: much like PDEs: next lecture Initial conditions: y, dy/dt,..., at t = t 0 Define y 0 = y; y 1 = dy/dx,..., gives a set of first order ODEs amses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 18 Ordinar / 30

First order initial value problem Start from the general first order form: dy dt = f(t, y) t is one dimensional, y can have multiple components All approaches will evaluate f at discrete points t 0, t 1,... Like integration: y n+1 = y n + t+h t f(t, y(t ))dt Consecutive points may have a fixed step size h = x k+1 x k or may be adaptive. y n+1 may be implicitly dependent on f(y n+1 ). Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 19 Ordinar / 30

Stiff ODEs A stiff ODE is one that is hard to solve, i.e. requiring a very small stepsize h or leading to instabilities in some algoritms. Usually due to wide variation of time scales in the ODEs. Not all methods equally suited for stiff ODEs. Implicit ones tend to be better for stiff problems. Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 20 Ordinar / 30

ODE solver algorithms: Euler To solve: dy = f(t, y) dt Simple approximation: y n+1 y n + hf(t n, y n ) forward Euler Rationale: So: y(t n + h) = y(t n ) + h dy dt (t n) + O(h 2 ) y(t n + h) = y(t n ) + hf(t n, y n ) + O(h 2 ) O(h 2 ) is the local error. For given interval [t 1, t 2 ], there are n = (t 2 t 1 )/h steps Global error: no(h 2 ) = O(h) Not very accurate, nor very stable (next): don t use. Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 21 Ordinar / 30

Stability Example: solve harmonic oscillator numerically: dx dt = y dy dt = x Using Euler gives ( ) ( ) ( xn+1 1 h xn = h 1 y n+1 y n ) Stability: eigenvalues of that matrix: λ ± = 1 ± ih λ ± = 1 + h 2 > 1 Unstable for any h! amses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 22 Ordinar / 30

ODE algorithms: implicit mid-point Euler To solve dy dt Symmetric simple approximation: = f(t, y) y n+1 y n + hf(x n, (y n + y n+1 )/2) mid-point Euler This is an implicit formula, i.e., has to be solved for y n+1. Example: Harmonic oscillator ( 1 h ) ( ) y [1] 2 n+1 h 1 2 y [2] = n+1 ( y [1] n+1 y [2] n+1 ) = M Stable! ( y [1] n y [2] n ( 1 h 2 h 2 1 ) ) ( y [1] n y [2] n ) Eigenvalues M are λ± = (1 ± ih/2)2 1 + h 2 /4 λ ± = 1 amses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 23 Ordinar / 30

ODE solver algorithms: Predictor-Corrector Computation of new point Correction using that new point Gear P.C.: keep previous values of y to do higher order Taylor series (predictor), then use f in last point to correct. Can suffer from catestrophic cancellation at very low h. Adams: Similarly uses past points to compute. Runge-Kutta: Refines by using mid-points. Some schemes require correction until convergence. Some schemes can use the jacobian, e.g. the derivatives of the right hand side. Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 24 Ordinar / 30

Further ODE solver techniques Adaptive methods: As with the integration, rather than taking a fixed h, vary h such that the solution has a certain accuracy. Don t code this yourself! Good schemes are implemented in packages such as scipy.integrate.odeint, scipy.integrate.ode odeint uses an Adams integrator for non-stiff problems, and a backwards differentiation method for stiff problem. ode is a bit more flexible. Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 25 Ordinar / 30

Lotka Volterra using scipy.integrate.odeint dx = x(α βy) dt dy = y(γ δx) dt from scipy.integrate\ import odeint alpha=0.1 beta=0.015 gamma=0.0225 delta=0.02 def system(z,t): x,y=z[0],z[1] dxdt= x*(alpha-beta*y) dydt=-y*(gamma-delta*x) return [dxdt,dydt] t=linspace(0,300.,1000) x0,y0=1.0,1.0 sol=odeint(system,[x0,y0],t) X,Y=sol[:,0],sol[:,1] plot(x,y) amses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 26 Ordinar / 30

Conclusions Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 27 Ordinar / 30

Conclusions Many different methods for numerical integration and solving ODEs Python package scipy.integrate helps you out. It has procedures to readily get good results: scipy.integrate.quad and scipy.integrate.odeint Unfortunately, hard to get what they really do: For scipy.integrate.quad, had to look into the scipy python source to know that it uses Legendre polynomials. For scipy.integrate.odeint, had to look into the fortran documentation If you re using scipy for anything but exploration: do you research and learn what they really do! Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 28 Ordinar / 30

Next Time Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 29 Ordinar / 30

Next and Final Lecture Thursday November 27, 2014, 11:00 am Topic: Partial differential equations Different location McLennan Physical Laboratories MP134 60 St. George Street Ramses van Zon (SciNet HPC Consortium)Research Computing with Python, Lecture 7, Numerical November Integration 25, 2014 and Solving 30 Ordinar / 30