13 Numerical Solution of Ode'S
13 Numerical Solution of Ode'S
13 Numerical Solution of Ode'S
We see that k1 is the same change in x as given by the forward Euler rule. k2 is another
guess for the change in x, but with the slope calculated at the approximate midpoint [t +
13 NUMERICAL SOLUTION OF ODES 29
t/2, x(t) + k1 /2]. You can guess that this will be a somewhat more accurate result. Note,
however, that we have to evaluate the function twice in each time step, as opposed to once
each time step for the forward Euler method.
Here are your two problems:
1. The listed Runge-Kutta algorithm is third-order accurate in the step and second-order
accurate in the whole: in each step, we get a cancelation of all the second-order terms
in the Taylor series! Can you gure out how to show this?
Solution: We have to show that the Runge-Kutta formula captures up to second-order
terms in the Taylor series. First, the formula for k2 is itself approximated as a Taylor
series:
k2 = tf (t + t/2, x + k1 /2)
t f k1 f
= t f + + + h.o.t.
2 t 2 x
t f t f
= t f + + f + h.o.t. ,
2 t 2 x
where we again assume evaluation of functions at x(t) and t when the argument is
omitted. The rst relation comes from the bivariate Taylor series, and the second by
substituting in k1 . The acronym h.o.t. stands for higher-order terms. Next, write
out x(t + t) according to the last line of the RK2 rule:
x(t + t) = x + k2
t2 f f
= x + tf + + f + h.o.t.
2 t x
You recognize the rst two terms in the square brackets here as the total derivative
of (bivariate) f with t, which is the second derivative of x with time. How does this
result look compared to the direct Taylor series for x? We have for the univariate
Taylor series applied to x:
dx t2 dx2
x(t + t) = x + t + + h.o.t.
dt 2 dt2
So the RK2 formula does indeed capture the rst- and second-order terms of the Taylor
series. Note that the h.o.t. that appears when we worked with k2 is not the same as
the h.o.t. that appears in the direct expansion of x - so we cannot claim that the
third-order terms are captured in the RK2 rule. RK4 (shown below) captures both the
third- and fourth-order terms!
2. Compare the error convergence rates, for the forward Euler and the second-order
Runge-Kutta on the simple problem dx/dt = x, x(t = 0) = 1. To do this, you
will make several simulations with each method, over the time scale [0, 5], and with
13 NUMERICAL SOLUTION OF ODES 30
t = [0.0125 0.025 0.05 0.10 0.20 0.50 1.00]. For each, record the maximum absolute
error with respect to the exact solution (a decaying exponential), and then make a
summary plot of t versus error, including both methods. You should be able to see
the first-order convergence of forward Euler and the second-order convergence of RK2.
As an aside, forward Euler has stability problems that are more severe than for the Runge-
Kutta family, and this is another reason why forward Euler should usually not be your first
choice.
Please do not use the canned MATLAB ODE functions to solve the second problem. I am
asking you to make the explicit calculations for programming practice, to see the error rates
first-hand, and because we sometimes dont have MATLAB available where needed, e.g., on
an embedded processor.
Solution: See MATLAB results and code below. The Euler method is first-order: a ten-
fold reduction in t gives a ten-fold reduction in error. RK2 is second-order: the ten-fold
reduction in t gives a one-hundred-fold reduction in error. Most impressive, the RK4 is
fourth-order so we get a ten-thousand-fold improvement for a ten-fold reduction in t. Note
that RK4 requires four evaluations per time step, and RK2 requires two. But it is usually
well worth it! Finally, notice that the RK2 and the forward Euler both give awful errors
with t = 1. This is no surprise, since the time constant of the system is one also. The RK4
gives us a much better result here, the result of those extra evaluations.
0
10
2
10
4
10
max |error|
6
10
8
10
10
10 Euler
RK2
RK4
12
10
2 1 0
10 10 10
dt
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function simEulerRK2
clear all;
figure(1);clf;hold off;
figure(2);clf;hold off;
subplot(Position,[.2 .2 .5 .5]);
fn = fopen(simEulerRK2.dat,w);
% x = f(t,x)
t = timevector(i) ;
end;
t = timevector(i) ;
k1 = dt*f(t,xRK2(i)) ;
k2 = dt*f(t+dt/2, xRK2(i)+k1/2) ;
xRK2(i+1) = xRK2(i) + k2 ;
end;
13 NUMERICAL SOLUTION OF ODES 33
t = timevector(i) ;
k1 = dt*f(t,xRK4(i)) ;
k2 = dt*f(t+dt/2, xRK4(i)+k1/2) ;
k3 = dt*f(t+dt/2, xRK4(i)+k2/2) ;
k4 = dt*f(t+dt, xRK4(i)+k3) ;
end;
figure(1);
plot(timevector,log10(abs(xEuler-exact)),b,...
timevector,log10(abs(xRK2-exact)),r,...
timevector,log10(abs(xRK4-exact)),g,LineWidth,2) ;
hold on;
grid;
disp(sprintf(...
max(abs(xRK4-exact))));
fprintf(fn,...
max(abs(xRK4-exact)));
figure(2);
loglog(dt, (max(abs(xEuler-exact))),o,...
dt,(max(abs(xRK2-exact))),s, ...
dt, (max(abs(xRK4-exact))),d,LineWidth,2);
hold on;
if dt == max(dtvector),
legend(Euler,RK2,RK4,4);
xlabel(dt);
ylabel(max |error|);
grid;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.