Cankaya Universitesi Fen-Edebiyat FakultesiJournal of Arts and Sciences Sayı 1 / Mayıs 2004
COMPARISON OF RUNGE-KUTTA METHODS OFORDER 4 AND 5 ON LORENZ EQUATION
Emre SERMUTLU 1
AbstractAn explicit MATLAB algorithm for the implementation of Runge-Kutta method of orders 4 and 5 is given. The running time and maxi-mum errors for the two methods are compared on Lorenz equation.
Keywords:Runge-Kutta, Lorenz, Numerical Solution, System of ODE.
Ozet4 ve 5. derece Runge-Kutta metodlarını kullanan acık bir MATLABprogramı verilmistir. Iki metodun calısma suresi ve maksimum hatalarıLorenz denklemi uzerinde karsılastırılmıstır.
Anahtar Kelimeler: Runge-Kutta, Lorenz, numerik cozum, difer-ansiyel denklem sistemi.
1. Introduction
Systems of ordinary differential equations are very common in manyapplied sciences and engineering problems as well as within mathemat-ics itself. Unfortunately, most of these equations are nonlinear whereasmost of the methods of solution are linear. This leaves numerical meth-ods of solution the only possibility in many cases.
The method of Runge-Kutta is one of the well-known numericalmethods for differential equations. The common versions are order4 and order 5. In this paper, we have written a MATLAB routinethat applies this method to any system of differential equation, in anydimensions, and finds solutions satisfying a give error bound.
The advantage of using such an explicit m-file rather than MAT-LAB’s built-in solvers like ode45 [1] is that we have greater controlin the inner workings of the program. We can specify the step size,maximum error level, time limit etc. We can start with more than one
1Address: Department of Mathematics and Computer Sciences,Cankaya University, 06530 Balgat - Ankara, Turkeye-mail: [email protected]
61
62 RUNGE-KUTTA ON LORENZ
initial points and plot the result. Also, the algorithm is more peda-gogical and may be used to illustrate the method of Runge-Kutta fornumerical analysis students.
2. Runge-Kutta Methods of Order 4 and 5
Consider the system of ordinary differential equations
(1)
dx1
dt= f1(x1, x2, . . . , xn, t)
dx2
dt= f2(x1, x2, . . . , xn, t)
......
dxn
dt= fn(x1, x2, . . . , xn, t)
Which can be expressed more simply by
(2)dx
dt= f(x, t)
where x is an n-dimensional vector.This is a non-autonomous equation. If we replace the right hand side
by f(x), that is, if we eliminate the explicit time-dependence, we willobtain an autonomous equation.
If the function f on the right hand side are nonlinear, we will needa numerical technique.
We can use Taylor expansions, but they require the calculation ofderivatives of the functions fi, which is impractical. The Runge-Kuttamethods have the same accuracy as Taylor at any order, yet there’s noneed for derivatives.
Suppose we know the x at the time ti. Let’s call it xi. Then wecalculate x at ti+1 = ti + h as follows:
RUNGE-KUTTA ON LORENZ 63
Runge-Kutta Method of Order 4
(3)
k1 = hf(ti, xi)
k2 = hf
(ti +
h
2, xi +
k1
2
)
k3 = hf
(ti +
h
2, xi +
k2
2
)k4 = hf(ti + h, xi + k3)
xi+1 = xi +1
6(k1 + 2k2 + 2k3 + k4)
Runge-Kutta Method of Order 5
(4)
k1 = hf(ti, xi)
k2 = hf
(ti +
h
2, xi +
k1
2
)
k3 = hf
(ti +
h
4, xi +
3k1 + k2
16
)
k4 = hf
(ti +
h
2, xi +
k3
2
)
k5 = hf
(ti +
3h
4, xi +
−3k2 + 6k3 + 9k4
16
)
k6 = hf
(ti + h, xi +
k1 + 4k2 + 6k3 − 12k4 + 8k5
7
)
xi+1 = xi +7k1 + 32k3 + 12k4 + 32k5 + 7k6
90
3. Lorenz Equation
The equation
(5)
dx1
dt= −10 x1 + 10 x2
dx2
dt= −x1 x3 + 23 x1 − x2
dx3
dt= x1 x2 −
8
3x3
64 RUNGE-KUTTA ON LORENZ
is a special case of Lorenz equation that exhibits chaotic orbits. Thehighly sensitive nature of the solution of the initial conditions wasdiscovered by Lorenz in 1963.[2]
Theoretical properties of Lorenz system, which was originally usedto model fluid circulation in atmosphere, is the subject of dynamicalsystems and chaos theory. [3]
In this paper, what is interesting about Lorenz equation is the diffi-culty of using numerical methods to reach a given error bound.
We have solved this equation with the initial condition
(6) x(0) =
01
20
for 0 6 t 6 20.This orbit is an example that shows sensitive dependence on initial
conditions.
4. The M-File
The following MATLAB file solves the Lorenz equation using RungeKutta Method of order 4 or 5.
By changing the first few lines, we can give the equation, initialpoint(s), step size, maximum running time, maximum error and theorder of the method. The program will halt if the error limit is satisfiedor time limit exceeded, whichever comes first.
The given system must be an autonomous system of equations.
%Fourth or Fifth order Runge-Kutta
%to solve the system of autonomous ODE x’=f(x)
%where x=(x1,x2,...,xn)
%prints the solution to matrix x.
tic;clear;
f=inline(’[-10*x(1)+10*x(2);-x(1)*x(3)+23*x(1)-x(2);
x(1)*x(2)-(8/3)*x(3)]’); %equation
x0=[0 1 20]’; %initial points
k=2000; %k+1 is the number of columns of the solution matrix x
dt=0.01; %step size of t
error=10^(-4); %maximum error of x
plotrange=[-16 16 -20 20 0 40]; %the region to be plotted
order=5; %the order of the Runge-Kutta method. Must be 4 or 5.
timelimit=3000; %maximum time allowed for the program to run.
dim=size(x0,1); %the dimension of the system
m=size(x0,2); %the number of initial points
tmax=timelimit/4;
RUNGE-KUTTA ON LORENZ 65
fprintf(’This program uses Runge-Kutta method of order %-2.0f\n’,order)
fprintf(’starting with the initial point %2.2f %2.2f %2.2f\n’,x0’)
fprintf(’from time t=0 to t=%-3.0f’,k*dt)
fprintf(’ with stepsize delta t=%2.12g\n’,dt)
fprintf(’It will continue until error falls below %2.12g’,error)
fprintf(’ or time limit %3.0f’,timelimit)
fprintf(’ seconds is exceeded\n\n\n’)
for q=1:m
x=zeros(dim,k+1);
x(:,1)=x0(:,q);
xprevious=repmat(inf,dim,k+1);
ttt=0;e=inf;p=0;
while ttt<tmax & e>error
n=2^p;
h=dt/n;
ta=0;xa=x0(:,q);
t0=clock;
for j=1:n*k
if order==4
k1=h*f(xa);
k2=h*f(xa+k1/2);
k3=h*f(xa+k2/2);
k4=h*f(xa+k3);
xa=xa+(k1+2*k2+2*k3+k4)/6;
ta=j*h;
elseif order==5
k1=h*f(xa);
k2=h*f(xa+k1/2);
k3=h*f(xa+(3*k1+k2)/16);
k4=h*f(xa+k3/2);
k5=h*f(xa+(-3*k2+6*k3+9*k4)/16);
k6=h*f(xa+(k1+4*k2+6*k3-12*k4+8*k5)/7);
xa=xa+(7*k1+32*k3+12*k4+32*k5+7*k6)/90;
ta=j*h;
else fprintf(’order must be 4 or 5\n’)
break
end
if mod(j,n)==0;
i=j/n;
x(:,i+1)=xa;
end
end
e=max(max(abs(x-xprevious)));
xprevious=x;
66 RUNGE-KUTTA ON LORENZ
fprintf(’step size = dt/%-2.0f’,2^p)
fprintf(’ = %2.12g\n’,h)
fprintf(’estimated error < %2.12g\n’,e)
ttt=etime(clock,t0);
fprintf(’time in seconds = %2.1f\n\n’,ttt)
if e<error
fprintf(’ERROR LIMIT SATISFIED\n\n’)
elseif ttt>tmax
fprintf(’TIME LIMIT EXCEEDED\n\n’)
end
p=p+1;
end
plot3(x(1,:),x(2,:),x(3,:));
axis(plotrange);
grid on;
end
fprintf(’total time in seconds = %2.1f\n’,toc)
5. Comparison of the two Methods
The program above has been run separately for orders 4 and 5. Theresulting errors and program runtime is given below:
Runge-Kutta 4 Runge-Kutta 5Step Size Steps Time Error Step Size Steps Time Error0.0025 8000 35.5 10.72 0.005 4000 27.0 26.50.00125 16000 70.9 1.40 0.0025 8000 53.8 2.970.000625 32000 141.8 0.11 0.00125 16000 107.4 0.0870.0003125 64000 282.9 0.0077 0.000625 32000 214.6 0.00270.00015625 128000 564.1 0.00050 0.0003125 64000 428.0 0.000083
Table 1. Comparison Data on the two methods
At first, order 4 seems to have an advantage, it gives more accurateresults for smaller running time, but as step sizes decrease, order 5gives better results.
When step size is halved, running time is doubled for both methods.On the other hand, the error decreases by 1/16 for order 4 and 1/32for order 5. This gives the method of order 5 superiority in the longrun.
The graph of the resulting solution is given below. The graphs forthe two methods are identical:
RUNGE-KUTTA ON LORENZ 67
Figure 1. Solution of Lorenz Equation for t = 0 − 20in 3-D
Figure 2. Projection of the Solution on xy-plane
68 RUNGE-KUTTA ON LORENZ
Figure 3. Projection of the Solution on xz-plane
Figure 4. Projection of the Solution on yz-plane
RUNGE-KUTTA ON LORENZ 69
6. Conclusion
The nonlinear systems of differential equations require using com-puter algebra systems.
Although package programs exist to solve such systems, an explicitroutine has certain advantages:
• Inner workings of the numerical methods will be clear, especiallyfor students.
• Parameters like maximum running times, step size etc. can becontrolled.
• Different methods can be contrasted and compared
In this paper we implemented Runge-Kutta methods of order 4 and5 by a MATLAB m-file, and compared them on the Lorenz equation.
For smaller step sizes, Runge-Kutta method of order 5 clearly givessmaller errors for a given running time.
In future, similar programs can be written to compare 1-step andmulti-step methods.
References
[1] Shampine, L. F. and M. W. Reichelt, 1997, The MATLAB ODE Suite SIAMJournal on Scientific Computing, 18:1-22
[2] Lorenz E., 1963,Deterministic nonperiodic flow, J. Atmospheric Science 20:130-141
[3] K.T.Alligood, T.D.Sauer,J.A.Yorke, 1996, Chaos: An Introduction to Dynami-cal Systems, Springer-Verlag
Recommended