function  [sys,x0,str,ts] = simplesys(t,x,u,flag,V,g,xIC)
%   function [sys,x0,str,ts] = simplesys(t,x,u,flag,V,g,xIC)
%   
%   Version 1.0, 3/21/07
%   S-file for 2-degree of freedom aircraft simulation.
%   V is the constant speed, g is the accel of gravity
%   xIC is the initial condition on the state vector.
% States
%   x=[x,y,psi]
%   Units are [ft, ft, rad]
%   x is north position (ft)
%   y is east position ft
%   psi is the heading angle
%   phi is the bank angle
% Inputs
%   u=[phi]
%   Units are [rad]
% Outputs
%   y=[x,y,psixdot,ydot]
%   Units are [ft, ft, rad,ft/sec,ft/sec]

switch flag,      
  case 0,         % Initialization
    [sys,x0,str,ts]=simplesysInitializeSizes(xIC);
  case 1,         % Compute derivatives of continuous states
    sys=simplesysDerivatives(t,x,u,V,g) ;
  case 2,
    sys=simplesysUpdate(t,x,u);
  case 3,
      sys=simplesysOutputs(t,x,u,V,g);  % Compute output vector
  case 4,                   % Compute time of next sample
    sys=simplesysGetTimeOfNextVarHit(t,x,u);
  case 9,                   % Finished. Do any needed 
    sys=simplesysTerminate(t,x,u);
  otherwise                 % Invalid input
    error(['Unhandled flag = ',num2str(flag)]);
end
 
%***********************************************************
%*                    simplesysInitializeSizes                   *
%***********************************************************
function [sys,x0,str,ts]=simplesysInitializeSizes(xIC)
% Return the sizes of the system vectors, initial 
% conditions, and the sample times and offets.
sizes = simsizes;   % Create the sizes structure
sizes.NumContStates  = 3;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     = 5;
sizes.NumInputs      = 1;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;   % at least one sample time is needed
sys = simsizes(sizes);      % load sys with the sizes structure
x0  = xIC;    % Specify initial conditions for all states 
str = [];    % str is always an empty matrix
ts  = [0 0]; %initialize the array of sample times


%***********************************************************
%*                    simplesysDerivatives                       *
%***********************************************************
function sys=simplesysDerivatives(t,x,u,V,g)
% Compute derivatives of continuous states
% Get elements of the state vector
xft=x(1);   yft=x(2);     psi=x(3); phi=u(1);
xdot=V*cos(psi);
ydot=V*sin(psi);
psidot=g*tan(phi)/V;
% Assemble the entire derivative vector.
sys = [xdot,ydot,psidot];



%***********************************************************
%*                    simplesysUpdate                            *
%***********************************************************
function sys=simplesysUpdate(t,x,u)
% Compute update for discrete states. If necessary, check for
% sample time hits.
sys = [];    % Empty since this model has no discrete states.



%***********************************************************
%*                    simplesysOutputs                           *
%***********************************************************
function sys=simplesysOutputs(t,x,u,V,g)
xft=x(1);   yft=x(2);     psi=x(3); phi=u(1);
xdot=V*cos(psi);
ydot=V*sin(psi);
sys = [xft,yft,psi,xdot,ydot];


   
       
%***********************************************************
%*                    simplesysGetTimeOfNextVarHit               *
%***********************************************************
function sys=simplesysGetTimeOfNextVarHit(t,x,u)
% Return the time of the next hit for this block.  Note that 
% the result is absolute time.  Note that this function is 
% only used when you specify a variable discrete-time sample
% time [-2 0] in the sample time array in sampleTime = 1;    
sys = [] ;




%************************************************************
%*                    simplesysTerminate                          *
%************************************************************
function sys=simplesysTerminate(t,x,u)
% Perform any necessary tasks at the end of the simulation
sys = [];

