function [sys,x0,str,ts] = fazrov_auton2(t,x,u,flag,skt,x1min,x1max,x1num,x2min,x2max,x2num)

% NelinSys - a program tool for analysis and synthesis of nonlinear control systems
%            based on MATLAB/Simulink 5.2
%
% (C) 2002-2005, Martin Ondera (eskimo@pobox.sk) (Martin.Ondera@stuba.sk)

% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
%
% Please, see the LICENSE.TXT file to read the full license.

switch flag,

% =========================== Inicializacna cast s-funkcie  ============================== %
case 0
   
   % Inicializacia pociatocnych podmienok %
   if (x1num > 1)
      x1step = (x1max - x1min) / (x1num - 1);
   else
      x1step = 0;
   end

   if (x2num > 1)
      x2step = (x2max - x2min) / (x2num - 1);
   else
      x2step = 0;
   end

   % Definicia stavov, vstupov a vystupov %
   sizes = simsizes;
   sizes.NumContStates = 2 * x1num * x2num + 1;
   sizes.NumDiscStates = 0;
   sizes.NumOutputs = sizes.NumContStates;
   sizes.NumInputs = 0;
   sizes.DirFeedthrough = 0;
   sizes.NumSampleTimes = 1;
   sys = simsizes(sizes);

   % Nastavenie pociatocnych podmienok (PP) pre jednotlive stavy %
   j = 1;
   for k = 0 : (x1num - 1)
      for l = 0 : (x2num - 1)
         x0(j:j+1,:) = [x1min + k*x1step; x2min + l*x2step];
         j = j + 2;
      end
   end
   
   % Posledny stav je len priznak konca simulacie %
   x0(end+1,:) = 0;
   
   % Inicializacia "str" ako prazdna matica %
   str = [];

   % Sample times in TS %
   ts = [0 0];

% ========================== Vypocet podla 1. stavovej rovnice ========================== %
case 1

   % Vypocet pre systemy druheho radu, postupny vypocet x1,x2 pre rozne PP %
   j = 1;
   
   for k = 0 : (x1num - 1)
      for l = 0 : (x2num - 1)
         x1 = x(j);
         x2 = x(j+1);
         sys(j:j+1,:) = [x(j+1); eval(skt)];
         j = j + 2;
      end
   end
   
   % Ak sa riesenie dostalo mimo rozsah, nekresli dalej a nastav priznak konca simulacie %
   if any(isinf(sys)) | any(isnan(sys)) | any(~isreal(sys))
      sys = zeros(size(sys));
      sys(end+1,:) = 1;
   else
      sys(end+1,:) = 0;
   end

% ========================== Vypocet podla 2. stavovej rovnice ========================== %
case 3
   % Vystup vektora x1 - hodnoty x1 v danom case pri roznych PP %
   for j = 1 : (x1num * x2num)
      sys(j,:) = x(2*j-1);
   end
   
   % Vystup vektora x2 - hodnoty x2 v danom case pri roznych PP %
   for j = 1 : (x1num * x2num)
      sys(j+(x1num*x2num),:) = x(2*j);
   end
   
   % "Odovzdanie" priznaku konca simulacie na vystup %
   sys(end+1,:) = x(end);
   
% ================================= Nepouzite priznaky ================================== %
case {2, 4, 9}
   sys = [];

% ================================= Spracovanie chyby =================================== %
otherwise
   error(['Unknown flag = ',num2str(flag)]);
end