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

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

bruce.shapiro.com
from bruce.shapiro.com More from this publisher
21.04.2015 Views

98 CHAPTER 5. RUNGE-KUTTA METHODS Figure 5.4: Comparison of absolute error at t = 1 as a function of step size for the test problem. Dashed Line: Euler’s Method; Solid Line: Runge-Kutta 4-stage method k 4 = y 0 + hf(t 1/2 , k 3 ) (5.81) = 1 + (0.5)(1.3125) = 1.65625 (5.82) y 1 = y 0 + h 6 (f(t 0, k 1 ) + 2f(t 1/2 , k 2 ) + 2f(t 1/2 , k 3 ) + f(t 1 , k 4 )) (5.83) = y 0 + h 6 (k 1 + 2k 2 + 2k 3 + k 4 ) (5.84) = 1 + .5 1 + 2(1.25) + 2(1.3125) + 1.65625 = 1.64844 (5.85) 6 Thus the numerical approximation to y(0.5) is y 1 ≈ 1.64844. For the second step, k 1 = y 1 = 1.64844 (5.86) k 2 = y 1 + h 2 f(t 1, k 1 ) (5.87) = 1.64844 + (0.25)(1.64844) = 2.06055 (5.88) k 3 = y 1 + h 2 f(t 1.5, k 2 ) (5.89) = 1.64844 + (0.25)(2.06055) = 2.16358 (5.90) k 4 = y 1 + hf(t 1.5 , k 3 ) (5.91) = 1.64844 + (0.5)(2.16358) = 2.73023 (5.92) y 2 = y 1 + h 6 (k 1 + 2k 2 + 2k 3 + k 4 ) (5.93) = 1.64844 + .5 1.64844 + 2(2.06055) + 2(2.16358) + 2.73023 = 2.71735 (5.94) 6 Math 582B, Spring 2007 California State University Northridge c○2007, B.E.Shapiro Last revised: May 23, 2007

CHAPTER 5. RUNGE-KUTTA METHODS 99 This gives us a numerical approximation of y(1) ≈ 2.71735, and error of approximately 0.034% (the exact value is e ≈ 2.71828. By comparison, a forward Euler computation with the same step size will yield a numerical result of 2.25, an error approximately 17%. Since it is an explicit method, the Runge-Kutta 4-stage method is very easy to implement in a computer, even though calculations are very tedious to do by hand. Here is a Mathematica implementation. RK4[f , {t0 , y0 }, h , tmax ] := Module[ {k1, k2, k3, k4, t, yval, r}, r = {{t0, y0}}; yval = y0; For[t = t0, t < tmax, t += h, k1 = yval; k2 = yval + (h/2) f[t, k1]; k3 = yval + (h/2)f[t + h/2, k2]; k4 = yval + h f[t + h/2, k3]; yval = yval + (h/6) *(f[t, k1] + 2f[t + h/2, k2] + 2 + h/2, k3] + f[t + h, k4]); AppendTo[r, {t + h, yval}]; ]; Return[r]; ] f[t 5.4 General Form of Runge-Kutta Methods The general form of the s-stage Runge-Kutta Method is s∑ y n = y n−1 + h b i K i (5.95) i=1 where ⎛ K i = f ⎝t n−1 + c i h, y n−1 + h s∑ j=1 a ij K j ⎞ ⎠ (5.96) and c i = s∑ a ij (5.97) j=1 The coefficients a ij , b j are chosen by comparing the method with a Taylor series expansion and choosing values that cancel specific terms in the error. There are many c○2007, B.E.Shapiro Last revised: May 23, 2007 Math 582B, Spring 2007 California State University Northridge

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

Figure 5.4: Comparison of absolute error at t = 1 as a function of step size for<br />

the test problem. Dashed Line: Euler’s Method; Solid Line: Runge-Kutta 4-stage<br />

method<br />

k 4 = y 0 + hf(t 1/2 , k 3 ) (5.81)<br />

= 1 + (0.5)(1.3125) = 1.65625 (5.82)<br />

y 1 = y 0 + h 6 (f(t 0, k 1 ) + 2f(t 1/2 , k 2 ) + 2f(t 1/2 , k 3 ) + f(t 1 , k 4 )) (5.83)<br />

= y 0 + h 6 (k 1 + 2k 2 + 2k 3 + k 4 ) (5.84)<br />

= 1 + .5 1 + 2(1.25) + 2(1.3125) + 1.65625 = 1.64844 (5.85)<br />

6<br />

Thus the numerical approximation to y(0.5) is y 1 ≈ 1.64844. For the second<br />

step,<br />

k 1 = y 1 = 1.64844 (5.86)<br />

k 2 = y 1 + h 2 f(t 1, k 1 ) (5.87)<br />

= 1.64844 + (0.25)(1.64844) = 2.06055 (5.88)<br />

k 3 = y 1 + h 2 f(t 1.5, k 2 ) (5.89)<br />

= 1.64844 + (0.25)(2.06055) = 2.16358 (5.90)<br />

k 4 = y 1 + hf(t 1.5 , k 3 ) (5.91)<br />

= 1.64844 + (0.5)(2.16358) = 2.73023 (5.92)<br />

y 2 = y 1 + h 6 (k 1 + 2k 2 + 2k 3 + k 4 ) (5.93)<br />

= 1.64844 + .5 1.64844 + 2(2.06055) + 2(2.16358) + 2.73023 = 2.71735 (5.94)<br />

6<br />

Math 582B, Spring 2007<br />

California State University Northridge<br />

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

Last revised: May 23, 2007

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

Saved successfully!

Ooh no, something went wrong!