Professional Documents
Culture Documents
An Algorithm Using PDF
An Algorithm Using PDF
NIKOLAOS S. CHRISTODOULOU
TEI of Chalkis
School of Technological Applications (STEF)
Gen. Department of Applied Sciences
Psahna Gr-34400, Greece
e-mail: nikolaos_christodoulou@yahoo.gr
Abstract
1. Introduction
y : R → Rm ,
In this paper, we have written a MATLAB code that applies RK4 and RK5
methods to Rössler system. However this code applies to any system of differential
equation, in any dimensions, and finds solutions satisfying a given error bound. The
advantage of using such an explicit m-file rather than MATLAB’s built-in solvers
like ode45 [6] is that we have better control in the inner workings of the program.
We can determine the step size, maximum error level, time limit, etc. We can start
with more than one initial point and plot the results. Also, the algorithm is more
pedagogical and may be used to illustrate the method of Runge-Kutta for numerical
analysis students.
⎧ x = −( y + z ) ,
⎪
⎨ y = x + ay, (2.1)
⎪
⎩ z = b + z ( x − c ).
While the equations look simple enough, they lead to wonderful trajectories,
some examples of which are illustrated in Section 5.
Rössler studied the chaotic attractor with a = 0.2, b = 0.2 and c = 5.7,
though properties of a = 0.1, b = 0.1 and c = 14 have been more commonly used
since.
In order to find the fixed points, the three Rössler equations (2.1) are set to zero
and the ( x, y, z ) coordinates of each fixed point were determined by solving the
resulting equations. This yields for a given set of parameter values the general
equations of each of the fixed point coordinates:
4 NIKOLAOS S. CHRISTODOULOU
As shown in Figure 1, one of these fixed points resides in the center of the
attractor loop and the other lies comparatively removed from the attractor.
Theoretical properties of Rössler system, is the subject of dynamical systems and
chaos theory [1]. In this paper, what is interesting about Rössler equations is the
difficulty of using numerical methods to reach a given error bound.
In this section, we present the numerical methods for the solution of (1.1) where
the vector valued functions are defined by (2.1). We will mainly concentrate on the
case where (2.1) is solved numerically by the 4th and 5th order Runge-Kutta
methods. The main purpose is to review the concepts and the structure of these
methods.
dy
= f (t , y ), y (t0 ) = y0 .
dt
Then, the RK4 method for this problem is given by the following equations:
1
yn +1 = yn + h(k1 + 2k 2 + 2k3 + k 4 ) ,
6
t n +1 = t n + h,
k1 = f (tn , yn ) ,
k 2 = f ⎛⎜ tn + h, yn + hk1 ⎞⎟ ,
1 1
⎝ 2 2 ⎠
AN ALGORITHM USING RUNGE-KUTTA METHODS OF ORDER … 5
k3 = f ⎛⎜ tn + h, yn + hk 2 ⎞⎟ ,
1 1
⎝ 2 2 ⎠
k 4 = f (tn + h, yn + hk3 ).
Thus, the next value ( y n +1 ) is determined by the present value ( y n ) plus the
product of the size of the interval (h ) and an estimated slope. The slope is a
weighted average of slopes:
• k 2 is the slope at the midpoint of the interval, using slope k1 to determine the
value of y at the point t n + h 2 using Euler’s method;
• k3 is again the slope at the midpoint, but now using the slope k 2 to determine
the y-value;
• k 4 is the slope at the end of the interval, with its y-value determined using
k3 .
In averaging the four slopes, greater weight is given to the slopes at the
midpoint:
1
slope = (k + 2k2 + 2k3 + k 4 ).
6 1
The RK4 method is a fourth-order method, meaning that the error per step is on
the order of h5 , while the total accumulated error has order h 4 .
Note that the above formulae are valid for both scalar- and vector-valued
functions, (i.e., y can be a vector and f an operator).
Similarly the 5th order Runge-Kutta method (RK5) for the same problem is
given by the following equations:
k1 = f (tn , yn ) ,
k 2 = hf ⎛⎜ tn + , yn + 1 ⎞⎟ ,
h k
⎝ 2 2⎠
3k + k 2 ⎞
k3 = hf ⎛⎜ tn + , yn + 1
h
16 ⎟⎠
,
⎝ 4
6 NIKOLAOS S. CHRISTODOULOU
k 4 = hf ⎛⎜ tn + , yn + 3 ⎞⎟ ,
h k
⎝ 2 2⎠
−3k 2 + 6k3 + 9k 4 ⎞
k5 = hf ⎛⎜ tn +
3h
, yn + ⎟,
⎝ 4 16 ⎠
4. MATLAB Code
The following MATLAB m-file solves the Rössler attractor 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.
5 tic; clear;
6 f = inline ( '[-y(2) - y(3); y(1) + 0.1 * y(2); 0.1 + y(3) * (y(1) - 14)]', 'y' ); % equation
7 order = 5; % The order of the Runge-Kutta method must be 4 or 5
8 y0 = [1 1 0]'; % This is the initial point
9 k = 5000; % k+1 is the number of columns of the solution matrix y
10 dt = 0.05; % The step size of t for the numerical method
11 error = 10ˆ(-4); % This is the maximum error of y
12 plotrange = [-20 15 -150 20 0 150]; % The region to be plotted
13 timelimit = 3000; % The maximum time allowed for the program to run.
14 dim = size(y0, 1); % The dimension of the system
15 m = size(y0,2); % The number of initial points
16 tmax = timelimit/4;
17
19 fprintf ('starting with the initial point (%2.2f %2.2f %2.2f\n', y0'))
AN ALGORITHM USING RUNGE-KUTTA METHODS OF ORDER … 7
20 fprintf ('from time t=0 to t = %-3.0f', k * dt)
21 fprintf ('The stepsize is Δ t = %2.12g\n', dt)
22 fprintf ('It will continue until the error falls below %2.12g', error)
23 fprintf ('or time limit %3.0f', timelimit)
24 fprintf ('seconds is exceeded\n\n\n')
25 for q = 1 : m
26 y = zeros (dim, k + 1);
27 y(:, 1) = y0 (:, q);
28 yprevious = repmat (inf, dim, k + 1);
29 ttt = 0;
30 e = inf;
31 p = 0;
32 while ttt<tmax & e>error
33 n = 2ˆp;
34 h = dt/n;
35 ta = 0;
36 ya = y0 (:, q);
37 t0 = clock;
38 for j = 1: n * k
39 if order = 4 % This is the 4th order RK method
40 k1 = h * f (ya);
41 k2 = h * f (ya + k1/2);
42 k3 = h * f (ya + k2/2);
43 k4 = h * f (ya + k3);
44 ya = ya + (k1 + 2 * k2 + 2 * k3 + k4)/6;
45 ta = j * h;
46 else if order = 5 % This is the 5th order RK method
47 k1 = h * f (ya);
48 k2 = h * f (ya + k1/2);
49 k3 = h * f (ya + (3 * k1 + k2)/16);
50 k4 = h * f (ya + k3/2);
51 k5 = h * f (ya+ (-3 * k2 + 6 * k3 + 9 * k4)/16);
52 k6 = h * f (ya + (k1 + 4 * k2 + 6 * k3-12 * k4 + 8 * k5)/7);
53 ya = ya + (7 * k1 + 32 * k3 + 12 * k4 + 32 * k5 + 7 * k6)/90;
54 ta = j * h;
55 else fprintf ('The order of the method must be 4 or 5\n')
56 break
57 end
58 if mod (j, n) = 0;
8 NIKOLAOS S. CHRISTODOULOU
59 i = j/n;
60 y (:, i+1) = ya;
61 end
62 end
63 e = max (max (abs (y-yprevious)));
64 yprevious = y;
65 fprintf ('The step size is dt/%-2.0f', 2ˆp)
66 fprintf (' = %2.12g\n', h)
67 fprintf ('The estimated error < %2.12g\n', e)
68 ttt = etime (clock, t0);
69 fprintf ('time in seconds = %2.1f\n\n', ttt)
70 if e<error
71 fprintf ('The error limit is satisfied\n\n')
72 elseif ttt>tmax
73 fprintf ('Time limit exceeded\n\n')
74 end
75 p = p + 1;
76 end
77 Figure (1)
78 plot 3 (y(1, :), y(2, :), y(3, :));
79 hold on
80 a = 0.1; b = 0.1; c = 14;
81 A1 = c + sqrt (cˆ2-4 * a * b); A2 = -c-sqrt (cˆ2-4 * a * b);
82 A3 = c-sqrt (cˆ2-4 * a * b); A4 = -c + sqrt (cˆ2-4 * a * b);
83 plot 3 (A1/2, A2/(2 * a), A1/(2 * a), 'r*') % plots fixed points
84 hold on
85 plot 3 (A3/2, A4/(2 * a), A3/(2 * a), 'r*') % plots fixed points
86 axis (plotrange);
87 grid on;
88 end
89 fprintf ('The total time in seconds is %2.1f\n', toc)
We study the Rössler chaotic attractor (2.1) with a = 0.1, b = 0.1 and c = 14.
The program in Section 4, has been run separately for orders 4 and 5 with initial
condition y 0 = (1 1 0 ) and step size 0, 05. The resulting errors and program runtime
is given below:
AN ALGORITHM USING RUNGE-KUTTA METHODS OF ORDER … 9
The graph of the resulting solutions is given in Figure 2. The graphs for the two
methods are identical. We see that the fixed point FP− given by (2.2) is located
in the middle of the attractor and is a saddle-focus with an unstable 2D manifold
(an unstable spiral mainly in the x-y plane) when the trajectory settles down onto a
chaotic attractor. The FP+ given by (2.2) is outside of the region of the attractor.
10 NIKOLAOS S. CHRISTODOULOU
The nonlinearity z ( x − c ) becomes active when the trajectory leaves the x-y plane.
The trajectory thus visits the neighborhood of the FP+ - also a saddle-focus - whose
1D unstable manifold sends the trajectory along the 1D stable manifold of the FP− .
A new cycle can then occur. With these parameter values of a, b and c, the trajectory
thus describes a chaotic attractor.
As shown in the general plots of the Rössler attractor above, one of these fixed
points resides in the center of the attractor loop and the other lies comparatively
removed from the attractor. This attractor has some similarities to the Lorenz
attractor, but is simpler and has only one manifold.
6. Conclusions
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.
References