introduction to odes in matlab - benjamin...

49
Introduction to ODEs in Matlab Jeremy L. Marzuola Charles Talbot Benjamin Wilson E-mail address : [email protected] Department of Mathematics, UNC-Chapel Hill, CB#3250 Phillips Hall, Chapel Hill, NC 27599 USA E-mail address : [email protected] Department of Mathematics, UNC-Chapel Hill, CB#3250 Phillips Hall, Chapel Hill, NC 27599 USA E-mail address : [email protected] Department of Mathematics, UNC-Chapel Hill, CB#3250 Phillips Hall, Chapel Hill, NC 27599 USA

Upload: phunganh

Post on 14-Jun-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

Introduction to ODEs in Matlab

Jeremy L. Marzuola

Charles Talbot

Benjamin Wilson

E-mail address: [email protected]

Department of Mathematics, UNC-Chapel Hill, CB#3250 PhillipsHall, Chapel Hill, NC 27599 USA

E-mail address: [email protected]

Department of Mathematics, UNC-Chapel Hill, CB#3250 PhillipsHall, Chapel Hill, NC 27599 USA

E-mail address: [email protected]

Department of Mathematics, UNC-Chapel Hill, CB#3250 PhillipsHall, Chapel Hill, NC 27599 USA

Page 2: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

Abstract. An Introduction to ODEs using Matlab developed to enhance

modeling and computational skills in the First Course in Differential Equa-tions at the University of North Carolina.

Page 3: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

Dedication

This book is dedicated to Ingrid and Virginia, whose sleeplessness during thenight assisted in writing many a chapter. Also, special thanks to Moshe Feldman,Dylan Gooch, Julia Sampson, Aric Wheeler, Yaoxuan Xia, Zhongcheng Xiao andXiaohan Yi for being willing to stick with the course in its first iteration, pointingout where things could be clarified in early versions of the problem sets and workingso 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 Alperinfrom Marine Sciences at UNC and we are grateful that he participated and createdsuch an interesting model application. Special thanks to Katie Newhall and GregForest for looking over early versions and giving helpful comments. This coursewas also taken on as part of JLM’s NSF Career Grant DMS-1352353.

Page 4: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s
Page 5: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s
Page 6: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

Contents

Chapter 1. Introduction 1

Chapter 2. Week 1 31. Introduction to Matlab 32. Worksheet 1 3

Chapter 3. Week 2 51. Variables, Matrices, Vectors 52. Worksheet 2 5

Chapter 4. Week 3 71. Solving Linear Systems in Matlab, Introduction to Functions in Matlab 72. Worksheet 7

Chapter 5. Week 4 91. Functions, Slope Fields, Converting Higher Order Differential Equations

to Systems of First Order Differential Equations 92. Worksheet 9

Chapter 6. Week 5 111. Discussion Topics 112. Worksheet 11

Chapter 7. Week 6 151. Discussion Topics 152. Worksheet 15

Chapter 8. Week 7 191. Discussion Topics 192. Worksheet 19

Chapter 9. Week 8 231. Discussion Topics 232. Worksheet 23

Chapter 10. Week 9 251. Discussion Topics 252. Worksheet 25

Chapter 11. Week 10 271. Discussion Topics 272. Worksheet 27

vii

Page 7: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

viii CONTENTS

Chapter 12. Week 11 291. Discussion Topics 292. Worksheet 29

Chapter 13. Week 12 311. Discussion Topics 312. Worksheet 31

Chapter 14. Week 13 351. Discussion Topics 352. Worksheet 35

Chapter 15. Week 14 391. Discussion Topics 392. Worksheet 39

Page 8: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

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 integratingit 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 lin-ear algebra we will see that we have gained quite a lot. To approximate solutionsof this form, we will learn some basic algorithms and implement them in Matlab.Throughout, you are welcome work on implementing similar codes in other lan-guages that are good for computation, such as Fortran, Python or C++ if you areinterested.

1

Page 9: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s
Page 10: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

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 Matlabcapabilities.

3

Page 11: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s
Page 12: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

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× 104.c. Create z = eiπ/4.d. Evaluate and display (

√x+ y−1/4)π.

e. Evaluate and display log(zz̄).

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 =

10−2

10−1

100

101

102

.

c. Create and display the 1× 9 row vector

~z =[e−iπ e−i3π/4 e−iπ/2 e−iπ/4 e0 eiπ/4 eiπ/2 ei3π/4eiπ

].

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 Matlabcomponents like linspace, ones, zeros, diag, rand, floor, ceil). Do notsimply type all numbers into a matrix in the editor.

a. Create and display M1 =

1 . . . 1...

. . ....

1 . . . 1

, a 5× 5 matrix of all 1’s.

5

Page 13: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

6 3. WEEK 2

b. Create and display

M2 =

1 0 · · · 00 2 · · · 0...

. . ....

0 · · · 0 10

,a 10× 10 matrix with non-zero entries only on the diagonal.

c. Create and display M3 =

[5 0 5 00 5 0 5

].

d. Create and display M4, a 2× 2 matrix with random values between −2 and2.

e. Create and display M5, the 4× 4 matrix

M5 =

[M3

M4 M4

]

Page 14: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

CHAPTER 4

Week 3

1. Solving Linear Systems in Matlab, Introduction to Functions inMatlab

Matrices, Vectors, Gaussian Elimination, Solving Linear Systems inMatlab, Introduction to Functions in Matlab

2. Worksheet

1. Systems of equations. We will see how to use Matlab to solve the followinglinear system of equations.

5x1 + 2x2 + 3x3 = 0

x1 − 2x2 + x3 = 2

x1 + x2 − x3 = 1

In Matlab, use the operation A\b with

A =

5 2 31 −2 11 1 −1

the “coefficient matrix” and

b =

021

the right hand side. Create and display the solution vector

x =

x1x2x3

.By hand, verify that the output solves the system of equations (you do not needto turn this in).

2. Solve the following linear systems of equations. Display the unique solution ifone exists. If no solution exists display the phrase “No solution exists” anddisplay the least squares solution. If more than one solution exists display thephrase “Infinitely many solutions”. (Use implicit Matlab components like \,lsqr, rref). You do not need to use an “if” statement.

7

Page 15: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

8 4. WEEK 3

a.

5x1 + 2x2 + 3x3 = 1

x1 − 2x2 + x3 = 2

x1 + x2 − x3 = 1

b.

x1 + 2x2 = 1

−2x1 − 4x2 = −2

c.

5x1 + 2x2 + 3x3 = 1

x1 − 2x2 + x3 = 2

4x1 + 4x2 + 2x3 = 1

3. Functions. Write the following functions. (Each should be in its own file. Useimplicit Matlab components like exp, sqrt, log, tanh, factorial, abs).

a. f1(x) = x2e−x2

.

b. f2(x) = tanh(x)√1+x2

.

c. f3(x) = x− x3

3! .

d. For g = 1.0, L = 1.0,

f4(t, v, h) =

[v

gL sin(h)

].

e. f5(x) = 1√|x| | log x|2

.

4. Perform the following tasks using the functions from problem 3.

a. Evaluate and display f1(1).

b. Evaluate and display f2(0).

c. Evaluate and display f3(2).

d. Evaluate and display f4(1, 2, π).

e. Plot the curve y = f5(x) on the set x ∈ [2, 10]. Make the curve a red, dashedline, label the axes “x” and “f5(x)”, and title the plot “Week 3 Plot”.

Page 16: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

CHAPTER 5

Week 4

1. Functions, Slope Fields, Converting Higher Order DifferentialEquations to Systems of First Order Differential Equations

Making functions in Matlab, Plotting slope fields in Matlab, Solv-ing first order differential equations, converting second order differentialequations into a system of first order differential equations

2. Worksheet

1. Anonymous functions. Create anonymous Matlab functions for the givenmaps.a. Write the function g1 assuming one inputs the components of the input vector

(x1, x2, x3).

g1 : R3 → R3

such that

g1

x1x2x3

=

sin(x1 − x2)x3e

2x1

x21 + x22 + x23

.

Evaluate and display g1(1, 1, 2).

b. Write the function g1 assuming one inputs a vector ~x ∈ R3.

g2 : R3 → R3

such that

g2(~x) =

sin(x1 − x2)x3e

2x1

x21 + x22 + x23

.

Create and display the vector ~x = (1, 1, 2). Evaluate and display g2(~x).

2. Write the functions g1 and g2 from question 1 in their own separate functionfiles 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

Page 17: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

10 5. WEEK 4

a. Use Matlab to make a direction field for the given differential equation. Letx ∈ [−3, 3] and y ∈ [−3, 3]. (Use Matlab components like meshgrid andquiver).

b. On the same plot as your direction field plot three solution curves for theinitial conditions y(0) = −1, y(0) = 0, and y(0) = 1. Label the plots. (UseMatlab 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 ofthe form

y′ = f(t, y), y(t0) = y0 (3)

by giving the function f(t, y) as input. Even though Equation 3 only showsa first order equation, these methods can still be used to solve higher orderequations if they are entered correctly by allowing y and f to both be columnvectors instead of scalars. Convert the second order equation

y′′ + y′ + y2 = sin(t)

into a first order system of ODEs and create the Matlab function that inputs y asa 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′.

Page 18: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

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 func-tions from Homework 2:

• f1(x) = x2e−x2

.

• f2(x) = tanh(x)√1+x2

.

• f3(x) = x− x3

3! .

• f5(x) = 1√|x| | log x|2

.

a. Using a right Riemann sum with 10000 subintervals of equal size, approxi-mate ∫ 10

−10f1(x)dx (4)

(use implicit Matlab components linspace and sum).

b. Using a left Riemann sum with 10000 subintervals of equal size, approximate(4).

2. Numerical integration. Numerically integrate and display your solutions (ifone exists) to the following (use implicit Matlab components like quad, trapz,integral):

a. f1(x) from x = −10 to 10. Compare this to your answers from question 1.

b. f2 ∗ f3 from x = 0 to 2;

c. Investigate the integral of (f5(x))2 from x = 0 to 1;

d.√f5(x) from x = 0 to 1;

11

Page 19: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

12 6. WEEK 5

3. Fitting polynomials to data. Make the following plots in Matlab (use im-plicit Matlab components like polyfit, polyval, plot):

a. A cubic polynomial approximation to sinx on the interval from 0 to 2π.b. A quartic polynomial approximation to sinx on the interval from 0 to 2π.c. A cubic polynomial approximation to sinx on the interval from 0 to 3π.d. A quartic polynomial approximation to sinx on the interval from 0 to 3π.

Plot parts a and b on the same plot with a graph of the curve y = sinx. Dothe same thing for parts c and d. (What happens with part c?)

Page 20: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

2. WORKSHEET 13

4. Logistic growth model. A population grows according the the logistic modelgiven by the equation

dy

dt= 10 ∗ (1− y)(y − 2), t ≥ 0. (5)

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 initialconditions. Graph the solution curves on the same plot as your directionfield. (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.

Page 21: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s
Page 22: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

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 =

[a bc d

]is the quantity det(A) := ad − bc. A matrix is invertible if and only if it has anonzero determinant. A matrix that is not invertible is called singular.

a. Using a for loop and if/elseif/else statements, write a Matlab functioncalled invertiblePowers(A,n) that takes in a matrix A and a positive in-teger 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 nis a positive integer. If it is not print “n is not a positive integer” (a goodway to test this is by checking if floor(n)=ceil(n)). If A is a 2 × 2 ma-trix and n is a positive integer, then calculate the determinant of the first npowers of A: det(A),det(A2), . . . ,det(An) (do not use the implicit Matlabcomponent det), and use these values to determine if Ak is invertible fork = 1, 2, . . . , n. For each k let ak = det(Ak). Have your program print outeither “det(Ak) = ak so Ak is invertible” if ak 6= 0, or “det(Ak) = 0 so Ak issingular” if ak = 0 (just display the letter “A” as opposed to the actual nameof 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 andnumbers, n:

(i) A =

[1 2 34 5 6

], n = 4;

(ii) A =

[1 24 5

], n = −1.5;

(iii) A =

[1 24 5

], n = 3;

15

Page 23: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

16 7. WEEK 6

(iv) A =

[1 23 6

], n = 3.

2. Write a function called invertibleMat(A) that computes and displays the de-terminants of the matrix A. Use an if/then statement to print out either theterm “invertible” or “singular” based upon the outcome. Run the function onthe following matrices. (use implicit Matlab components like det, if, then, end,disp):

a.

M2 =

[1 23 4

]b.

M3 =

1 0 12 1 −14 3 2

c. M4 − I for

M4 =

1 1 0 00 1 0 00 0 1 10 0 0 1

3. Second order equation. Recall the differential equation from homework 3

y′′ + y′ + y2 = sin(t). (6)

Use ode45 and the function you created, myHw3Odefunc, to find the solutionto Eq. (6) with the initial conditions y(0) = 1, y′(0) = 2. Plot your solutioncurve on the interval t ∈ [0, 5].

4. Systems of First Order Linear Equations. A homogenous linear system ofdifferential equation with constant coefficients can be written in the form

d~x

dt= A~x (7)

where ~x(t) is an n×1 solution vector and A is a constant matrix. When n = 2 wecan draw a visualization in the x1x2-plane called the phase plane. We evaluateA~x at many points ~x and plot the resulting vectors (similar to a slope field). Aplot that includes many trajectories is called a phase portrait. Write a functioncalled 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. (Letx1 and x2 each be in the interval [−2.5, 2.5]. Use implicit Matlab componentsmeshgrid and quiver.)a.

A =

[1 24 1

]

Page 24: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

2. WORKSHEET 17

b.

A =

[−1 2−2 −1

]c.

A =

[1 04 2

]d.

A =

[−4 −1

2 −1

]

Page 25: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s
Page 26: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

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 × nmatrix A if there is some (nonzero) vector v ∈ Cn such that Av = λv. In thiscase, v is called an eigenvector of A associated with the eigenvalue λ. If thereare n linear independent eigenvectors for A then they form a basis of Cn.

Write a function called eigenBasis(A) that prints out the eigenvalues ofthe matrix A with corresponding eigenvectors. Then print out whether or notthe eigenvectors of A form a basis for the space Cn. Run the function on each ofthe 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 columnsare not linearly independent and therefore do not form a basis of Cn.

a.

M2 =

[1 23 4

]b.

M3 =

1 0 12 1 −14 3 2

c.

M4 =

1 1 0 00 1 0 00 0 1 10 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

dt= A~x. (8)

Use the function you made, directionField(A), to make a direction field forthe following matrices with x1 and x2 in the interval [−2.5, 2.5]. Then create afunction

19

Page 27: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

20 8. WEEK 7

phasePortrait(A,tmin,tmax,x1init,x2init) which uses ode45 to draw solu-tion curves to Equation (8) in the x1x2-plane. This is known as a phase portraitof the system. The function should take a 2× 2 matrix, A, minimum and max-imum t values, and initial conditions for x1(t) and x2(t). Your function shouldalso plot the initial condition point.

Use this function to create a phase portrait on the same plot as your directionfields for each matrix with the given initial conditions. Letting t go from 0 to 20should suffice for these example.a.

A =

[1 24 1

]

x1(0) x2(0)i. −1.75 2.5

ii. −2 2.5iii. 1.76 −2.5iv. 1.77 −2.5

b.

A =

[−1 2−2 −1

]

x1(0) x2(0)i. −2.5 0ii. 1.5 −2.5

iii. 2.5 0iv. −2.5 2

c.

A =

[1 04 2

]

x1(0) x2(0)i. −0.35 1

ii. 0.15 −0.5iii. 0.3 −1iv. 0 0.5

d.

A =

[−4 −1

2 −1

]

Page 28: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

2. WORKSHEET 21

x1(0) x2(0)i. −2.5 −2.3ii. −2.5 0

iii. 2.5 2.3iv. 2.5 −2.5

3. Pendulum. A simple physical problem that leads to a nonlinear, second orderdifferential equation is the oscillating pendulum. The angle θ that an oscillatingpendulum of length L makes with the vertical direction (see Figure 1) satisfiesthe equation

d2θ

dt2+g

Lsin θ = 0. (9)

Figure 1. An oscillating pendulum

a. Recall the function

f4(t, v, h) =

[v

gL sin(h)

]from homework 2. Use ode45 or ode15s and the function f4 to solve the pen-dulum equation, Eq. (9), for g = L = 1.0 and θ(0) = 0.1, θ′(0) = 0. (Whydo we use f4 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 toa really long pendulum? Use Matlab to solve the pendulum equation withthese parameters and the same initial conditions as part a. You may want toalter f4 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 youput a pendulum on the moon? Use Matlab to solve the pendulum equationwith these parameters and the same initial conditions as part a.

d. If θ is small then sin θ ≈ θ so Eq. (9) can be approximated by the linearequation

d2θ

dt2+g

Lθ = 0. (10)

Use Matlab to solve the linearized pendulum equation with the same initialconditions as part a. You may want to create a function similar to f4. Plotthe solution curve and compare it to the curve in part a.

Page 29: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

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 pertainingto each part in the following spots:

a db c

e. Create a phase diagram (plot of θ′ vs θ) for the pendulum equation withg = L = 1.0. Plot this graph in a separate figure.

Bonus: What is the relationship between the eigenvalues of a matrix A and thebehavior of the direction field for Equation (8) near the point (0, 0)? Send yourobservations in your email with your assignment. Hint: The four parts of question2 all behave in distinct ways near (0, 0).

Page 30: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

CHAPTER 9

Week 8

1. Discussion Topics

Writing your own ODE solvers

2. Worksheet

1. Euler’s method. (Forward or explicit) Euler’s method is a simple way toapproximate a solution to a differential equation of the form

y′ = f(t, y), y(t0) = y0 (11)

for a discrete set of points, yn. Suppose we want to approximate a solution to Eq.(24) over the interval t0 < t < T . The steps in Euler’s method are to first dividethe interval into N equal sections. Then tn = t0 + nh for n = 0, 1, . . . , N andh = (T − t0)/N . The values of yn are then found recursively with the formula

y0 = y(t0),

yn+1 = yn + hf(tn, yn).

a. Use a for loop construction to write your own ODE solver for the followingdifferential equation

y′ = y3 + 2t, y(0) = 0

(use implicit Matlab components like for, end) over the interval t = 0 tot = 1 implementing 50 uniform time steps of (explicit or forward) Euler’sMethod.

Display the values t0, t25, t50, y0, y25, and y50.

b. Solve the same equation using ode15s with a relative error tolerance of 1e−10letting Matlab set the number of time steps. (Use the implicit Matlab com-mands 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 orderto approximate a solution to Eq. (24). Each input variable is defined as they arein question 1 (t0=t0 and y0=y0). The outputs, t and y should be 1 × (N + 1)

23

Page 31: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

24 9. WEEK 8

vectors with t(n) = tn and y(n) = yn. Use your function to graph approximatesolutions to the equation

y′ = y3 + 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 mathematicalphysics. When λ is a nonnegative integer, n, the solution to Eq. (12) is a poly-nomial, Hn(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 differentialequation similar to myHw3OdeFunc.

n y(0) y′(0)0 1 01 0 22 −2 03 0 −12

Recall how to use an ODE solver backwards and forwards when the ini-tial conditions are not given at the beginning of your interval. Plot Hn(x),x ∈ [−1.8, 1.8] and y ∈ [−10, 10], for n = 0, 1, 2, 3 on the same graph. (Youmay want to use the implicit Matlab commands ode45 or ode15s to find so-lutions and wrev to help plot each piece correctly).

b. Use polyfit and your solutions in part a to find Hn(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).

Page 32: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

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 differen-tial 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 compo-nents 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 giveninitial condition with 1000 uniform steps and graph the approximate solution onthe 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 + y2), 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 similarto forward Euler’s method but the recursive formula is slightly different makingit an implicit method. We would like to approximate a solution to the equation

y′ = f(t, y), y(t0) = y0 (13)

over the interval t0 ≤ t ≤ T . To calculate yn using backward Euler, we use theformula

y0 = y(t0),

yn+1 = yn + hf(tn+1, yn+1).

where tn and h are the same as they were on homework 6. This is an implicitmethod because yn+1 appears on both sides of the equation. We must use alge-bra or some numerical method (Newton’s method) to find yn+1.

a. Run the function Eulers to approximate a solution to the differential equation

y′ = t2y, y(0) = 2. (14)

25

Page 33: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

26 10. WEEK 9

over the interval t = 0 to t = 2 implementing 100 uniform time steps. Plotthe solution curve.

b. Now write an ODE solver for Eq. (14) over the interval t = 0 to t = 2 imple-menting 100 uniform time steps of backward Euler’s method.

c. Solve the Eq. (14) by hand and plot your approximations from parts a andb along with the exact solution.

3. Newton’s Method. A standard numerical method for finding solutions tothe equation f(x) = 0 is known as Newton’s method. Suppose f : [a, b] → Ris differentiable and its derivative is given by f ′. To approximate a root of fwe first guess a root x0. We then recursively approximate the root using theequation

xn+1 = xn −f(xn)

f ′(xn). (15)

The method will usually converge assuming x0 is close enough to the root andf ′(x0) 6= 0.

Write a function root=newtonsMethod(f,df,x0,N) to perform N iterationsof Newton’s method given the functions f and df = f ′ and an initial guess x0for a root of f . Use your function for the following exercises and display theresults in a long format (use the Matlab command format).

a. Perform 1000 iterations of Newton’s method to find a root of f(x) = x2 − 2with x0 = 1.

b. Perform 1000 iterations of Newton’s method to find a root of f(x) = sinxwith x0 = 3.

c. Perform 1000 iterations of Newton’s method to find a solution to the equationof x2 = x+ 1 with x0 = 2.

d. Perform 1000 iterations of Newton’s method to find a solution to the equationof log(x2) = 2 with x0 = 2.

Bonus: Name the four constants you approximated in Question 3. Display thenames or symbols of the constants.

Page 34: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

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 theatmosphere near sea level. Let x(t) represent the distance of the object fromthe ground (in meters) where x = 0 is the ground level and x < 0 means theobject is above the ground (this assumption is made so the direction of positivevelocity, dx/dt, is downward). Assuming a constant acceleration due to gravityis 9.8 m/s2 and a drag coefficient of the object is γ kg/s, by Newton’s secondlaw of motion, the mathematical model of the object falling is given by

md2x

dt2+ γ

dx

dt= 9.8m. (16)

a. Rewrite the function [tout,yout]=Eulers(f,t0,y0,T,N) from Homework 7to run Euler’s method for any order differential equation that can be writtenas a system of first order differential equations of the form

~y′(t) = ~f(t, ~y), ~y(t0) = ~y0. (17)

The function should run N iterations of Euler’s method. tout should bean (N + 1) × 1 column vector such that tout(n + 1) = tn. For an order kdifferential equation, yout should be an (N + 1)× k matrix where yout(n+

1, k) = y(k−1)n (tn) (an approximation to the (k − 1)st derivative of y at tn).

b. Run your new Eulers function to solve Eq. (16) for an object of mass 1 kg anda drag coefficients 0.2 kg/s dropped from a height of 10 m (so x(0) = −10and x′(0) = 0). Plot the approximation found using 10000 step of Euler’smethod for t ∈ [0, 2.5].

c. Display the time at which the object hits the ground. (Find the first valueof 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 1m at a velocity of 10 m/s. Graph the approximation over the same intervalon the same plot as the graph for part b. Plot the line x = 0 to represent theground.

27

Page 35: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

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 y1(0) = 1 and y′1(0) = 0, then for y2(0) = 1 and y′2(0) = 1 onthe interval t ∈ [0, 2π] using 1000 iterations of Euler’s method. Graph yoursolutions on the same plot.

b. Compute the function

W (t) = det

[y1(t) y2(t)y′1(t) y′2(t)

](19)

at the values of t you approximated in part a. W (t) is called the Wronskianof y1 and y2.

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 verifi-cation of Abel’s Theorem.

3. a. Plot the function f(x) = ex − 10 cos(x) on the interval from 0 to 4. Find theinteger x0 in this interval such that f(x0) ≈ 0.

b. Use 10 iterations of the Newton’s method function you created in Homework7 using the x0 you found in part a.

c. Plot with x-marks the differences of the iterates (|x1 − x0|, |x2 − x1|, . . . ,|x10 − x9|). On the same figure plot with circles the points f(xn) for n =1, . . . , 10.

Page 36: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

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 bythe integral

Γ(p+ 1) =

∫ ∞0

e−xxp dx. (21)

The integral converges as x→∞ for all p.

a. Evaluate and display Γ(n) for n = 1, 2, . . . , 10; (use the implicit Matlab com-mand integral).

b. Evaluate and display n! for n = 0, 1, . . . , 9; (use the implicit Matlab commandfactorial).

If p is a positive integer, n, it can be shown that Γ(n + 1) = n!. Since Γ(p) isalso defined when p is not an integer, this function provides an extension of thefactorial 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 lightmoving 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

y(0) =1

32/3Γ(2/3), y′(0) =

−1

31/3Γ(1/3)

over the interval x ∈ [−10, 10]. Notice that the plot of the solution is oscilla-tory for x < 0 and exponentially decaying for x > 0 (you will need to adjustyour error tolerances to see the correct behavior).

29

Page 37: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

30 12. WEEK 11

b. Compare your function to the implicit Matlab function airy on the sameinterval using a plot (note, the Airy function in Matlab will have a tinyimaginary component, so you will need to take the real part using real).

3. Bessel Equation of Order 0. The Bessel equation of order 0,

x2y′′ + xy′ + x2y = 0, (23)

is related to modes of oscillation on discs and many other physical examples.The solutions to this equation define the Bessel functions J0(x) and Y0(x).

a. Use an ODE solver of your choice to solve the Bessel equation with initialconditions y(0) = 1 and y′(0) = 0 over the interval x ∈ [−10, 10]. Since the co-efficients 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 initialconditions y(0) = 0 and y′(0) = 1 over the interval x ∈ [−10, 10]. As is parta, we will need to replace the initial conditions. To approximate a solutionfor x > 0 use y(0.01) = −3 and y′(0.01) = 64. For x < 0 use y(−0.01) = −3and y′(−0.01) = −64.

c. Plot your solution functions and compare them to plots of the implicit Matlabfunctions besselj and bessely. (Use 0 for the order of the Bessel functionsof the first and second kind).

Page 38: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

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’smethod, 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 thevalues of the numerical approximation yn approach that corresponding valuesof the actual solution? Assuming this is true, we want to use step sizes thatare small enough to ensure accurate approximations, but not too small to slowdown calculations too much.

a. Recall the system of differential equations from problem 2a from Homework10

x′(t) = u(t)

y′(t) = v(t)

u′(t) =−x

x2 + y2

v′(t) =−y

x2 + y2

with initial value (x, y, u, v)(0) = (1, 0, 0, 1). This initial value problem hasan 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 problemfor N = 100, 200, 300, 400, 500, and 1000 over the time interval [0, 4π]. Usingsubplots 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 = 300N = 400 N = 500 N = 1000

b. Make a 1× 6 vector of the maximum error for each N in part a.

errorN = max{|xn − cos tn|, |yn − sin tn|, |un + sin tn|, |vn − cos tn|}.

31

Page 39: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

32 13. WEEK 12

Plot the maximum error as a function of N . (Use the implicit Matlab com-mand max.)

c. Use tic and toc to find the time taken per step of each method. You willhave to alter your Eulers code to do this. Display the total time elapsed andthe average time per step for each N .

2. The Runge-Kutta Method. Another explicit numerical method to solve theinitial value problem

y′ = f(t, y), y(t0) = y0 (24)

is called the Runge-Kutta method. The Runge-Kutta formula involves a weightedaverage of values of f(t, y) at different points in the interval tn ≤ t ≤ tn+1. It isgiven by

yn+1 = yn + h

(kn1 + 2kn2 + 2kn3 + kn4

6

)(25)

where

kn1 = f(tn, yn)

kn2 = f(tn +1

2h, yn +

1

2hkn1)

kn3 = f(tn +1

2h, yn +

1

2hkn2)

kn4 = f(tn + h, yn + hkn3).

The sum (kn1 + 2kn2 + 2kn3 + kn4)/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 valueproblem

y′ = 2t+ e−ty, y(0) = 1

on the interval t ∈ [0, 2] for N = 100, 200, 300, 500, and 1000. Graph yoursolutions 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 thefollowing 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.

Page 40: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

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 theexact solution on both plots). In the command window, display y(2) for eachsolution. (You should display seven values of y(2): the exact value and two foreach method corresponding to the two values of N). Also display the total timeit takes to run each method. (Use tic and toc.)

Page 41: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s
Page 42: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

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(t0) = y0 (26)

and let y = ϕ(t) denote its solution. By integrating the equation

ϕ′(t) = f(t, ϕ(t))

from tn to tn+1, we obtain∫ tn+1

tn

ϕ′(t) dt =

∫ tn+1

tn

f(t, ϕ(t)) dt,

or

ϕ(tn+1) = ϕ(tn) +

∫ tn+1

tn

f(t, ϕ(t)) dt. (27)

The integral in Eq. (27) is represented geometrically by the area under the curvein Figure 1 over the interval t ∈ [tn, tn+1]. If we approximate this area with thearea of the gray square then Eq. (27) becomes

ϕ(tn+1) = ϕ(tn) + (tn+1 − tn)f(tn, ϕ(tn)).

By replacing ϕ(tn) by yn and tn+1 − tn by h this gives

yn+1 = yn + hf(tn, yn). (28)

We recognize Eq. (28) as the formula used in forward Euler’s method. Thisvisualization allows us to see why our approximation to the solution using Euler’smethod improves as h gets closer to zero since a smaller h causes the area of theapproximating 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 shownin Figure 2 then Eq. (27) becomes

yn+1 = yn + hf(tn+1, yn+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

yn+1 = yn +h

2(f(tn, yn) + f(tn+1, yn+1)). (30)

35

Page 43: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

36 14. WEEK 13

tn tn+1

t

y′

y′ = f(t, ϕ(t))

f(tn, ϕ(tn))

Figure 1. Geometric representation of forward Euler’s method.

tn tn+1

t

y′

y′ = f(t, ϕ(t))

f(tn, ϕ(tn+1))

Figure 2. Geometric representation of backward Euler’s method.

Eq. 30 is an implicit formula because it has yn+1 on both sides of the equation.We can make it explicit by using Euler’s formula and replacing yn+1 on the righthand side by the right hand side of Eq. 28. This gives

yn+1 = yn +h

2(f(tn, yn) + f(tn+1, yn + hf(tn, yn))). (31)

Eq. (31) is known as the improved Euler formula, the Huen formula, or theexplicit 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’smethod, and the Runge-Kutta method for N = 100 and N = 1000 iterations.Approximate solutions with the improved Euler’s method for N = 100 andN = 1000. Graph the solutions on two figures and include the exact solutionon both plots. Display the approximate value of y(2) for each N and the

Page 44: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

2. WORKSHEET 37

tn tn+1

t

y′

y′ = f(t, ϕ(t))

f(tn, ϕ(tn+1))

f(tn, ϕ(tn))

Figure 3. Geometric representation of improved Euler’s method.

exact value of y(2) in the command window. Compare these approximationswith those from Homework 12.

2. Order of accuracy. Suppose we are numerically approximating a solution to

y′ = f(t, y), y(t0) = y0

and we assume our computer can execute all computations exactly, i.e., it canretain infinitely many digits at each step. The difference En between the solutiony = ϕ(t) and the numerical approximation yn at the point t = tn is given by

En = ϕ(tn)− yn.The error En is known as the global truncation error. A numerical solution toa differential equation is said to be kth-order accurate and the method used toobtain such a solution is said to be a kth order method if |En| is no greater thana constant times hk. In other words, for some constant C,

|En| ≤ Chk. (33)

Recall, for an approximation over the interval t ∈ [t0, T ] using numerical methodwith N iterations,

h =T − t0N

.

Consider the initial value problem

y′ = t2 − y, y(0) = 1. (34)

a. For each of the following methods, approximate the solution to Eq. (34) overthe 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 figurefor each method containing ten curves.

i. Forward Euler’s method.

Page 45: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

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 valueof the global truncation error, |En|. For each method, you should get ninepoints, one for each h. In a separate figure for each method, plot these pointsversus h.

Bonus (1 point): What is the order of each of the methods used in Question2? Send me your answers in the email with your assignment. (Hint: tryfitting polynomial curves of different orders to the graphs in part b until youhave what appears to be a best fit curve).

c. Using tic and toc find the total time it takes to run each method in parta for each value of h. You should alter your functions for each method toinclude the computation time as an additional output argument.

Make a table for each method displaying the maximum global truncationerror and computation time for each h. (You should end up with three tableseach with nine rows, one for each h, and three columns displaying h, themaximum global truncation error, and the computation time).

Page 46: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

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 equationsused to model the dynamics of a biological situation in which one species (thepredator) preys on another species (the prey). Denote by x and y the populationsof prey and predator, respectively, at time t. The populations change throughtime 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 theinteraction between the two species.

Notice that if x = 0 then y′ = −cy so the predator population decreases andeventually dies out. Similarly, if y = 0 then x′ = ax so the prey population growsexponentially. We can modify the predator-prey equations to the following

x′(t) = x(a1 − a2x− αy) (37)

y′(t) = y(c1 − c2y + γx). (38)

In this modified system, if y = 0 then x′ = x(a1 − a2x) and if x = 0then y′ = y(c1 − c2y) so the growth in each population is logistic instead ofexponential.

a. Consider the predator-prey equations

39

Page 47: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

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 simul-taneously 0. The predator-prey equations in this problem have four criticalpoints, one of which is (1, 1). Find the other three and plot all four criticalpoints 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 fieldand critical points. You may have to modify your Runge-Kutta function fromHomework 12 so it can solve a system of equations.

2. Adams Method. In every numerical procedure we have discussed so far theapproximation of yn = y(tn) relies only on the value of y−n− 1. In this method,we will see that the approximation of yn use the values of yn−1 and yn−2. Meth-ods that use information at more than the last approximated point are referredto as multistep methods.

The Adams-Bashforth formula is a multistep method designed around beingaccurate for polynomials of a chosen order. For a constant step size h, the second

Page 48: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

2. WORKSHEET 41

order Adams-Bashforth formula is given by

yn+1 = yn +3

2hf(tn, yn)− 1

2hf(tn−1, yn−1). (39)

Use this Adams-Bashforth algorithm to solve the Lorenz equations

x′ = y − xy′ = x(1− z)− yz′ = xy − z

(x, y, z)(0) = (1, 1, 0).

on time scale [0, 15]. Plot the three dimensional solution curve. Hint: approxi-mate y1 with a step of explicit Euler’s method.

The Lorenz equations were first studied as a model in meteorology and otherapplications of fluid dynamics. They concern the motion of a layer of fluid, suchas 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 numeri-cal method for solving ordinary differential equations that implements the mid-point rule. The algorithm is

yn+1 = yn +1

2h(f(tn+1, yn+1) + f(tn, yn)). (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/10ii. h = 1/30

iii. h = 1/40iv. 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 smallerrors that are introduced in the course of a numerical method will disappearas the procedure continues. Instability occurs if small errors tend to increase,perhaps unbounded.

If we are investigating an initial value problem numerically, we may intro-duce instabilities that were not part of the original problem. In other words, theerrors in our method cause the approximation of the solution to greatly differfrom the exact solution and to possibly increase unbounded. Avoidance of suchinstabilities 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 hgiven in Problem 3.

Page 49: Introduction to ODEs in Matlab - Benjamin Wilsonbwilson.web.unc.edu/files/2015/01/383L-Notes.pdfIntroduction to ODEs in Matlab Jeremy L. Marzuola ... was also taken on as part of JLM’s

42 15. WEEK 14

a. Forward Euler’s methodb. Backward Euler’s methodc. Improved Euler’s methodd. Runge-Kutta methode. Adams methodf. Crank-Nicolson method (already done in Problem 3)

Create a different plot for each value of h that contains all approximationsand the exact solution. Notice some of the approximations are unstable.

This problem is called stiff because a much smaller step size, h, is requiredfor stability than for accuracy. The backwards differentiation formulas are themost popular for dealing with stiff problems.