Lorenz 2
Lorenz 2
Lorenz 2
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.
dx1
= f1 (x1 , x2 , . . . , xn , t)
dt
dx2
= f2 (x1 , x2 , . . . , xn , t)
dt
(1)
.. ..
. .
dxn
= fn (x1 , x2 , . . . , xn , t)
dt
dx
(2) = f (x, t)
dt
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
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
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.
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
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