function [sys, x0] = sfunc(t,x,u,flag)
%SFUNC  An example M-file for defining a system of ord. diff. eqns. (ODEs). 
%	[SYS, X0] = SFUNC(T,X,U,FLAG) is an example M-file to show how	
%	ordinary differential equations (ODEs) and/or 
%	a discrete system of equations can be defined in SIMULINK.. 
%	It describes the calling syntax for SIMULINK models.
%
%	SYS = SFUNC(T,X,U,FLAG) returns depending on FLAG certain system
%	values, SYS, given time point, T, current state vector, X, and
%	current input vector, U. FLAG is used to indicate the type of output  
%	to be returned in SYS:
%
%	FLAG	SYS	DESCRIPTION
%  	----	---	-----------
%	1	DX	state derivatives, dX/dT.
%	2	DS	discrete states, X(n+1).
%	3	Y	system outputs .
%	4	TNEXT	next time interval for update  (only discrete systems).
%	5	R	return the values of its root-functions.
%
%	For efficiency two other options have been added which tell the system 
%	that X, DS, T and U  are unchanged from the last call: 
%
%	-1 	DX 	state derivatives. 
%	-2 	DS	discrete states.
%
%	The state vector, X, should be  partitioned into continuous and discrete 
%	states, the first states containing the continuous states and the last  
%	states in X containing the discrete states.  
%	
%	To find out system characteristics the SFUNC can be called with no
%	right hand arguments (or a FLAG value of zero). SFUNC then returns 
%	a vector of system sizes, SIZES=SFUNC,  which 
%	contains the sizes of the state vector and other parameters:
%
%		SIZES(1) number of continuous states
%		SIZES(2) number of discrete states. 
%		SIZES(3) number of outputs 
%		SIZES(4) number of inputs 
%		SIZES(5) number of roots that the system has. 
%		SIZES(6) set to 1 if the system has direct feed-through of 
%			 its inputs (used for systems within systems).

%	SYS=SFUNC(T,X,U,FLAG,P1,P2, ....) allows function parameters
%	P1, P2, ... to be passed directly to SFUNC at each call.
%	These are used to allow local function parameters to be declared 
%	externally. 

%	Other system information can be extracted as follows:
%
%	[SIZES,X0]=SFUNC; returns vectors X0 which are the initial
%	conditions of state vector, X.	
%	[SIZES,X0,XSTR]=SFUNC;  returns string vector XSTR which indicate 
%	the ordering of the state vector.	

% NOTES: 
%   (1) Setting FLAG=2 is used to indicate to SFUNC that a true time update is 
%	taking place (as opposed to an exploratory step). Systems with memory,
%	discrete states and output displays should update their values only at 
%	this point. 
%   (2) Systems which do not have locally saved values should only consider 
%	abs(FLAG). Negative values of FLAG are only used for efficiency in the
%	case where outputs and derivatives are required at the same point. 

%	Copyright (c) 1990-94 by The MathWorks, Inc.
%	Andrew Grace 11-12-90.



% Here is an example of how to create a description of a linear system 
% described as state-space equations.

% Enter state space matrices. 
A=[-0.09   -0.01
    1          0];

B=[ 1   -7
    0   -2];

C=[ 0    2
    1   -5];

D=[-3    0
    1    0];


% If no right hand arguments then return the number of states, outputs 
% and inputs.
if nargin==0,  sys=[2,0,2,2,0,1]; x0 = zeros(2,1); return, end

%
% Linear Systems Description

if abs(flag) == 1
	sys = A*x + B*u;	% Derivatives
elseif flag == 3
	sys =  C*x + D*u; 	% Outputs
elseif abs(flag) == 0
	sys=[2,0,2,2,0,1]; x0 = zeros(2,1);
else
	sys = [];		% Real time update (ignored).
end
