Wed Jan Improved Euler and Runge Kutta. Announcements: Warm-up Exercise:

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

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

MA 114 Worksheet #01: Integration by parts

Math Week 1 notes

Section Differential Equations: Modeling, Slope Fields, and Euler s Method

Section 7.4 Runge-Kutta Methods

Section 1.3: slope fields and graphs of differential equation solutions: Consider the first order DE IVP for a function y x : y = f x, y, y x 0

AP Calculus BC : The Fundamental Theorem of Calculus

Warm up: Recall we can approximate R b

Numerical Analysis Exam with Solutions

. If y x is a solution to this IVP and if we consider its graph y = y x, then the IC means the graph must pass through the point x 0


Questions. x 2 e x dx. Use Part 1 of the Fundamental Theorem of Calculus to find the derivative of the functions g(x) = x cost2 dt.

1 Systems of First Order IVP

Questions. x 2 e x dx. Use Part 1 of the Fundamental Theorem of Calculus to find the derivative of the functions g(x) = x cost2 dt.

Lab on Taylor Polynomials. This Lab is accompanied by an Answer Sheet that you are to complete and turn in to your instructor.

Math 1132 Practice Exam 1 Spring 2016

Simple ODE Solvers - Derivation

Homework 5 - Solutions

NUMERICAL SOLUTION OF ODE IVPs. Overview

Module 2, Section 2 Solving Equations

Math 132 Summer 2013 Exam 1. 1 sin 3. tan. 2 3 x 2. dx = ln( -2 ) - ln ( -6 ) = ln(2) - ln(6) = ln( 2/6) = ln( 1/3) = ln =

Math Review. Daniel B. Rowe, Ph.D. Professor Department of Mathematics, Statistics, and Computer Science. Copyright 2018 by D.B.

Examples of the Accumulation Function (ANSWERS) dy dx. This new function now passes through (0,2). Make a sketch of your new shifted graph.

Numerical Solution of Differential Equations

Math 1526 Excel Lab 2 Summer 2012

BARUCH COLLEGE MATH 2207 FALL 2007 MANUAL FOR THE UNIFORM FINAL EXAMINATION. No calculator will be allowed on this part.

4x x dx. 3 3 x2 dx = x3 ln(x 2 )

F (x) is an antiderivative of f(x) if F (x) = f(x). Lets find an antiderivative of f(x) = x. We know that d. Any ideas?

MATH 101 Midterm Examination Spring 2009

Math Week 1 notes

Exact and Approximate Numbers:

The Fundamental Theorem of Calculus Part 3

CHAPTER 80 NUMERICAL METHODS FOR FIRST-ORDER DIFFERENTIAL EQUATIONS

Numerical Integration

9.8 APPLICATIONS OF TAYLOR SERIES EXPLORATORY EXERCISES. Using Taylor Polynomials to Approximate a Sine Value EXAMPLE 8.1

Integration. 5.1 Antiderivatives and Indefinite Integration. Suppose that f(x) = 5x 4. Can we find a function F (x) whose derivative is f(x)?

Summer AP Assignment Coversheet Falls Church High School

MA123, Chapter 8: Idea of the Integral (pp , Gootman)

Initial Value Problems for. Ordinary Differential Equations

5.1 Second order linear differential equations, and vector space theory connections.

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

Chapter 1: Preliminaries and Error Analysis

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

Definition: Quadratic equation: A quadratic equation is an equation that could be written in the form ax 2 + bx + c = 0 where a is not zero.

AP Calculus BC Summer Packet 2017

Summer AP Assignment Coversheet Falls Church High School

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

MATH 1014 Tutorial Notes 8

Lecture 21: Numerical Solution of Differential Equations

Day 5 Notes: The Fundamental Theorem of Calculus, Particle Motion, and Average Value

Vector Functions & Space Curves MATH 2110Q

Chapter P Prerequisites

AP Calculus AB Summer Assignment

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

Math Week 1 notes

Computer Problems for Taylor Series and Series Convergence

Math 231 Final Exam Review

10 Numerical Solutions of PDEs

y=5 y=1+x 2 AP Calculus Chapter 5 Testbank Part I. Multiple-Choice Questions

Chapter 2 Solutions of Equations of One Variable

Taylor Series and Series Convergence (Online)

Sections 5.1: Areas and Distances

MATH 1325 Business Calculus Guided Notes

Chapter 6 - Ordinary Differential Equations

Calculus with the TI-89. Sample Activity: Exploration 7. Brendan Kelly

AP Calculus BC. Practice Exam. Advanced Placement Program

Order of convergence. MA3232 Numerical Analysis Week 3 Jack Carl Kiefer ( ) Question: How fast does x n

AP Calculus AB Summer Assignment

Lecture Notes on Numerical Differential Equations: IVP

AP Calculus AB SUMMER ASSIGNMENT. Dear future Calculus AB student

Introductory Numerical Analysis

dx dx x sec tan d 1 4 tan 2 2 csc d 2 ln 2 x 2 5x 6 C 2 ln 2 ln x ln x 3 x 2 C Now, suppose you had observed that x 3

Fundamental Theorem of Calculus

1.5. Analyzing Graphs of Functions. The Graph of a Function. What you should learn. Why you should learn it. 54 Chapter 1 Functions and Their Graphs

Numerical Methods - Initial Value Problems for ODEs

for the initial position and velocity. Find formulas for v t and x t. b) Assuming x 0 = 0 and v 0 O 0, show that the maximum value of x t is x max

4 The Cartesian Coordinate System- Pictures of Equations

4.9 APPROXIMATING DEFINITE INTEGRALS

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

8.4 Integration of Rational Functions by Partial Fractions Lets use the following example as motivation: Ex: Consider I = x+5

PreCalculus. American Heritage Upper School Summer Math Packet

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

Chapter 11 ORDINARY DIFFERENTIAL EQUATIONS

WILEY. Differential Equations with MATLAB (Third Edition) Brian R. Hunt Ronald L. Lipsman John E. Osborn Jonathan M. Rosenberg

Name Class. 5. Find the particular solution to given the general solution y C cos x and the. x 2 y

() Chapter 8 November 9, / 1

Functions and Logarithms

7.1 Indefinite Integrals Calculus

2.1 The Rectangular Coordinate System

Solutions to the Exercises of Chapter 5

First In-Class Exam Solutions Math 246, Professor David Levermore Thursday, 20 September 2018

Math Exam 1a. c) lim tan( 3x. 2) Calculate the derivatives of the following. DON'T SIMPLIFY! d) s = t t 3t

MATH 108 REVIEW TOPIC 6 Radicals

One Solution Two Solutions Three Solutions Four Solutions. Since both equations equal y we can set them equal Combine like terms Factor Solve for x

8/6/2010 Assignment Previewer

171S1.1 Graphs, Functions, Models August 23, 2012

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

Prerequisites for Calculus

Transcription:

Wed Jan 31 2.5-2.6 Improved Euler and Runge Kutta. Announcements: Warm-up Eercise:

2.5-2.6 Improved Euler and Runge Kutta In more complicated differential equations it is a very serious issue to find relatively efficient ways of approimating solutions. An entire field of mathematics, ``numerical analysis'' deals with such issues for a variety of mathematical problems. Our tet eplores improvements to Euler in sections 2.5 and 2.6, in particular it discusses improved Euler, and Runge Kutta. Runge Kutta-type codes are actually used in commerical numerical packages, e.g. in Wofram, Matlab, Maple etc. Let's summarize some highlights from 2.5-2.6. Suppose we already knew the solution y to the initial value problem y = f, y y 0 = y 0. If we integrate the DE from to h and apply the Fundamental Theorem of Calculus, we get h y h y = f t, y t dt, i.e. y h = y h f t, y t dt. One problem with Euler is that we approimate this integral above by h f, y, i.e. we use the value at the left-hand endpoint as our approimation of the integrand, on the entire interval from to h. This causes errors that are larger than they need to be, and these errors accumulate as we move from subinterval to subinterval and as our approimate solution diverges from the actual solution. The improvements to Euler depend on better approimations to the integral. These are subtle, because we don't yet have an approimation for y t when t is greater than, so also not for the integrand f t, y t on the interval, h.

Improved Euler uses an approimation to the Trapezoid Rule to compute the integral in the formula h y h = y f t, y t dt. Recall, the trapezoid rule to approimate the integral h f t, y t would be 1 h f, y f h, y h. 2 Since we don't know y h we approimate its value with unimproved Euler, and substitute into the formula above. This leads to the improved Euler "pseudocode" for how to increment approimate solutions. Improved Euler pseudocode: k = f, y 1 j j #current slope k = f 2 j h, y j h k 1 #use unimproved Euler guess for y j h to estimate slope function when = j h k = 1 2 k k #average of two slopes 1 2 = h #increment j 1 j y j 1 = y j h k #increment y dt Eercise 1 Contrast Euler and Improved Euler for our IVP y = y y 0 = 1 to estimate y 1 e with one step of size h = 1. Your computations should mirror the picture below. Note that with improved Euler we actually get a better estimate for e with ONE step of size 1 than we did with 5 steps of size h = 0.2 using unimproved Euler on Tuesday. (It's still not a very good estimate.)

In the same vein as ``improved Euler'' we can use the Simpson approimation for the integral for y instead of the Trapezoid rule, and this leads to the Runge-Kutta method. You may or may not have talked about Simpson's Parabolic Rule for approimating definite integrals in Calculus. It is based on a parabolic approimation to the integrand, whereas the Trapezoid rule is based on an approimationg of the graph with trapezoids, on small subintervals. Simpson's Rule: Consider two numbers 0 1, with interval width h = 1 0 and interval midpoint = 1 2 0 If you fit a parabola p to the three points, g 0 0,, g,, g 1 1 then 1. 1 p t dt = h 6 g 0 4 g g 1. 0 (You will check this fact in your homework this week!) This formula is the basis for Simpson's rule, which you can also review in your Calculus tet or at Wikipedia. (Wikipedia also has a good entry on Runge-Kutta.) Applying the Simpson's rule approimation for our DE, and if we already knew the solution function y, we would have h y h = y f t, y t dt h h y f, y 4 f 6 2, y h f h, y h. 2 However, we have the same issue as in Trapezoid - that we've only approimated up to y so far. Here's the magic pseudo-code for how Runge-Kutta takes care of this. (See also section 2.6 to the tet.) Runge-Kutta pseudocode: k 1 = f j, y j k 2 = f j k 3 = f j # left endpoint slope h h 2, y j 2 k 1 h h 2, y j 2 k 2 # first midpoint slope estimate, using k to increment y 1 j # second midpoint slope estimate using k to increment y 2 j k 4 = f j h, y j h k 3 # right hand slope estimate, using k 3 to increment y j k = 1 6 k 1 2 k 2 2 k 3 k 4 #weighted average of all four slope estimates, consistent with Simpson's rule j = 1 j h # increment y j = y 1 j h k # increment y

Eercise 2 Contrast Euler and Improved Euler to Runge-Kutta for our IVP y = y y 0 = 1 to estimate y 1 e with one step of size h = 1. Your computations should mirror the picture below. Note that with Runge Kutta you actually get a better estimate for e with ONE step of size h = 1 than you did with 100 steps of size h = 0.01 using unimproved Euler! Runge-Kutta pseudocode: k 1 = f j, y j k 2 = f j k 3 = f j # left endpoint slope h h 2, y j 2 k 1 h h 2, y j 2 k 2 # first midpoint slope estimate, using k to increment y 1 j # second midpoint slope estimate using k to increment y 2 j k 4 = f j h, y j h k 3 # right hand slope estimate, using k 3 to increment y j k = 1 6 k 1 2 k 2 2 k 3 k 4 #weighted average of all four slope estimates, consistent with Simpson's rule j = 1 j h # increment y j = y 1 j h k # increment y

Numerical eperiments Estimate e by estimating y 1 for the solution to the IVP y = y y 0 = 1. Apply Runge-Kutta with n = 10, 20, 40... subintervals, successively doubling the number of subintervals until you obtain the target number below - rounded to 9 decimal digits - twice in succession. > evalf e ; 2.718281828459045 (10) It worked with n = 40: Initialize > restart : # clear any memory from earlier work > Digits 15 : # we need lots of digits for "famous numbers" > > unassign ``, `y` : # in case you used the letters elsewhere, and came back to this piece of code > f, y y : # slope field function for our DE i.e. for y ()=f(,y) > 0 0 : y 0 1 : #initial point h 0.025 : n 40 : #step size and number of steps - first attempt for "famous number e" Runge Kutta loop. WARNING: ONLY RUN THIS CODE AFTER INITIALIZING > for i from 0 to n do #this is an iteration loop, with inde "i" running from 0 to n if frac i 10 = 0 then print i, i, y i ; #print iteration step, current,y, values, and eact solution value end if: k1 f i, y i ; #current slope function value k2 f i h 2, y i h 2 k1 ; # first estimate for slope at right endpoint of midpoint of subinterval k3 f i h 2, y i h 2 k2 ; #second estimate for midpoint slope k4 f i h, y i h k3 ; #estimate for right-hand slope k k1 2 k2 2 k3 k4 6 ; # Runge Kutta estimate for rate of change of y i 1 i h; y i 1 y i h k; end do: #how to end a for loop in Maple 0, 0, 1 10, 0.250, 1.28402541566434 20, 0.500, 1.64872126807197 30, 0.750, 2.11700001155073 40, 1.000, 2.71828181979283 (11)

For other problems you may wish to use or modify the following Euler and improved Euler loops. Euler loop: WARNING: ONLY RUN THIS CODE AFTER INITIALIZING > for i from 0 to n do #this is an iteration loop, with inde "i" running from 0 to n print i, i, y i ; #print iteration step, current,y, values, and eact solution value k f i, y i ; #current slope function value i 1 i h; y i 1 y i h k; end do: #how to end a for loop in Maple > Improved Euler loop: WARNING: ONLY RUN THIS CODE AFTER INITIALIZING > for i from 0 to n do #this is an iteration loop, with inde "i" running from 0 to n print i, i, y i ; #print iteration step, current,y, values, and eact solution value k1 f i, y i ; #current slope function value k2 f i h, y i h k1 ; #estimate for slope at right endpoint of subinterval k k1 k2 2 ; # improved Euler estimate i 1 i h; y i 1 y i h k; end do: #how to end a do loop in Maple >

Before running, please put Eulers.m, Runge Kutta.m, Improved Eulers.m, SlopeField.m, eample.m in the same folder. To see the result, input eample in the command windows eample.m 1 % Clear the command window screen, all the variables and... close all windows 2 clc, clear, close all 3 4 % Input your function f(,y) (as a string), where f(,y) =... dy/d. In this eample, f(,y) = sin(*y) 5 f = '1-3.*+y'; 6 7 % Draw the slope field in the region minappleapplema,... yminappleyappleyma for dy/d = f(,y) 8 min = -1; 9 ma = 1; 10 ymin = -1; 11 yma = 1; 12 13 SlopeField(min,ma,ymin,yma,f) 14 15 hold on 16 17 % Use different numerical methods to solve the IVP on the... interval [0,b] with n subintervals : 18 % dy/d = f(,y) 19 % y(0) = y0 20 0 = 0; 21 b = 1; 22 y0 = 0; 23 n = 10; 24 25 % Use Euler Method to Solve 26 [ y]=eulers(0,y0,b,n,f); 27 plot(,y,'--','linewidth',2,'color','r'); 28 29 % Use Improved Euler Method to solve 30 [ y]=improved Eulers(0,y0,b,n,f); 31 plot(,y,'-','linewidth',2,'color',[0.9100 0.4100... 0.1700]); 32 33 % Use Runge Kutta method to solve 34 [ y]=runge Kutta(0,y0,b,n,f); 35 plot(,y,':','linewidth',2,'color','b'); 36 37 label('');ylabel('y') 38 legend('slope Field','Euler','Improved Euler','Runge Kutta') 39 title(['dy/d = ' f]) 1

The code gives you 1 0.8 0.6 0.4 0.2 dy/d = 1-3.*+y Slope Field Euler Improved Euler Runge Kutta y 0-0.2-0.4-0.6-0.8-1 -1-0.5 0 0.5 1 The functions used in eample.m SlopeField.m 1 function []= SlopeField(min,ma,ymin,yma,f) 2 % the function f(,y) must be entered as a string. For eample if 3 % f(,y)=y*cos(), then you need to enter 'y*cos()' for f 4 5 f = str2func(['@(,y) ',f]); % converts the string input into a... function handle 6 7 stepsize=abs(min-ma)/40; 8 [X,Y] = meshgrid(min:stepsize:ma, ymin:stepsize:yma); 9 dy=f(x,y); 10 dx=ones(size(dy)); 11 L=sqrt(1+dY.ˆ2); 12 quiver(x,y,dx./l,dy./l,'color','k','linewidth',1,'autoscalefactor',0.5); 13 ais tight 2

14 end 3

Eulers.m 1 function [,y] = Eulers( 0,y 0,b,n,f) 2 % Eulers method with n intervals to solve the initial value... problem dy/d=f(,y) with 3 % initial conditions y( 0)=y 0 on the interval [ 0,b]. The... function 4 % f(,y) must be entered as a string. 5 6 % For eample, if you wish to solve dy/d=y*cos() with y(0)=1... on the 7 % interval [0,15] with 50 intervals you would enter 8 % Eulers(0,1,15,50,'y.*cos()') 9 10 f = str2func(['@(,y) ',f]); %converts the string input into a... function handle 11 12 h=(b- 0)/n; 13 =zeros(n+1,1); y= zeros(n+1,1); %Pre-allocoting vectors for speed 14 (1)= 0; y(1)=y 0; 15 16 for i=1:n 17 (i+1)= 0+i*h; 18 y(i+1)=y(i)+h*f((i),y(i)); 19 end 20 end 4

Improved Eulers.m 1 function [,y] = Improved Eulers( 0,y 0,b,n,f) 2 % Improved Eulers method with n intervals to solve the initial... value problem dy/d=f(,y) with 3 % initial conditions y( 0)=y 0 on the interval [ 0,b]. The... function 4 % f(,y) must be entered as a string. 5 6 % For eample, if you wish to solve dy/d=y*cos() with y(0)=1... on the 7 % interval [0,15] with 50 intervals you would enter 8 % Improved Eulers(0,1,15,50,'y.*cos()') 9 10 f = str2func(['@(,y) ',f]); % converts the string input into a... function handle 11 12 h=(b- 0)/n; 13 =zeros(n+1,1); y= zeros(n+1,1); % Pre-allocoting vectors for speed 14 (1)= 0; y(1)=y 0; 15 for i=1:n 16 (i+1)= 0+i*h; 17 k1=f((i),y(i)); 18 u=y(i)+h*k1; 19 k2=f((i+1),u); 20 y(i+1)=y(i)+h*(k1+k2)/2; 21 end 22 end 5

Runge Kutta.m 1 function [,y] = Runge Kutta( 0,y 0,b,n,f) 2 % Runge Kutta method with n intervals to solve the initial value... problem dy/d=f(,y) with 3 % initial conditions y( 0)=y 0 on the interval [ 0,b]. The... function 4 % f(,y) must be entered as a string. 5 6 % For eample, if you wish to solve dy/d=y*cos() with y(0)=1... on the 7 % interval [0,15] with 50 intervals you would enter 8 % Runge Kutta(0,1,15,50,'y.*cos()') 9 10 f = str2func(['@(,y) ',f]); % converts the string input into a... function handle 11 12 h=(b- 0)/n; 13 =zeros(n+1,1); y= zeros(n+1,1); % Pre-allocoting vectors for speed 14 (1)= 0; y(1)=y 0; 15 for i=1:n 16 (i+1)= 0+i*h; 17 k1=f((i),y(i)); 18 k2=f((i)+h/2,y(i)+h/2*k1); 19 k3=f((i)+h/2,y(i)+h/2*k2); 20 k4=f((i+1),y(i)+h*k3); 21 y(i+1)=y(i)+h*(k1+2*k2+2*k3+k4)/6; 22 end 23 end 6