21.04.2015 Views

The Computable Differential Equation Lecture ... - Bruce E. Shapiro

The Computable Differential Equation Lecture ... - Bruce E. Shapiro

The Computable Differential Equation Lecture ... - Bruce E. Shapiro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 5. RUNGE-KUTTA METHODS 99<br />

This gives us a numerical approximation of y(1) ≈ 2.71735, and error of approximately<br />

0.034% (the exact value is e ≈ 2.71828. By comparison, a forward Euler<br />

computation with the same step size will yield a numerical result of 2.25, an error<br />

approximately 17%.<br />

Since it is an explicit method, the Runge-Kutta 4-stage method is very easy to<br />

implement in a computer, even though calculations are very tedious to do by hand.<br />

Here is a Mathematica implementation.<br />

RK4[f , {t0 , y0 }, h , tmax ] := Module[<br />

{k1, k2, k3, k4, t, yval, r},<br />

r = {{t0, y0}};<br />

yval = y0;<br />

For[t = t0, t < tmax, t += h,<br />

k1 = yval;<br />

k2 = yval + (h/2) f[t, k1];<br />

k3 = yval + (h/2)f[t + h/2, k2];<br />

k4 = yval + h f[t + h/2, k3];<br />

yval = yval + (h/6) *(f[t, k1] + 2f[t + h/2, k2] + 2<br />

+ h/2, k3] + f[t + h, k4]);<br />

AppendTo[r, {t + h, yval}];<br />

];<br />

Return[r];<br />

]<br />

f[t<br />

5.4 General Form of Runge-Kutta Methods<br />

<strong>The</strong> general form of the s-stage Runge-Kutta Method is<br />

s∑<br />

y n = y n−1 + h b i K i (5.95)<br />

i=1<br />

where<br />

⎛<br />

K i = f ⎝t n−1 + c i h, y n−1 + h<br />

s∑<br />

j=1<br />

a ij K j<br />

⎞<br />

⎠ (5.96)<br />

and<br />

c i =<br />

s∑<br />

a ij (5.97)<br />

j=1<br />

<strong>The</strong> coefficients a ij , b j are chosen by comparing the method with a Taylor series expansion<br />

and choosing values that cancel specific terms in the error. <strong>The</strong>re are many<br />

c○2007, B.E.<strong>Shapiro</strong><br />

Last revised: May 23, 2007<br />

Math 582B, Spring 2007<br />

California State University Northridge

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!