function [sys, x0] = dsfunc(t,x,u,flag)
%DSFUNC	An example M-file for defining a discrete system.  
%	[SYS,X0] = DSFUNC(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 (empty matrix for discrete). 
%	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 (always empty for discrete). 
%	-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 DSFUNC can be called with no
%	right hand arguments (or a FLAG value of zero). DSFUNC then returns 
%	a vector of system sizes, SIZES=DSFUNC,  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).

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


%	SYS=DSFUNC(T,X,U,FLAG,FPARAM1,FPARAM2,.) allows function parameters
%	FPARAM1, FPARAM2, ... to be passed directly to DSFUNC 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]=DSFUNC; returns vectors X0 which are the initial
%	conditions of state vector, X.	
%	[SIZES,X0,XSTR]=DSFUNC;  returns string vector XSTR which indicate 
%	the ordering of the state vector.	

% NOTES: 
%   (1) Setting FLAG=2 is used to indicate to DSFUNC 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 effeciency in the
%	case where outputs and derivatives are required at the same point. 
%	    

% Here is an example of how to create a set of discrete equations: 
%	x(n+1) = Ax(n) + Bu(n)
%	y(n)   = Cx(n) + Du(n)

% Generate a discrete linear system:
A=[-1.3839   -0.5097
    1.0000         0];
B=[-2.5559         0
         0    4.2382];
C=[      0    2.0761
         0    7.7891];
D=[   -0.8141   -2.9334
       1.2426         0];

sample=1;	  % Sample time
ns=0;             % Number of continuous states
nd=length(A);     % Number of discrete states
[no,ni]=size(D);  % Number of inputs and outputs. 

% Linear Systems Description
if  abs(flag)==2
	sample_hit = (abs(rem(t,sample)) < sample/1e6);
	if sample_hit 
		xn=A*x(1:nd)+B*u;
		y=C*x(1:nd)+D*u;
		sys=[xn;y];
	else
		sys = x;
	end
elseif flag == 3
	dx=x;               
	sys=dx(nd+1:nd+no);
elseif flag ==4, 
	sys=ceil(t/sample+sample/1e8)*sample;
elseif flag == 0,
	sys=[ns,nd+no,no,ni,0,1]; x0 = ones(nd+no,1);
end
