3
AN ADAPTIVE RUNGE-KUTTA METHOD MATH 545, SPRING 2007 PROFESSOR LEMESURIER 1. The basic method for a single time step The starting point is the method of Runge and Kutta for advancing the solution of an ODE dy/dt = f (t, y) by one time step of length h, with local truncation error O(h 5 ), leading to global truncation error O(h 4 ), both as for Simpson’s rule. In fact for the ODE dy/dt = f (t), the Runge-Kutta method is the same as integration by Simpson’s rule. The algorithms are given in a mixture of Matlab and mathematical notation: the goal is to be as close as possible to the final Matlab program, but using mathematical notation where it is easier to understand. I include the semi-colons at the end of lines where Matlab needs them, and start index values at 1 as needed with Matlab. This mix is acceptable in your work so long as all mathematical details and notation are adequately explained in comments. The following algorithm is implemented (almost verbatim) in file rkstep.m as a Matlab function with calling format [tnew,ynew] = rkstep(f,t0,y0,h) Input: Function f (t, y) determining dy/dt = f (t, y); t 0 , the initial time; y 0 , the initial value y(t 0 ); h, the time step size. Output: tnew, the new time t 0 + h; ynew, the approximate value of y(tnew). f 1 = h · f (t 0 ,y 0 ); f 2 = h · f (t 0 + h/2,y 0 + f 1 /2); f 3 = h · f (t 0 + h/2,y 0 + f 2 /2); f 4 = h · f (t 0 + h, y 0 + f 3 ); ynew = y 0 +(f 1 +2 · f 2 +2 · f 3 + f 4 )/6; tnew = t 0 + h; Date : March 20, 2007.

Adaptive Runge Kutta

Embed Size (px)

Citation preview

Page 1: Adaptive Runge Kutta

AN ADAPTIVE RUNGE-KUTTA METHOD

MATH 545, SPRING 2007 PROFESSOR LEMESURIER

1. The basic method for a single time step

The starting point is the method of Runge and Kutta for advancing the solution of an ODEdy/dt = f(t, y) by one time step of length h, with local truncation error O(h5), leading toglobal truncation error O(h4), both as for Simpson’s rule. In fact for the ODE dy/dt = f(t),the Runge-Kutta method is the same as integration by Simpson’s rule.

The algorithms are given in a mixture of Matlab and mathematical notation: the goal is tobe as close as possible to the final Matlab program, but using mathematical notation whereit is easier to understand. I include the semi-colons at the end of lines where Matlab needsthem, and start index values at 1 as needed with Matlab.

This mix is acceptable in your work so long as all mathematical details and notation areadequately explained in comments.

The following algorithm is implemented (almost verbatim) in file rkstep.m as a Matlabfunction with calling format

[tnew,ynew] = rkstep(f,t0,y0,h)

Input:Function f(t, y) determining dy/dt = f(t, y);t0, the initial time;y0, the initial value y(t0);h, the time step size.Output:tnew, the new time t0 + h;ynew, the approximate value of y(tnew).

f1 = h · f(t0, y0);f2 = h · f(t0 + h/2, y0 + f1/2);f3 = h · f(t0 + h/2, y0 + f2/2);f4 = h · f(t0 + h, y0 + f3);ynew = y0 + (f1 + 2 · f2 + 2 · f3 + f4)/6;tnew = t0 + h;

Date: March 20, 2007.

Page 2: Adaptive Runge Kutta

2 MATH 545, SPRING 2007 PROFESSOR LEMESURIER

2. The basic method with fixed time step size

This version builds on the above with a simple loop, suing a fixed time step size h. For laterconvenience, the time step size is input, requiring possible adjustment to hit the desiredending time.

The following algorithm is implemented in file rkstepsizeh.m as a Matlab function withcalling format

[T,Y] = rkfixedstepsize(f,a,b,ya,h)

Input:f(t,y) determining the equation dy/dt = f(t, y)a, the initial timeb, the final timeya [ya], the initial value y(a)h, the time step size.

Output:array of values Ti, 1 ≤ i ≤ n, T0 = a, Tn = barray of values Yi approximating y(ti).

n = b(b − a)/hc;Note: n is the number of time steps, subject to revision later;Note: bxc is x rounded down to an integer.T1 = a;Y1 = ya;for i = 2 to n

[Ti, Yi] = rkstep(f,Ti−1,Yi−1,h);end forif Tn < b Note: steps of size h do not exactly reach time b.

h = b − Tn;n = n+1;[Tn, Yn] = rkstep(f,Tn−1,Yn−1,h);

end if

3. Error control with variable step size

This final version uses Richardson extrapolation at each time step to estimate errors.

If the error is too large, it redoes the step with reduced time step size;if the error is smaller than necessary, the step is accepted but the step size is increased forthe next step.

In each case, the new step size is computed to give an expected error half of the maximumallowed.

Minimum and maximum step sizes are specified: the minimum to ensure that the algorithmfinishes; the maximum to have adequate resolution in the output for graphs and such.

Page 3: Adaptive Runge Kutta

AN ADAPTIVE RUNGE-KUTTA METHOD 3

The following algorithm is implemented in file rkadaptivestepsize.m as a Matlab functionwith calling format

[T,Y] = rkadaptivestepsize(f,a,b,y0,hmin,hmax,errortol)

Input:f(t,y) determining the equation dy/dt = f(t, y)a, the initial timeb, the final timeya [ya], the initial value y(a)hmax the maximum time step sizehmin the minimum time step size;Etol the maximum tolerable error per unit time.

Output:array of values Ti, 1 ≤ i ≤ n, T0 = a, Tn = barray of values Yi approximating y(ti).

t = a; Note: t holds the current time value.y = ya; Note: y holds current y(t) value.T1 = t;Y1 = y;h = hmax; Note: h is the time step size: start optimistically!i = 1; Note: i is the step counter.while t < b

[tnew,ynew] = rkstep(f,t,y,h); Note: provisional new values, if accurate enough.Note: now do two steps of length h/2 for Richardson error estimate:[tmid,ymid] = rkstep(f,t,y,h/2);[thalfh,yhalfh] = rkstep(f,tmid,ymid,h/2);Eest = |yhalfh − ynew|/h; Note: estimated error per unit time; should be at most Etol.if Eest ≤ Etol or h == hmin Note: accept this step.

t = tnew;y = ynew;i = i+1;Ti = t;Yi = y;if h = hmin;

Warning: step accepted due to minimum step size, but error tolerance not met.end if

end ifNote: adjust step size for the next time step.h = h(Etol/(2Eest))

1/4;Note: This should give error about Etol/2: the safety factor 2 prevents infinite loops.if h < hmin, h = hmin;if h > hmax, h = hmax;if t + h > b, h = b − t; Note: Do not go beyond t=b.

end while