Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lorenz 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Çankaya Üniversitesi Fen-Edebiyat Fakültesi

Journal of Arts and Sciences Sayı 1 / Mayıs 2004

COMPARISON OF RUNGE-KUTTA METHODS OF


ORDER 4 AND 5 ON LORENZ EQUATION

Emre SERMUTLU 1

Abstract
An 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.

Özet
4 ve 5. derece Runge-Kutta metodlarını kullanan açık bir MATLAB
programı verilmiştir. İki metodun çalışma süresi ve maksimum hataları
Lorenz denklemi üzerinde karşılaştırılmıştır.
Anahtar Kelimeler: Runge-Kutta, Lorenz, nümerik çözüm, difer-
ansiyel denklem sistemi.

1. Introduction
Systems of ordinary differential equations are very common in many
applied sciences and engineering problems as well as within mathemat-
ics itself. Unfortunately, most of these equations are nonlinear whereas
most 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 numerical
methods for differential equations. The common versions are order
4 and order 5. In this paper, we have written a MATLAB routine
that applies this method to any system of differential equation, in any
dimensions, 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 control
in 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
1
Address: Department of Mathematics and Computer Sciences,
Cankaya University, 06530 Balgat - Ankara, Turkey
e-mail: sermutlu@cankaya.edu.tr
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 for
numerical analysis students.

2. Runge-Kutta Methods of Order 4 and 5


Consider the system of ordinary differential equations

dx1
= f1 (x1 , x2 , . . . , xn , t)
dt
dx2
= f2 (x1 , x2 , . . . , xn , t)
dt
(1)
.. ..
. .

dxn
= fn (x1 , x2 , . . . , xn , t)
dt

Which can be expressed more simply by

dx
(2) = f (x, t)
dt

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 will
obtain an autonomous equation.
If the function f on the right hand side are nonlinear, we will need
a numerical technique.
We can use Taylor expansions, but they require the calculation of
derivatives of the functions fi , which is impractical. The Runge-Kutta
methods have the same accuracy as Taylor at any order, yet there’s no
need for derivatives.
Suppose we know the x at the time ti . Let’s call it xi . Then we
calculate x at ti+1 = ti + h as follows:
RUNGE-KUTTA ON LORENZ 63

Runge-Kutta Method of Order 4


k1 = hf (ti , xi )
 
h k1
k2 = hf ti + , xi +
2 2
 
(3) h k2
k3 = hf ti + , xi +
2 2

k4 = hf (ti + h, xi + k3 )

1
xi+1 = xi + (k1 + 2k2 + 2k3 + k4 )
6
Runge-Kutta Method of Order 5
k1 = hf (ti , xi )
 
h k1
k2 = hf ti + , xi +
2 2
 
h 3k1 + k2
k3 = hf ti + , xi +
4 16
 
h k3
(4) k4 = hf ti + , xi +
2 2
 
3h −3k2 + 6k3 + 9k4
k5 = hf ti + , xi +
4 16
 
k1 + 4k2 + 6k3 − 12k4 + 8k5
k6 = hf ti + h, xi +
7

7k1 + 32k3 + 12k4 + 32k5 + 7k6


xi+1 = xi +
90
3. Lorenz Equation
The equation

dx1
= −10 x1 + 10 x2
dt
dx2
(5) = −x1 x3 + 23 x1 − x2
dt
dx3 8
= x1 x2 − x3
dt 3
64 RUNGE-KUTTA ON LORENZ

is a special case of Lorenz equation that exhibits chaotic orbits. The


highly sensitive nature of the solution of the initial conditions was
discovered by Lorenz in 1963.[2]
Theoretical properties of Lorenz system, which was originally used
to model fluid circulation in atmosphere, is the subject of dynamical
systems 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
 
0
(6) x(0) =  1 
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 Runge
Kutta Method of order 4 or 5.
By changing the first few lines, we can give the equation, initial
point(s), step size, maximum running time, maximum error and the
order of the method. The program will halt if the error limit is satisfied
or 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. The
resulting errors and program runtime is given below:

Runge-Kutta 4 Runge-Kutta 5
Step Size Steps Time Error Step Size Steps Time Error
0.0025 8000 35.5 10.72 0.005 4000 27.0 26.5
0.00125 16000 70.9 1.40 0.0025 8000 53.8 2.97
0.000625 32000 141.8 0.11 0.00125 16000 107.4 0.087
0.0003125 64000 282.9 0.0077 0.000625 32000 214.6 0.0027
0.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 accurate


results for smaller running time, but as step sizes decrease, order 5
gives 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/32
for order 5. This gives the method of order 5 superiority in the long
run.
The graph of the resulting solution is given below. The graphs for
the two methods are identical:
RUNGE-KUTTA ON LORENZ 67

Figure 1. Solution of Lorenz Equation for t = 0 − 20


in 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 explicit
routine has certain advantages:
• Inner workings of the numerical methods will be clear, especially
for students.
• Parameters like maximum running times, step size etc. can be
controlled.
• Different methods can be contrasted and compared
In this paper we implemented Runge-Kutta methods of order 4 and
5 by a MATLAB m-file, and compared them on the Lorenz equation.
For smaller step sizes, Runge-Kutta method of order 5 clearly gives
smaller errors for a given running time.
In future, similar programs can be written to compare 1-step and
multi-step methods.
References
[1] Shampine, L. F. and M. W. Reichelt, 1997, The MATLAB ODE Suite SIAM
Journal 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

You might also like