mathematics 22: lecture 11 - runge-kutta - furman...

30
Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman University January 25, 2008 Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 1 / 11

Upload: nguyendien

Post on 26-May-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Mathematics 22: Lecture 11Runge-Kutta

Dan Sloughter

Furman University

January 25, 2008

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 1 / 11

Page 2: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Order of approximations

I One may show that the error in Euler’s method is bounded by thestep-size h times a constant. We call Euler’s method a first-ordermethod.

I The modified Euler method is a second-order method: the error isbounded by a constant times h2.

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 2 / 11

Page 3: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Order of approximations

I One may show that the error in Euler’s method is bounded by thestep-size h times a constant. We call Euler’s method a first-ordermethod.

I The modified Euler method is a second-order method: the error isbounded by a constant times h2.

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 2 / 11

Page 4: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Runge-Kutta method

I Consider the initial-value problem

du

dt= f (t, u), u(t0) = u0.

I Divide [t0, t0 + T ] into N equal intervals of length h = TN .

I Let ti = t0 + ih, i = 0, 1, 2, . . . , N.

I Having computed u0, u1, . . . , ui , let

k1 = f (ti , ui )

k2 = f

(ti +

h

2, ui +

h

2k1

)k3 = f

(ti +

h

2, ui +

h

2k2

)k4 = f (ti + h, ui + hk3).

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 3 / 11

Page 5: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Runge-Kutta method

I Consider the initial-value problem

du

dt= f (t, u), u(t0) = u0.

I Divide [t0, t0 + T ] into N equal intervals of length h = TN .

I Let ti = t0 + ih, i = 0, 1, 2, . . . , N.

I Having computed u0, u1, . . . , ui , let

k1 = f (ti , ui )

k2 = f

(ti +

h

2, ui +

h

2k1

)k3 = f

(ti +

h

2, ui +

h

2k2

)k4 = f (ti + h, ui + hk3).

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 3 / 11

Page 6: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Runge-Kutta method

I Consider the initial-value problem

du

dt= f (t, u), u(t0) = u0.

I Divide [t0, t0 + T ] into N equal intervals of length h = TN .

I Let ti = t0 + ih, i = 0, 1, 2, . . . , N.

I Having computed u0, u1, . . . , ui , let

k1 = f (ti , ui )

k2 = f

(ti +

h

2, ui +

h

2k1

)k3 = f

(ti +

h

2, ui +

h

2k2

)k4 = f (ti + h, ui + hk3).

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 3 / 11

Page 7: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Runge-Kutta method

I Consider the initial-value problem

du

dt= f (t, u), u(t0) = u0.

I Divide [t0, t0 + T ] into N equal intervals of length h = TN .

I Let ti = t0 + ih, i = 0, 1, 2, . . . , N.

I Having computed u0, u1, . . . , ui , let

k1 = f (ti , ui )

k2 = f

(ti +

h

2, ui +

h

2k1

)k3 = f

(ti +

h

2, ui +

h

2k2

)k4 = f (ti + h, ui + hk3).

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 3 / 11

Page 8: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Runge-Kutta (cont’d)

I Let

ui+1 = ui +h

6(k1 + 2k2 + 2k3 + k4) .

I Runge-Kutta is a fourth-order method.

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 4 / 11

Page 9: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Runge-Kutta (cont’d)

I Let

ui+1 = ui +h

6(k1 + 2k2 + 2k3 + k4) .

I Runge-Kutta is a fourth-order method.

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 4 / 11

Page 10: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Example

I Consider the initial-value problem

du

dt= u cos(t), u(0) = 1.

on the interval [0, 6].

I Let h = 0.1 as before.

I For the first step, we have

k1 = (1.0) cos(0) = 1.0

k2 = (1.0 + (0.05)(1.0)) cos(0.05) = 1.0486878

k3 = (1.0 + (0.05)(1.0486878)) cos(0.05) = 1.0511191

k4 = (1.0 + (0.1)(1.0511191)) cos(0.1) = 1.0995910.

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 5 / 11

Page 11: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Example

I Consider the initial-value problem

du

dt= u cos(t), u(0) = 1.

on the interval [0, 6].

I Let h = 0.1 as before.

I For the first step, we have

k1 = (1.0) cos(0) = 1.0

k2 = (1.0 + (0.05)(1.0)) cos(0.05) = 1.0486878

k3 = (1.0 + (0.05)(1.0486878)) cos(0.05) = 1.0511191

k4 = (1.0 + (0.1)(1.0511191)) cos(0.1) = 1.0995910.

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 5 / 11

Page 12: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Example

I Consider the initial-value problem

du

dt= u cos(t), u(0) = 1.

on the interval [0, 6].

I Let h = 0.1 as before.

I For the first step, we have

k1 = (1.0) cos(0) = 1.0

k2 = (1.0 + (0.05)(1.0)) cos(0.05) = 1.0486878

k3 = (1.0 + (0.05)(1.0486878)) cos(0.05) = 1.0511191

k4 = (1.0 + (0.1)(1.0511191)) cos(0.1) = 1.0995910.

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 5 / 11

Page 13: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Example (cont’d)

I And so

u1 = 1.0 +0.1

6(1.0 + (2)(1.0486878) + (2)(1.0511191) + 1.0995910)

= 1.1049867.

I Note: the exact value is u(0.1) = esin(0.1) = 1.1049868.

I Recall: with Euler’s method we had u1 = 1.1 and with the modifiedEuler method we had u1 = 1.1047252.

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 6 / 11

Page 14: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Example (cont’d)

I And so

u1 = 1.0 +0.1

6(1.0 + (2)(1.0486878) + (2)(1.0511191) + 1.0995910)

= 1.1049867.

I Note: the exact value is u(0.1) = esin(0.1) = 1.1049868.

I Recall: with Euler’s method we had u1 = 1.1 and with the modifiedEuler method we had u1 = 1.1047252.

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 6 / 11

Page 15: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Example (cont’d)

I And so

u1 = 1.0 +0.1

6(1.0 + (2)(1.0486878) + (2)(1.0511191) + 1.0995910)

= 1.1049867.

I Note: the exact value is u(0.1) = esin(0.1) = 1.1049868.

I Recall: with Euler’s method we had u1 = 1.1 and with the modifiedEuler method we had u1 = 1.1047252.

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 6 / 11

Page 16: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Using Octave

I Runge-Kutta in Octave:

octave:1> function w = f(t,u)> w = u*cos(t);> endfunctionoctave:2> t = [0:0.1:6];octave:3> u(1) = 1.0;octave:4> for i = 1:60> k1 = f(t(i),u(i));> k2 = f(t(i)+0.05,u(i)+0.05*k1);> k3 = f(t(i)+0.05,u(i)+0.05*k2);> k4 = f(t(i)+0.1,u(i)+0.1*k3);> u(i+1) = u(i) + (0.1/6)*(k1 + 2*k2 + 2*k3 + k4);> endfor

I Note: u(6) ≈ u60 = 0.75623, which is exact to 5 decimal places.

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 7 / 11

Page 17: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Using Octave

I Runge-Kutta in Octave:

octave:1> function w = f(t,u)> w = u*cos(t);> endfunctionoctave:2> t = [0:0.1:6];octave:3> u(1) = 1.0;octave:4> for i = 1:60> k1 = f(t(i),u(i));> k2 = f(t(i)+0.05,u(i)+0.05*k1);> k3 = f(t(i)+0.05,u(i)+0.05*k2);> k4 = f(t(i)+0.1,u(i)+0.1*k3);> u(i+1) = u(i) + (0.1/6)*(k1 + 2*k2 + 2*k3 + k4);> endfor

I Note: u(6) ≈ u60 = 0.75623, which is exact to 5 decimal places.

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 7 / 11

Page 18: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Using Octave (cont’d)

I Comparison of exact (green) and approximate (red) solutions:

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 8 / 11

Page 19: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Octave: lsode

I The following commands use the built-in Octave function lsode tosolve our equation:

octave:1> function w = f(u, t)> w = u*cos(t);> endfunctionoctave:2> t = [0:0.1:6];octave:3> u = lsode("f",1.0,t);

I Note: u and t are reversed in the definition of f from our notation.

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 9 / 11

Page 20: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Octave: lsode

I The following commands use the built-in Octave function lsode tosolve our equation:

octave:1> function w = f(u, t)> w = u*cos(t);> endfunctionoctave:2> t = [0:0.1:6];octave:3> u = lsode("f",1.0,t);

I Note: u and t are reversed in the definition of f from our notation.

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 9 / 11

Page 21: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

lsode (cont’d)

I The method used by Octave is an adaptive step-size method.

I That is, the actual step-size (value of h) used varies as the integrationproceeds based on the behavior of the function.

I In particular, the values in the t vector do not determine the step size,but are there only for evaluation and plotting purposes.

I In particular, if one only wanted to know u(6), t could be specified byt = [0:6:6], in which case u(2) is the approximation to u(6).

I Or, we could just use u = lsode("f",1.0,[0 6]); to perform theevaluation.

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 10 / 11

Page 22: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

lsode (cont’d)

I The method used by Octave is an adaptive step-size method.

I That is, the actual step-size (value of h) used varies as the integrationproceeds based on the behavior of the function.

I In particular, the values in the t vector do not determine the step size,but are there only for evaluation and plotting purposes.

I In particular, if one only wanted to know u(6), t could be specified byt = [0:6:6], in which case u(2) is the approximation to u(6).

I Or, we could just use u = lsode("f",1.0,[0 6]); to perform theevaluation.

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 10 / 11

Page 23: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

lsode (cont’d)

I The method used by Octave is an adaptive step-size method.

I That is, the actual step-size (value of h) used varies as the integrationproceeds based on the behavior of the function.

I In particular, the values in the t vector do not determine the step size,but are there only for evaluation and plotting purposes.

I In particular, if one only wanted to know u(6), t could be specified byt = [0:6:6], in which case u(2) is the approximation to u(6).

I Or, we could just use u = lsode("f",1.0,[0 6]); to perform theevaluation.

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 10 / 11

Page 24: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

lsode (cont’d)

I The method used by Octave is an adaptive step-size method.

I That is, the actual step-size (value of h) used varies as the integrationproceeds based on the behavior of the function.

I In particular, the values in the t vector do not determine the step size,but are there only for evaluation and plotting purposes.

I In particular, if one only wanted to know u(6), t could be specified byt = [0:6:6], in which case u(2) is the approximation to u(6).

I Or, we could just use u = lsode("f",1.0,[0 6]); to perform theevaluation.

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 10 / 11

Page 25: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

lsode (cont’d)

I The method used by Octave is an adaptive step-size method.

I That is, the actual step-size (value of h) used varies as the integrationproceeds based on the behavior of the function.

I In particular, the values in the t vector do not determine the step size,but are there only for evaluation and plotting purposes.

I In particular, if one only wanted to know u(6), t could be specified byt = [0:6:6], in which case u(2) is the approximation to u(6).

I Or, we could just use u = lsode("f",1.0,[0 6]); to perform theevaluation.

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 10 / 11

Page 26: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Runge-Kutta in Maxima

I To approximate a solution in Maxima using Runge-Kutta:

I load("dynamics")I u:rk(u*cos(t),u,1.0,[t,0,6,0.1])$

I The resulting ordered pairs are in the variable u.

I To plot the result: wxplot2d([discrete,u])

I Note: In the lab use, plot2d([discrete,u])

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 11 / 11

Page 27: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Runge-Kutta in Maxima

I To approximate a solution in Maxima using Runge-Kutta:I load("dynamics")

I u:rk(u*cos(t),u,1.0,[t,0,6,0.1])$

I The resulting ordered pairs are in the variable u.

I To plot the result: wxplot2d([discrete,u])

I Note: In the lab use, plot2d([discrete,u])

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 11 / 11

Page 28: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Runge-Kutta in Maxima

I To approximate a solution in Maxima using Runge-Kutta:I load("dynamics")I u:rk(u*cos(t),u,1.0,[t,0,6,0.1])$

I The resulting ordered pairs are in the variable u.

I To plot the result: wxplot2d([discrete,u])

I Note: In the lab use, plot2d([discrete,u])

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 11 / 11

Page 29: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Runge-Kutta in Maxima

I To approximate a solution in Maxima using Runge-Kutta:I load("dynamics")I u:rk(u*cos(t),u,1.0,[t,0,6,0.1])$

I The resulting ordered pairs are in the variable u.

I To plot the result: wxplot2d([discrete,u])

I Note: In the lab use, plot2d([discrete,u])

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 11 / 11

Page 30: Mathematics 22: Lecture 11 - Runge-Kutta - Furman Universitymath.furman.edu/~dcs/courses/math22/lectures/lecture-11.pdf · Mathematics 22: Lecture 11 Runge-Kutta Dan Sloughter Furman

Runge-Kutta in Maxima

I To approximate a solution in Maxima using Runge-Kutta:I load("dynamics")I u:rk(u*cos(t),u,1.0,[t,0,6,0.1])$

I The resulting ordered pairs are in the variable u.

I To plot the result: wxplot2d([discrete,u])

I Note: In the lab use, plot2d([discrete,u])

Dan Sloughter (Furman University) Mathematics 22: Lecture 11 January 25, 2008 11 / 11