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

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

AIMS Exercise Set # 1

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

Intro to ODEs in Matlab. Jeremy L. Marzuola Charles Talbot Ben Wilson

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

Math 128A Spring 2003 Week 12 Solutions

Math 308 Week 8 Solutions

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

APPM 2360: Final Exam 10:30am 1:00pm, May 6, 2015.

Fourth Order RK-Method

CS520: numerical ODEs (Ch.2)

Initial value problems for ordinary differential equations

Ordinary Differential Equations

Understand the existence and uniqueness theorems and what they tell you about solutions to initial value problems.

Ordinary differential equations - Initial value problems

Multistep Methods for IVPs. t 0 < t < T

1 The pendulum equation

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

Solving Differential Equations: First Steps

June 2011 PURDUE UNIVERSITY Study Guide for the Credit Exam in (MA 262) Linear Algebra and Differential Equations

Chapter 1, Section 1.2, Example 9 (page 13) and Exercise 29 (page 15). Use the Uniqueness Tool. Select the option ẋ = x

Physics 584 Computational Methods

x x2 2 + x3 3 x4 3. Use the divided-difference method to find a polynomial of least degree that fits the values shown: (b)

Worksheet 8 Sample Solutions

Lab 5: Nonlinear Systems

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

u n 2 4 u n 36 u n 1, n 1.

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.

Eigenvalues and Eigenvectors, More Direction Fields and Systems of ODEs

Why are Discrete Maps Sufficient?

Nonlinear Autonomous Systems of Differential

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.

Boyce/DiPrima/Meade 11 th ed, Ch 1.1: Basic Mathematical Models; Direction Fields

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

ODE Homework 1. Due Wed. 19 August 2009; At the beginning of the class

Differential Equations FMNN10 Graded Project #1 c G Söderlind 2017

MTH 452/552 Homework 3

Homework and Computer Problems for Math*2130 (W17).

Math 1060 Linear Algebra Homework Exercises 1 1. Find the complete solutions (if any!) to each of the following systems of simultaneous equations:

INTRODUCTION TO COMPUTER METHODS FOR O.D.E.

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

Lecture 8: Calculus and Differential Equations

Lecture 8: Calculus and Differential Equations

MATH 215/255 Solutions to Additional Practice Problems April dy dt

21 Linear State-Space Representations

Math 266: Ordinary Differential Equations

Numerical Methods for Initial Value Problems; Harmonic Oscillators

Applied Linear Algebra in Geoscience Using MATLAB

Scientific Computing: An Introductory Survey

Systems of Linear ODEs

NUMERICAL SOLUTION OF ODE IVPs. Overview

Modeling and Experimentation: Compound Pendulum

Part IB Numerical Analysis

MATH 4B Differential Equations, Fall 2016 Final Exam Study Guide

Mathematical Models. MATH 365 Ordinary Differential Equations. J. Robert Buchanan. Spring Department of Mathematics

January 18, 2008 Steve Gu. Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,

Computer lab for MAN460

Mathematical Models. MATH 365 Ordinary Differential Equations. J. Robert Buchanan. Fall Department of Mathematics

NUMERICAL METHODS. lor CHEMICAL ENGINEERS. Using Excel', VBA, and MATLAB* VICTOR J. LAW. CRC Press. Taylor & Francis Group

Solutions to Final Exam Sample Problems, Math 246, Spring 2011

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

MATH 251 Week 6 Not collected, however you are encouraged to approach all problems to prepare for exam

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

20D - Homework Assignment 5

Math 56 Homework 1 Michael Downs. ne n 10 + ne n (1)

MS 3011 Exercises. December 11, 2013

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

Differential Equations Spring 2007 Assignments

Math Ordinary Differential Equations

Numerical Methods - Initial Value Problems for ODEs

Math 504 (Fall 2011) 1. (*) Consider the matrices

Old Math 330 Exams. David M. McClendon. Department of Mathematics Ferris State University

CDS 101 Precourse Phase Plane Analysis and Stability

HOMEWORK 3: Phase portraits for Mathmatical Models, Analysis and Simulation, Fall 2010 Report due Mon Oct 4, Maximum score 7.0 pts.

Math 122L. Additional Homework Problems. Prepared by Sarah Schott

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

Numerical Differential Equations: IVP

An Overly Simplified and Brief Review of Differential Equation Solution Methods. 1. Some Common Exact Solution Methods for Differential Equations

Numerical Methods for the Solution of Differential Equations

Math 251 December 14, 2005 Answer Key to Final Exam. 1 18pt 2 16pt 3 12pt 4 14pt 5 12pt 6 14pt 7 14pt 8 16pt 9 20pt 10 14pt Total 150pt

Section 9.3 Phase Plane Portraits (for Planar Systems)

MB4018 Differential equations

Polytechnic Institute of NYU MA 2132 Final Practice Answers Fall 2012

ECE257 Numerical Methods and Scientific Computing. Ordinary Differential Equations

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

Solution: In standard form (i.e. y + P (t)y = Q(t)) we have y t y = cos(t)

Chapter 6 - Ordinary Differential Equations

Review for Exam 2 Ben Wang and Mark Styczynski

Exam in TMA4215 December 7th 2012

Numerical solution of ODEs

Lecture 4: Numerical solution of ordinary differential equations

Elementary Differential Equations

Chapter 4: First-order differential equations. Similarity and Transport Phenomena in Fluid Dynamics Christophe Ancey

Math 331 Homework Assignment Chapter 7 Page 1 of 9

CHAPTER 5: Linear Multistep Methods

Numerical Methods for Ordinary Differential Equations

Problem set 7 Math 207A, Fall 2011 Solutions

Math 215/255 Final Exam (Dec 2005)

Chapter 4. Systems of ODEs. Phase Plane. Qualitative Methods

Section 2.1 Differential Equation and Solutions

Transcription:

Introduction to ODEs in Matlab Jeremy L. Marzuola Charles Talbot Benjamin Wilson E-mail address: marzuola@math.unc.edu Department of Mathematics, UNC-Chapel Hill, CB#3250 Phillips Hall, Chapel Hill, NC 27599 USA E-mail address: ctalbot@live.unc.edu Department of Mathematics, UNC-Chapel Hill, CB#3250 Phillips Hall, Chapel Hill, NC 27599 USA E-mail address: wilsonbn@live.unc.edu Department of Mathematics, UNC-Chapel Hill, CB#3250 Phillips Hall, Chapel Hill, NC 27599 USA

Abstract. An Introduction to ODEs using Matlab developed to enhance modeling and computational skills in the First Course in Differential Equations at the University of North Carolina.

Dedication This book is dedicated to Ingrid and Virginia, whose sleeplessness during the night assisted in writing many a chapter. Also, special thanks to Moshe Feldman, Dylan Gooch, Julia Sampson, Aric Wheeler, Yaoxuan Xia, Zhongcheng Xiao and Xiaohan Yi for being willing to stick with the course in its first iteration, pointing out where things could be clarified in early versions of the problem sets and working so hard in the Fall of 2014 to get as much from the first Lab course as possible. The Environmental Engineering worksheet was co-developed with Marc Alperin from Marine Sciences at UNC and we are grateful that he participated and created such an interesting model application. Special thanks to Katie Newhall and Greg Forest for looking over early versions and giving helpful comments. This course was also taken on as part of JLM s NSF Career Grant DMS-1352353.

Contents Chapter 1. Introduction 1 Chapter 2. Week 1 3 1. Introduction to Matlab 3 2. Worksheet 1 3 Chapter 3. Week 2 5 1. Variables, Matrices, Vectors 5 2. Worksheet 2 5 Chapter 4. Week 3 7 1. Solving Linear Systems in Matlab, Introduction to Functions in Matlab 7 2. Worksheet 7 Chapter 5. Week 4 9 1. Functions, Slope Fields, Converting Higher Order Differential Equations to Systems of First Order Differential Equations 9 2. Worksheet 9 Chapter 6. Week 5 11 1. Discussion Topics 11 2. Worksheet 11 Chapter 7. Week 6 15 1. Discussion Topics 15 2. Worksheet 15 Chapter 8. Week 7 19 1. Discussion Topics 19 2. Worksheet 19 Chapter 9. Week 8 23 1. Discussion Topics 23 2. Worksheet 23 Chapter 10. Week 9 25 1. Discussion Topics 25 2. Worksheet 25 Chapter 11. Week 10 27 1. Discussion Topics 27 2. Worksheet 27 vii

viii CONTENTS Chapter 12. Week 11 29 1. Discussion Topics 29 2. Worksheet 29 Chapter 13. Week 12 31 1. Discussion Topics 31 2. Worksheet 31 Chapter 14. Week 13 35 1. Discussion Topics 35 2. Worksheet 35 Chapter 15. Week 14 39 1. Discussion Topics 39 2. Worksheet 39

CHAPTER 1 Introduction The goal for this class will be to learn how to approximate solutions to equations (or systems of equations) of the form y = f(x, y), y(0) = 0. (1) Many solutions can be found by looking at an equation such as this by integrating it out and using the initial condition y(x) = y(0) + x 0 f(z, y(z))dz. (2) This looks like we have not gained much, but as we will see, with a little linear algebra we will see that we have gained quite a lot. To approximate solutions of this form, we will learn some basic algorithms and implement them in Matlab. Throughout, you are welcome work on implementing similar codes in other languages that are good for computation, such as Fortran, Python or C++ if you are interested. 1

CHAPTER 2 Week 1 1. Introduction to Matlab 2. Worksheet 1 1. Download Matlab!! Play with the help function to learn some of its key features. 2. Check out the tutorials from [?] to get a sense of some of the implicit Matlab capabilities. 3

CHAPTER 3 Week 2 1. Variables, Matrices, Vectors Introduction to variables and allocation in Matlab, Matrices, Vectors 2. Worksheet 2 1. Scalar variables in Matlab. (use implicit Matlab components like e, exp, 1i, sqrt, real, conj, log). a. Create x = 25.0. b. Create y = 1.4 10 4. c. Create z = e iπ/4. d. Evaluate and display ( x + y 1/4 ) π. e. Evaluate and display log(z z). 2. Vector variables in Matlab. (use implicit Matlab components like linspace,.,., 1i, pi, plot). Do not simply type all numbers into a vector in the editor. a. Create the 1 201 row vector x =.1[ 100 99... 99 100]. b. Create and display the 5 1 column vector y = c. Create and display the 1 9 row vector z = 10 2 10 1 10 0 10 1 10 2. [ e iπ e i3π/4 e iπ/2 e iπ/4 e 0 e iπ/4 e iπ/2 e i3π/4 e iπ]. d. Create the vector with pointwise components b = 1 2π e x/2. e. Plot b versus x. 3. Matrices. Make the following matrix variables in Matlab (use implicit Matlab components like linspace, ones, zeros, diag, rand, floor, ceil). Do not simply type all numbers into a matrix in the editor. 1... 1 a. Create and display M 1 =....., a 5 5 matrix of all 1 s. 1... 1 5

6 3. WEEK 2 b. Create and display 1 0 0 0 2 0 M 2 =....., 0 0 10 a 10 10 matrix with non-zero entries only on the diagonal. [ ] 5 0 5 0 c. Create and display M 3 =. 0 5 0 5 d. Create and display M 4, a 2 2 matrix with random values between 2 and 2. e. Create and display M 5, the 4 4 matrix [ ] M M 5 = 3 M 4 M 4

CHAPTER 4 Week 3 1. Solving Linear Systems in Matlab, Introduction to Functions in Matlab Matrices, Vectors, Gaussian Elimination, Solving Linear Systems in Matlab, Introduction to Functions in Matlab 2. Worksheet 1. Systems of equations. We will see how to use Matlab to solve the following linear system of equations. 5x 1 + 2x 2 + 3x 3 = 0 x 1 2x 2 + x 3 = 2 x 1 + x 2 x 3 = 1 In Matlab, use the operation A\b with 5 2 3 A = 1 2 1 1 1 1 the coefficient matrix and b = 0 2 1 the right hand side. Create and display the solution vector x 1 x = x 2. x 3 By hand, verify that the output solves the system of equations (you do not need to turn this in). 2. Solve the following linear systems of equations. Display the unique solution if one exists. If no solution exists display the phrase No solution exists and display the least squares solution. If more than one solution exists display the phrase Infinitely many solutions. (Use implicit Matlab components like \, lsqr, rref). You do not need to use an if statement. 7

8 4. WEEK 3 a. b. c. 5x 1 + 2x 2 + 3x 3 = 1 x 1 2x 2 + x 3 = 2 x 1 + x 2 x 3 = 1 x 1 + 2x 2 = 1 2x 1 4x 2 = 2 5x 1 + 2x 2 + 3x 3 = 1 x 1 2x 2 + x 3 = 2 4x 1 + 4x 2 + 2x 3 = 1 3. Functions. Write the following functions. (Each should be in its own file. Use implicit Matlab components like exp, sqrt, log, tanh, factorial, abs). a. f 1 (x) = x 2 e x2. b. f 2 (x) = tanh(x) 1+x 2. c. f 3 (x) = x x3 3!. d. For g = 1.0, L = 1.0, 1 e. f 5 (x) =. x log x 2 [ f 4 (t, v, h) = v g L sin(h) ]. 4. Perform the following tasks using the functions from problem 3. a. Evaluate and display f 1 (1). b. Evaluate and display f 2 (0). c. Evaluate and display f 3 (2). d. Evaluate and display f 4 (1, 2, π). e. Plot the curve y = f 5 (x) on the set x [2, 10]. Make the curve a red, dashed line, label the axes x and f 5 (x), and title the plot Week 3 Plot.

CHAPTER 5 Week 4 1. Functions, Slope Fields, Converting Higher Order Differential Equations to Systems of First Order Differential Equations Making functions in Matlab, Plotting slope fields in Matlab, Solving first order differential equations, converting second order differential equations into a system of first order differential equations 2. Worksheet 1. Anonymous functions. Create anonymous Matlab functions for the given maps. a. Write the function g 1 assuming one inputs the components of the input vector (x 1, x 2, x 3 ). g 1 : R 3 R 3 such that g 1 x 1 x 2 x 3 = sin(x 1 x 2 ) x 3 e 2x1 x 2 1 + x 2 2 + x 2 3. Evaluate and display g 1 (1, 1, 2). b. Write the function g 1 assuming one inputs a vector x R 3. such that g 2 ( x) = g 2 : R 3 R 3 sin(x 1 x 2 ) x 3 e 2x1 x 2 1 + x 2 2 + x 2 3. Create and display the vector x = (1, 1, 2). Evaluate and display g 2 ( x). 2. Write the functions g 1 and g 2 from question 1 in their own separate function files g1.m and g2.m and submit them with your assignment. 3. Direction fields. Solve the following differential equation by hand. dy dx = 2y. 9

10 5. WEEK 4 a. Use Matlab to make a direction field for the given differential equation. Let x [ 3, 3] and y [ 3, 3]. (Use Matlab components like meshgrid and quiver). b. On the same plot as your direction field plot three solution curves for the initial conditions y(0) = 1, y(0) = 0, and y(0) = 1. Label the plots. (Use Matlab components like hold on, plot, axis, and legend). 4. Complete the same exercises as problem 3 for the differential equation dy dx = x + y. 5. Built-in ODE solvers. Matlab ODE solvers allow one to solve equations of the form y = f(t, y), y(t 0 ) = y 0 (3) by giving the function f(t, y) as input. Even though Equation 3 only shows a first order equation, these methods can still be used to solve higher order equations if they are entered correctly by allowing y and f to both be column vectors instead of scalars. Convert the second order equation y + y + y 2 = sin(t) into a first order system of ODEs and create the Matlab function that inputs y as a 2-vector and outputs f(t, y) as a 2-vector. Call this function myweek4function and submit it with your assignment. Your function should accept inputs t and y ( y will be a two variable column vector) and output f(t, y), a column vector representing y.

CHAPTER 6 Week 5 1. Discussion Topics Numerical integration, Plotting in Matlab, Matlab ODE Solvers 2. Worksheet 1. Numerical integration using Riemann sums. Recall the following functions from Homework 2: f 1 (x) = x 2 e x2. f 2 (x) = tanh(x) 1+x 2. f 3 (x) = x x3 3!. 1 f 5 (x) =. x log x 2 a. Using a right Riemann sum with 10000 subintervals of equal size, approximate 10 10 (use implicit Matlab components linspace and sum). f 1 (x)dx (4) b. Using a left Riemann sum with 10000 subintervals of equal size, approximate (4). 2. Numerical integration. Numerically integrate and display your solutions (if one exists) to the following (use implicit Matlab components like quad, trapz, integral): a. f 1 (x) from x = 10 to 10. Compare this to your answers from question 1. b. f 2 f 3 from x = 0 to 2; c. Investigate the integral of (f 5 (x)) 2 from x = 0 to 1; d. f 5 (x) from x = 0 to 1; 11

12 6. WEEK 5 3. Fitting polynomials to data. Make the following plots in Matlab (use implicit Matlab components like polyfit, polyval, plot): a. A cubic polynomial approximation to sin x on the interval from 0 to 2π. b. A quartic polynomial approximation to sin x on the interval from 0 to 2π. c. A cubic polynomial approximation to sin x on the interval from 0 to 3π. d. A quartic polynomial approximation to sin x on the interval from 0 to 3π. Plot parts a and b on the same plot with a graph of the curve y = sin x. Do the same thing for parts c and d. (What happens with part c?)

2. WORKSHEET 13 4. Logistic growth model. A population grows according the the logistic model given by the equation dy = 10 (1 y)(y 2), t 0. (5) dt a. Graph a direction field plot of the logistic population growth model in Eq. (5) for t [0, 2] and y [0, 2.5] (use implicit Matlab commands like quiver and meshgrid). b. Use Matlab to the solve the logistic population model for the given initial conditions. Graph the solution curves on the same plot as your direction field. (Use implicit Matlab command ode45). (i) y(0) = 0.9; (ii) y(0) = 1 (iii) y(0) = 1.1; (iv) y(0) = 2; (v) y(0) = 2.1.

CHAPTER 7 Week 6 1. Discussion Topics Determinants, Eigenvalues, Eigenvectors and For Loops in Matlab 2. Worksheet 1. If/then statements and for loops. The determinant of the 2 2 matrix [ ] a b A = c d is the quantity det(a) := ad bc. A matrix is invertible if and only if it has a nonzero determinant. A matrix that is not invertible is called singular. a. Using a for loop and if/elseif/else statements, write a Matlab function called invertiblepowers(a,n) that takes in a matrix A and a positive integer n. The function should first check if the matrix is 2 2. If it is not, then print Matrix has the wrong dimensions. Then it should check if n is a positive integer. If it is not print n is not a positive integer (a good way to test this is by checking if floor(n)=ceil(n)). If A is a 2 2 matrix and n is a positive integer, then calculate the determinant of the first n powers of A: det(a), det(a 2 ),..., det(a n ) (do not use the implicit Matlab component det), and use these values to determine if A k is invertible for k = 1, 2,..., n. For each k let a k = det(a k ). Have your program print out either det(a k ) = a k so A k is invertible if a k 0, or det(a k ) = 0 so A k is singular if a k = 0 (just display the letter A as opposed to the actual name of the matrix). (You may use the Matlab components if, elseif, else, for, isequal, size, or, disp, and fprintf) b. Run your function and display the results for the following matrices and numbers, n: [ ] 1 2 3 (i) A =, n = 4; 4 5 6 [ ] 1 2 (ii) A =, n = 1.5; 4 5 [ ] 1 2 (iii) A =, n = 3; 4 5 15

16 7. WEEK 6 (iv) A = [ 1 2 3 6 ], n = 3. 2. Write a function called invertiblemat(a) that computes and displays the determinants of the matrix A. Use an if/then statement to print out either the term invertible or singular based upon the outcome. Run the function on the following matrices. (use implicit Matlab components like det, if, then, end, disp): a. b. c. M 4 I for M 2 = M 3 = M 4 = [ 1 2 3 4 ] 1 0 1 2 1 1 4 3 2 1 1 0 0 0 1 0 0 0 0 1 1 0 0 0 1 3. Second order equation. Recall the differential equation from homework 3 y + y + y 2 = sin(t). (6) Use ode45 and the function you created, myhw3odefunc, to find the solution to Eq. (6) with the initial conditions y(0) = 1, y (0) = 2. Plot your solution curve on the interval t [0, 5]. 4. Systems of First Order Linear Equations. A homogenous linear system of differential equation with constant coefficients can be written in the form d x = A x (7) dt where x(t) is an n 1 solution vector and A is a constant matrix. When n = 2 we can draw a visualization in the x 1 x 2 -plane called the phase plane. We evaluate A x at many points x and plot the resulting vectors (similar to a slope field). A plot that includes many trajectories is called a phase portrait. Write a function called directionfield(a) that creates a phase portrait for the system in Eq. (8) given a 2 2 matrix A. Run the program on the following matrices. (Let x 1 and x 2 each be in the interval [ 2.5, 2.5]. Use implicit Matlab components meshgrid and quiver.) a. A = [ 1 2 4 1 ]

2. WORKSHEET 17 b. c. d. [ ] 1 2 A = 2 1 [ ] 1 0 A = 4 2 A = [ 4 1 2 1 ]

CHAPTER 8 Week 7 1. Discussion Topics More on Matlab ODE Solvers and Newton s Method 2. Worksheet 1. Eigenvalues and Eigenvectors. λ C is called an eigenvalue of the n n matrix A if there is some (nonzero) vector v C n such that Av = λv. In this case, v is called an eigenvector of A associated with the eigenvalue λ. If there are n linear independent eigenvectors for A then they form a basis of C n. Write a function called eigenbasis(a) that prints out the eigenvalues of the matrix A with corresponding eigenvectors. Then print out whether or not the eigenvectors of A form a basis for the space C n. Run the function on each of the matrices below. (use implicit Matlab components like eig, disp, rank, if, then, end). Hint: If the rank of an n n matrix is less than n then its columns are not linearly independent and therefore do not form a basis of C n. a. b. c. M 2 = M 3 = M 4 = [ 1 2 3 4 ] 1 0 1 2 1 1 4 3 2 1 1 0 0 0 1 0 0 0 0 1 1 0 0 0 1 2. Phase portrait. In Homework 5 you wrote a function to make the direction field for the 2 2 homogeneous system of first order differential equations d x = A x. (8) dt Use the function you made, directionfield(a), to make a direction field for the following matrices with x 1 and x 2 in the interval [ 2.5, 2.5]. Then create a function 19

20 8. WEEK 7 phaseportrait(a,tmin,tmax,x1init,x2init) which uses ode45 to draw solution curves to Equation (8) in the x 1 x 2 -plane. This is known as a phase portrait of the system. The function should take a 2 2 matrix, A, minimum and maximum t values, and initial conditions for x 1 (t) and x 2 (t). Your function should also plot the initial condition point. Use this function to create a phase portrait on the same plot as your direction fields for each matrix with the given initial conditions. Letting t go from 0 to 20 should suffice for these example. a. [ ] 1 2 A = 4 1 x 1 (0) x 2 (0) i. 1.75 2.5 ii. 2 2.5 iii. 1.76 2.5 iv. 1.77 2.5 b. [ 1 2 A = 2 1 ] x 1 (0) x 2 (0) i. 2.5 0 ii. 1.5 2.5 iii. 2.5 0 iv. 2.5 2 c. A = [ 1 0 4 2 ] x 1 (0) x 2 (0) i. 0.35 1 ii. 0.15 0.5 iii. 0.3 1 iv. 0 0.5 d. A = [ 4 1 2 1 ]

2. WORKSHEET 21 x 1 (0) x 2 (0) i. 2.5 2.3 ii. 2.5 0 iii. 2.5 2.3 iv. 2.5 2.5 3. Pendulum. A simple physical problem that leads to a nonlinear, second order differential equation is the oscillating pendulum. The angle θ that an oscillating pendulum of length L makes with the vertical direction (see Figure 1) satisfies the equation d 2 θ dt 2 + g sin θ = 0. (9) L Figure 1. An oscillating pendulum a. Recall the function [ ] v f 4 (t, v, h) = g L sin(h) from homework 2. Use ode45 or ode15s and the function f 4 to solve the pendulum equation, Eq. (9), for g = L = 1.0 and θ(0) = 0.1, θ (0) = 0. (Why do we use f 4 to solve this ODE?) Plot your solution curve on t [0, 30]. b. What happens if g = 1.0 and L = 5.0? In other words, what happens to a really long pendulum? Use Matlab to solve the pendulum equation with these parameters and the same initial conditions as part a. You may want to alter f 4 to include the parameters g and L or make a new function. c. What happens if g = 0.17 and L = 1.0? In other words, what happens if you put a pendulum on the moon? Use Matlab to solve the pendulum equation with these parameters and the same initial conditions as part a. d. If θ is small then sin θ θ so Eq. (9) can be approximated by the linear equation d 2 θ dt 2 + g θ = 0. (10) L Use Matlab to solve the linearized pendulum equation with the same initial conditions as part a. You may want to create a function similar to f4. Plot the solution curve and compare it to the curve in part a.

22 8. WEEK 7 Use subplot to put all four graphs of the solution curves in the same figure. Label the plots and arrange them in a 2 2 matrix with graphs pertaining to each part in the following spots: a b d c e. Create a phase diagram (plot of θ vs θ) for the pendulum equation with g = L = 1.0. Plot this graph in a separate figure. Bonus: What is the relationship between the eigenvalues of a matrix A and the behavior of the direction field for Equation (8) near the point (0, 0)? Send your observations in your email with your assignment. Hint: The four parts of question 2 all behave in distinct ways near (0, 0).

CHAPTER 9 Week 8 Writing your own ODE solvers 1. Discussion Topics 2. Worksheet 1. Euler s method. (Forward or explicit) Euler s method is a simple way to approximate a solution to a differential equation of the form y = f(t, y), y(t 0 ) = y 0 (11) for a discrete set of points, y n. Suppose we want to approximate a solution to Eq. (24) over the interval t 0 < t < T. The steps in Euler s method are to first divide the interval into N equal sections. Then t n = t 0 + nh for n = 0, 1,..., N and h = (T t 0 )/N. The values of y n are then found recursively with the formula y 0 = y(t 0 ), y n+1 = y n + hf(t n, y n ). a. Use a for loop construction to write your own ODE solver for the following differential equation y = y 3 + 2t, y(0) = 0 (use implicit Matlab components like for, end) over the interval t = 0 to t = 1 implementing 50 uniform time steps of (explicit or forward) Euler s Method. Display the values t 0, t 25, t 50, y 0, y 25, and y 50. b. Solve the same equation using ode15s with a relative error tolerance of 1e 10 letting Matlab set the number of time steps. (Use the implicit Matlab commands odeset and RelTol.) c. Solve the given equation using ode45 with the default error tolerance. d. Plot your approximations of y(t) from parts a, b, and c on the same plot. 2. Write the function [t,y]=eulers(f,t0,y0,t,n) to run Euler s method in order to approximate a solution to Eq. (24). Each input variable is defined as they are in question 1 (t0=t 0 and y0=y 0 ). The outputs, t and y should be 1 (N + 1) 23

24 9. WEEK 8 vectors with t(n) = t n and y(n) = y n. Use your function to graph approximate solutions to the equation y = y 3 + 2t, y(0) = 0 the following values of N on the same plot. a. N = 10; b. N = 20; c. N = 40; d. N = 50; e. N = 100; f. N = 1000; 3. The Hermite Equation. The equation y 2xy + 2λy = 0, λ R (12) is known as the Hermite equation. It is an important equation in mathematical physics. When λ is a nonnegative integer, n, the solution to Eq. (12) is a polynomial, H n (x), of degree n called the nth Hermite polynomial. a. Use a Matlab ODE solver to find solutions to the Hermite equation for λ = 0, 1, 2, 3 with the following initial conditions over the interval x [ 1.8, 1.8]. You may want to make a function to help solve this second order differential equation similar to myhw3odefunc. n y(0) y (0) 0 1 0 1 0 2 2 2 0 3 0 12 Recall how to use an ODE solver backwards and forwards when the initial conditions are not given at the beginning of your interval. Plot H n (x), x [ 1.8, 1.8] and y [ 10, 10], for n = 0, 1, 2, 3 on the same graph. (You may want to use the implicit Matlab commands ode45 or ode15s to find solutions and wrev to help plot each piece correctly). b. Use polyfit and your solutions in part a to find H n (x) for n = 0, 1, 2, 3. Round each coefficient to the nearest integer and display each polynomial. (Use the Matlab components round and poly2sym).

CHAPTER 10 Week 9 1. Discussion Topics Approximation of ODEs, Special Functions 2. Worksheet 1. Slope Field. In Homework 3 you plotted direction fields for first order differential equations. Write a function slopefield(f,tmin,tmax,ymin,ymax,tstep,ystep) to plot a direction field for the first order ODE y = f(t, y) for t [tmin, tmax] and y [ymin, ymax] with uniform steps in the t and y direction of size tstep and ystep. Your arrows should all be unit length. Use implicit Matlab components meshgrid and quiver. Run slopefield for the following differential equations and given inputs. Then run the function Eulers that you created in Homework 7 for the given initial condition with 1000 uniform steps and graph the approximate solution on the same graph as the slope field for each part. a. y = 5 3 y, t [0, 4], y [0, 4], tstep= 0.1, ystep= 0.1, y(0) = 1; b. y = (4 ty)/(1 + y 2 ), t [0, 4], y [ 4, 4], tstep= 0.15, ystep= 0.2, y(0) = 2; 2. Implicit Euler s method. Backward (or implicit) Euler s method is similar to forward Euler s method but the recursive formula is slightly different making it an implicit method. We would like to approximate a solution to the equation y = f(t, y), y(t 0 ) = y 0 (13) over the interval t 0 t T. To calculate y n using backward Euler, we use the formula y 0 = y(t 0 ), y n+1 = y n + hf(t n+1, y n+1 ). where t n and h are the same as they were on homework 6. This is an implicit method because y n+1 appears on both sides of the equation. We must use algebra or some numerical method (Newton s method) to find y n+1. a. Run the function Eulers to approximate a solution to the differential equation y = t 2 y, y(0) = 2. (14) 25

26 10. WEEK 9 over the interval t = 0 to t = 2 implementing 100 uniform time steps. Plot the solution curve. b. Now write an ODE solver for Eq. (14) over the interval t = 0 to t = 2 implementing 100 uniform time steps of backward Euler s method. c. Solve the Eq. (14) by hand and plot your approximations from parts a and b along with the exact solution. 3. Newton s Method. A standard numerical method for finding solutions to the equation f(x) = 0 is known as Newton s method. Suppose f : [a, b] R is differentiable and its derivative is given by f. To approximate a root of f we first guess a root x 0. We then recursively approximate the root using the equation x n+1 = x n f(x n) f (x n ). (15) The method will usually converge assuming x 0 is close enough to the root and f (x 0 ) 0. Write a function root=newtonsmethod(f,df,x0,n) to perform N iterations of Newton s method given the functions f and df = f and an initial guess x0 for a root of f. Use your function for the following exercises and display the results in a long format (use the Matlab command format). a. Perform 1000 iterations of Newton s method to find a root of f(x) = x 2 2 with x 0 = 1. b. Perform 1000 iterations of Newton s method to find a root of f(x) = sin x with x 0 = 3. c. Perform 1000 iterations of Newton s method to find a solution to the equation of x 2 = x + 1 with x 0 = 2. d. Perform 1000 iterations of Newton s method to find a solution to the equation of log(x 2 ) = 2 with x 0 = 2. Bonus: Name the four constants you approximated in Question 3. Display the names or symbols of the constants.

CHAPTER 11 Week 10 1. Discussion Topics Stability and Instability in ODE algorithms 2. Worksheet 1. Newton s second law. Suppose an object of mass m (in kg) is falling in the atmosphere near sea level. Let x(t) represent the distance of the object from the ground (in meters) where x = 0 is the ground level and x < 0 means the object is above the ground (this assumption is made so the direction of positive velocity, dx/dt, is downward). Assuming a constant acceleration due to gravity is 9.8 m/s 2 and a drag coefficient of the object is γ kg/s, by Newton s second law of motion, the mathematical model of the object falling is given by m d2 x dt 2 + γ dx = 9.8m. (16) dt a. Rewrite the function [tout,yout]=eulers(f,t0,y0,t,n) from Homework 7 to run Euler s method for any order differential equation that can be written as a system of first order differential equations of the form y (t) = f(t, y), y(t 0 ) = y 0. (17) The function should run N iterations of Euler s method. tout should be an (N + 1) 1 column vector such that tout(n + 1) = t n. For an order k differential equation, yout should be an (N + 1) k matrix where yout(n + 1, k) = y n (k 1) (t n ) (an approximation to the (k 1)st derivative of y at t n ). b. Run your new Eulers function to solve Eq. (16) for an object of mass 1 kg and a drag coefficients 0.2 kg/s dropped from a height of 10 m (so x(0) = 10 and x (0) = 0). Plot the approximation found using 10000 step of Euler s method for t [0, 2.5]. c. Display the time at which the object hits the ground. (Find the first value of tout such that xout(tout) < 1e 2. Use implicit Matlab commands abs and find.) d. Repeat parts b and c for the same object thrown upwards from a height of 1 m at a velocity of 10 m/s. Graph the approximation over the same interval on the same plot as the graph for part b. Plot the line x = 0 to represent the ground. 27

28 11. WEEK 10 2. Wronskian and Abel s theorem. a. Use your function from Question 1 to compute solutions to y (t) + sin(t)y (t) + cos(t)y(t) = 0 (18) first for y 1 (0) = 1 and y 1(0) = 0, then for y 2 (0) = 1 and y 2(0) = 1 on the interval t [0, 2π] using 1000 iterations of Euler s method. Graph your solutions on the same plot. b. Compute the function [ ] y1 (t) y W (t) = det 2 (t) y 1(t) y 2(t) (19) at the values of t you approximated in part a. W (t) is called the Wronskian of y 1 and y 2. c. Approximate the ODE z (t) + sin(t)z(t) = 0, z(0) = 1 (20) on the interval [0, 2π] using 1000 iterations with your Euler s method function. d. Graph W (t) and z(t) on [0, 2π] on the same plot. This is a numerical verification of Abel s Theorem. 3. a. Plot the function f(x) = e x 10 cos(x) on the interval from 0 to 4. Find the integer x 0 in this interval such that f(x 0 ) 0. b. Use 10 iterations of the Newton s method function you created in Homework 7 using the x 0 you found in part a. c. Plot with x-marks the differences of the iterates ( x 1 x 0, x 2 x 1,..., x 10 x 9 ). On the same figure plot with circles the points f(x n ) for n = 1,..., 10.

CHAPTER 12 Week 11 1. Discussion Topics Implementing Newton s Method in Algorithms and RK4, Lotka- Volterra Model 2. Worksheet 1. Gamma function. The gamma function is denoted by Γ(p) and is defined by the integral Γ(p + 1) = The integral converges as x for all p. 0 e x x p dx. (21) a. Evaluate and display Γ(n) for n = 1, 2,..., 10; (use the implicit Matlab command integral). b. Evaluate and display n! for n = 0, 1,..., 9; (use the implicit Matlab command factorial). If p is a positive integer, n, it can be shown that Γ(n + 1) = n!. Since Γ(p) is also defined when p is not an integer, this function provides an extension of the factorial function to non-integral values of the independent variable (check that Γ(1/2) = π). Matlab has an implicit command gamma for the gamma function. You may use this command in the next question. 2. Airy s Equation. The solution to Airy s equation, y xy = 0, (22) is known as the Airy function. The Airy s equation was designed to model light moving through a caustic (such as diffraction that forms a rainbow for instance). a. Use an ODE solver of your choice to plot a special fundamental solution to Airy s equation with initial conditions 1 y(0) = 3 2/3 Γ(2/3), 1 y (0) = 3 1/3 Γ(1/3) over the interval x [ 10, 10]. Notice that the plot of the solution is oscillatory for x < 0 and exponentially decaying for x > 0 (you will need to adjust your error tolerances to see the correct behavior). 29

30 12. WEEK 11 b. Compare your function to the implicit Matlab function airy on the same interval using a plot (note, the Airy function in Matlab will have a tiny imaginary component, so you will need to take the real part using real). 3. Bessel Equation of Order 0. The Bessel equation of order 0, x 2 y + xy + x 2 y = 0, (23) is related to modes of oscillation on discs and many other physical examples. The solutions to this equation define the Bessel functions J 0 (x) and Y 0 (x). a. Use an ODE solver of your choice to solve the Bessel equation with initial conditions y(0) = 1 and y (0) = 0 over the interval x [ 10, 10]. Since the coefficients are singular near x = 0, we will need to replace the initial conditions. To approximate a solution for x > 0 use y(0.01) = 1 and y (0.01) = 0.005. For x < 0 use y( 0.01) = 1 and y ( 0.01) = 0.005. b. Use an ODE solver of your choice to solve the Bessel equation with initial conditions y(0) = 0 and y (0) = 1 over the interval x [ 10, 10]. As is part a, we will need to replace the initial conditions. To approximate a solution for x > 0 use y(0.01) = 3 and y (0.01) = 64. For x < 0 use y( 0.01) = 3 and y ( 0.01) = 64. c. Plot your solution functions and compare them to plots of the implicit Matlab functions besselj and bessely. (Use 0 for the order of the Bessel functions of the first and second kind).

CHAPTER 13 Week 12 1. Discussion Topics Multi-Step Methods and Crank-Nicholson, Lorenz Equations, Optics 2. Worksheet 1. Early convergence tests. The use of a numerical procedure, such as Euler s method, on an initial value problem raises questions of convergence. That is, as we decrease the step size h, (or increase the number of iterations, N) do the values of the numerical approximation y n approach that corresponding values of the actual solution? Assuming this is true, we want to use step sizes that are small enough to ensure accurate approximations, but not too small to slow down calculations too much. a. Recall the system of differential equations from problem 2a from Homework 10 x (t) = u(t) y (t) = v(t) u (t) = x x 2 + y 2 v (t) = y x 2 + y 2 with initial value (x, y, u, v)(0) = (1, 0, 0, 1). This initial value problem has an exact solution (x, y, u, v)(t) = (cos t, sin t, sin t, cos t). Use your function Eulers from Homework 9 to solve the initial value problem for N = 100, 200, 300, 400, 500, and 1000 over the time interval [0, 4π]. Using subplots in the formation shown below, graph your approximate solutions for (x(t), y(t)) for each N. On each plot also graph the exact solution (x(t), y(t)). N = 100 N = 200 N = 300 N = 400 N = 500 N = 1000 b. Make a 1 6 vector of the maximum error for each N in part a. error N = max{ x n cos t n, y n sin t n, u n + sin t n, v n cos t n }. 31

32 13. WEEK 12 Plot the maximum error as a function of N. (Use the implicit Matlab command max.) c. Use tic and toc to find the time taken per step of each method. You will have to alter your Eulers code to do this. Display the total time elapsed and the average time per step for each N. 2. The Runge-Kutta Method. Another explicit numerical method to solve the initial value problem y = f(t, y), y(t 0 ) = y 0 (24) is called the Runge-Kutta method. The Runge-Kutta formula involves a weighted average of values of f(t, y) at different points in the interval t n t t n+1. It is given by ( ) kn1 + 2k n2 + 2k n3 + k n4 y n+1 = y n + h (25) 6 where k n1 = f(t n, y n ) k n2 = f(t n + 1 2 h, y n + 1 2 hk n1) k n3 = f(t n + 1 2 h, y n + 1 2 hk n2) k n4 = f(t n + h, y n + hk n3 ). The sum (k n1 + 2k n2 + 2k n3 + k n4 )/6 can be interpreted as an average slope. a. Write a function [tout,yout]=rungekutta(f,t0,t,y0,n) to run the Runge- Kutta method to approximate values of the solution to to Eq. (24). b. Use the function you wrote in a to approximate a solution to the initial value problem y = 2t + e ty, y(0) = 1 on the interval t [0, 2] for N = 100, 200, 300, 500, and 1000. Graph your solutions on the same plot. 3. Comparison of methods. Consider the initial value problem y = 3 + t y, y(0) = 1. Find solutions over the interval t [0, 2] to this differential equation in the following ways: a. Find the exact solution (using paper and pencil). b. Approximate with forward Euler s method with N = 100 and N = 1000. c. Approximate with backward Euler s method with N = 100 and N = 1000.

2. WORKSHEET 33 d. Approximate with the Runge-Kutta method with N = 100 and N = 1000. Graph the solutions on two figures, one for each value of N (put the graph of the exact solution on both plots). In the command window, display y(2) for each solution. (You should display seven values of y(2): the exact value and two for each method corresponding to the two values of N). Also display the total time it takes to run each method. (Use tic and toc.)

CHAPTER 14 Week 13 1. Discussion Topics Hamiltonians, Newton s Method in higher dimensions, RK Schemes 2. Worksheet 1. Improved Euler formula. Consider the initial value problem y = f(t, y), y(t 0 ) = y 0 (26) and let y = ϕ(t) denote its solution. By integrating the equation ϕ (t) = f(t, ϕ(t)) from t n to t n+1, we obtain tn+1 tn+1 ϕ (t) dt = f(t, ϕ(t)) dt, t n t n or tn+1 ϕ(t n+1 ) = ϕ(t n ) + f(t, ϕ(t)) dt. (27) t n The integral in Eq. (27) is represented geometrically by the area under the curve in Figure 1 over the interval t [t n, t n+1 ]. If we approximate this area with the area of the gray square then Eq. (27) becomes ϕ(t n+1 ) = ϕ(t n ) + (t n+1 t n )f(t n, ϕ(t n )). By replacing ϕ(t n ) by y n and t n+1 t n by h this gives y n+1 = y n + hf(t n, y n ). (28) We recognize Eq. (28) as the formula used in forward Euler s method. This visualization allows us to see why our approximation to the solution using Euler s method improves as h gets closer to zero since a smaller h causes the area of the approximating rectangles to be closer the exact area under the curve f(t, ϕ(t)). If we approximate the integral in Eq. (27) by the area of the rectangle shown in Figure 2 then Eq. (27) becomes y n+1 = y n + hf(t n+1, y n+1 ). (29) We recognize Eq. (29) as the formula used in forward Euler s method. A better approximate formula can be obtained if we approximate the integral in Eq. (27) by the area of the trapezoid shown in Figure 3. Then, Eq. (27) becomes y n+1 = y n + h 2 (f(t n, y n ) + f(t n+1, y n+1 )). (30) 35

36 14. WEEK 13 y y = f(t, ϕ(t)) f(t n, ϕ(t n )) t n t n+1 t Figure 1. Geometric representation of forward Euler s method. y f(t n, ϕ(t n+1 )) y = f(t, ϕ(t)) t n t n+1 t Figure 2. Geometric representation of backward Euler s method. Eq. 30 is an implicit formula because it has y n+1 on both sides of the equation. We can make it explicit by using Euler s formula and replacing y n+1 on the right hand side by the right hand side of Eq. 28. This gives y n+1 = y n + h 2 (f(t n, y n ) + f(t n+1, y n + hf(t n, y n ))). (31) Eq. (31) is known as the improved Euler formula, the Huen formula, or the explicit trapezoidal rule. a. Write a function [tout,yout]=improvedeulers(f,t0,t,y0,n) to run the improved Euler s method to approximate values of the solution to Eq. (26). b. In Homework 12 you approximated solutions to the initial value problem y = 3 + t y y(0) = 1 (32) on the interval t [0, 2] using forward Euler s method, backward Euler s method, and the Runge-Kutta method for N = 100 and N = 1000 iterations. Approximate solutions with the improved Euler s method for N = 100 and N = 1000. Graph the solutions on two figures and include the exact solution on both plots. Display the approximate value of y(2) for each N and the

2. WORKSHEET 37 y y = f(t, ϕ(t)) f(t n, ϕ(t n+1 )) f(t n, ϕ(t n )) t n t n+1 t Figure 3. Geometric representation of improved Euler s method. exact value of y(2) in the command window. Compare these approximations with those from Homework 12. 2. Order of accuracy. Suppose we are numerically approximating a solution to y = f(t, y), y(t 0 ) = y 0 and we assume our computer can execute all computations exactly, i.e., it can retain infinitely many digits at each step. The difference E n between the solution y = ϕ(t) and the numerical approximation y n at the point t = t n is given by E n = ϕ(t n ) y n. The error E n is known as the global truncation error. A numerical solution to a differential equation is said to be kth-order accurate and the method used to obtain such a solution is said to be a kth order method if E n is no greater than a constant times h k. In other words, for some constant C, E n Ch k. (33) Recall, for an approximation over the interval t [t 0, T ] using numerical method with N iterations, Consider the initial value problem h = T t 0 N. y = t 2 y, y(0) = 1. (34) a. For each of the following methods, approximate the solution to Eq. (34) over the interval t [0, 1] for the following values of h: h = 0.05, 0.02, 0.01, 0.005, 0.002, 0.001, 0.0005, 0.0002, 0.0001. For each method, graph the nine approximate solutions and the exact solution (which you should find by hand). You should end up with a different figure for each method containing ten curves. i. Forward Euler s method.

38 14. WEEK 13 ii. Improved Euler s method. iii. Runge-Kutta method. b. For each method above, and each h, find the maximum of the absolute value of the global truncation error, E n. For each method, you should get nine points, one for each h. In a separate figure for each method, plot these points versus h. Bonus (1 point): What is the order of each of the methods used in Question 2? Send me your answers in the email with your assignment. (Hint: try fitting polynomial curves of different orders to the graphs in part b until you have what appears to be a best fit curve). c. Using tic and toc find the total time it takes to run each method in part a for each value of h. You should alter your functions for each method to include the computation time as an additional output argument. Make a table for each method displaying the maximum global truncation error and computation time for each h. (You should end up with three tables each with nine rows, one for each h, and three columns displaying h, the maximum global truncation error, and the computation time).

CHAPTER 15 Week 14 1. Discussion Topics Stable and Unstable Equilibrium Points, Linearization 2. Worksheet 1. Predator-Prey Equations. Predator-prey equations (also known as Lotka- Volterra equations) are a pair of first order, non-linear differential equations used to model the dynamics of a biological situation in which one species (the predator) preys on another species (the prey). Denote by x and y the populations of prey and predator, respectively, at time t. The populations change through time according to the equations x (t) = x(a αy) (35) y (t) = y( c + γx). (36) The constants a, c, α, and γ are all positive. a is the growth rate of the prey, c is the death rate of the predator, and α and γ are measures of the effect of the interaction between the two species. Notice that if x = 0 then y = cy so the predator population decreases and eventually dies out. Similarly, if y = 0 then x = ax so the prey population grows exponentially. We can modify the predator-prey equations to the following x (t) = x(a 1 a 2 x αy) (37) y (t) = y(c 1 c 2 y + γx). (38) In this modified system, if y = 0 then x = x(a 1 a 2 x) and if x = 0 then y = y(c 1 c 2 y) so the growth in each population is logistic instead of exponential. a. Consider the predator-prey equations 39

40 15. WEEK 14 x (t) = x(3 x 2y) y (t) = y(2 y x). Create a direction field for the system with x [0, 4] and y [0, 4]. b. Recall, a critical point of a system of differential equations is a point (x, y) at which the right hand sides of the equations in the given system are simultaneously 0. The predator-prey equations in this problem have four critical points, one of which is (1, 1). Find the other three and plot all four critical points on the same graph as the direction field. c. Use the Runge-Kutta method to solve the system over the time interval t [0, 10] with the following initial conditions: i. (x, y)(0) = (0, 0.1); ii. (x, y)(0) = (0.1, 0); iii. (x, y)(0) = (0.1, 0.1); iv. (x, y)(0) = (0.1, 0.2); v. (x, y)(0) = (0.1, 0.3); vi. (x, y)(0) = (3.5, 1); vii. (x, y)(0) = (3.5, 2); viii. (x, y)(0) = (3.5, 3); ix. (x, y)(0) = (3.5, 4). Plot all the approximate solutions on the same graph as the direction field and critical points. You may have to modify your Runge-Kutta function from Homework 12 so it can solve a system of equations. 2. Adams Method. In every numerical procedure we have discussed so far the approximation of y n = y(t n ) relies only on the value of y n 1. In this method, we will see that the approximation of y n use the values of y n 1 and y n 2. Methods that use information at more than the last approximated point are referred to as multistep methods. The Adams-Bashforth formula is a multistep method designed around being accurate for polynomials of a chosen order. For a constant step size h, the second

2. WORKSHEET 41 order Adams-Bashforth formula is given by y n+1 = y n + 3 2 hf(t n, y n ) 1 2 hf(t n 1, y n 1 ). (39) Use this Adams-Bashforth algorithm to solve the Lorenz equations x = y x y = x(1 z) y z = xy z (x, y, z)(0) = (1, 1, 0). on time scale [0, 15]. Plot the three dimensional solution curve. Hint: approximate y 1 with a step of explicit Euler s method. The Lorenz equations were first studied as a model in meteorology and other applications of fluid dynamics. They concern the motion of a layer of fluid, such as the earth s atmosphere, that is warmer at the bottom than at the top. 3. Crank-Nicolson method. The Crank-Nicolson method is an implicit numerical method for solving ordinary differential equations that implements the midpoint rule. The algorithm is y n+1 = y n + 1 2 h(f(t n+1, y n+1 ) + f(t n, y n )). (40) Use the Crank-Nicolson method to solve the differential equation y = 100y + 100t + 1, y(0) = 1 (41) for 0 t 1 and the following values of h: i. h = 1/10 ii. h = 1/30 iii. h = 1/40 iv. h = 1/60 Plot each approximation as well as the exact solution on the same graph. 4. Stability. The concept of stability is associated with the possibility that small errors that are introduced in the course of a numerical method will disappear as the procedure continues. Instability occurs if small errors tend to increase, perhaps unbounded. If we are investigating an initial value problem numerically, we may introduce instabilities that were not part of the original problem. In other words, the errors in our method cause the approximation of the solution to greatly differ from the exact solution and to possibly increase unbounded. Avoidance of such instabilities may require us to place restrictions on the step size h. Solve Eq. (41) using each of the following methods for the four values of h given in Problem 3.

42 15. WEEK 14 a. Forward Euler s method b. Backward Euler s method c. Improved Euler s method d. Runge-Kutta method e. Adams method f. Crank-Nicolson method (already done in Problem 3) Create a different plot for each value of h that contains all approximations and the exact solution. Notice some of the approximations are unstable. This problem is called stiff because a much smaller step size, h, is required for stability than for accuracy. The backwards differentiation formulas are the most popular for dealing with stiff problems.