function [ret,x0,str,ts,xts]=extrcont(t,x,u,flag);
%EXTRCONT	is the M-file description of the SIMULINK system named EXTRCONT.
%	The block-diagram can be displayed by typing: EXTRCONT.
%
%	SYS=EXTRCONT(T,X,U,FLAG) returns depending on FLAG certain
%	system values given time point, T, current state vector, X,
%	and input vector, U.
%	FLAG is used to indicate the type of output to be returned in SYS.
%
%	Setting FLAG=1 causes EXTRCONT to return state derivatives, FLAG=2
%	discrete states, FLAG=3 system outputs and FLAG=4 next sample
%	time. For more information and other options see SFUNC.
%
%	Calling EXTRCONT with a FLAG of zero:
%	[SIZES]=EXTRCONT([],[],[],0),  returns a vector, SIZES, which
%	contains the sizes of the state vector and other parameters.
%		SIZES(1) number of states
%		SIZES(2) number of discrete states
%		SIZES(3) number of outputs
%		SIZES(4) number of inputs.
%	For the definition of other parameters in SIZES, see SFUNC.
%	See also, TRIM, LINMOD, LINSIM, EULER, RK23, RK45, ADAMS, GEAR.

% Note: This M-file is only used for saving graphical information;
%       after the model is loaded into memory an internal model
%       representation is used.

% the system will take on the name of this mfile:
sys = mfilename;
new_system(sys)
simver(1.3)
if (0 == (nargin + nargout))
     set_param(sys,'Location',[177,80,510,533])
     open_system(sys)
end;
set_param(sys,'algorithm',     'RK-45')
set_param(sys,'Start time',    '0')
set_param(sys,'Stop time',     '10')
set_param(sys,'Min step size', '0.001')
set_param(sys,'Max step size', '0.01')
set_param(sys,'Relative error','0.001')
set_param(sys,'Return vars',   '')

add_block('built-in/Note',[sys,'/','Controllers'])
set_param([sys,'/','Controllers'],...
		'position',[150,5,155,10])


%     Subsystem  ['Double click',13,'on this for demo.5'].

new_system([sys,'/',['Double click',13,'on this for demo.5']])
set_param([sys,'/',['Double click',13,'on this for demo.5']],'Location',[-10,380,117,533])
set_param([sys,'/',['Double click',13,'on this for demo.5']],...
		'Mask Display','LQG\nDEMOS',...
		'Mask Dialogue','eval(''lqgdata; lqgdemos'')')


%     Finished composite block ['Double click',13,'on this for demo.5'].

set_param([sys,'/',['Double click',13,'on this for demo.5']],...
		'orientation',2,...
		'hide name',0,...
		'position',[120,174,191,210])

add_block('built-in/Note',[sys,'/','These require the Control System Toolbox.'])
set_param([sys,'/','These require the Control System Toolbox.'],...
		'position',[154,38,159,43])

add_block('built-in/Discrete State-Space',[sys,'/',['Discrete-Time state',13,'estimator with',13,'continuous cost',13,'function.']])
set_param([sys,'/',['Discrete-Time state',13,'estimator with',13,'continuous cost',13,'function.']],...
		'A','ae',...
		'B','be',...
		'C','ce',...
		'D','de',...
		'Sample time','Ts',...
		'Mask Display','Discrete Kalman\nestimator')
set_param([sys,'/',['Discrete-Time state',13,'estimator with',13,'continuous cost',13,'function.']],...
		'Mask Type','State estimator')
set_param([sys,'/',['Discrete-Time state',13,'estimator with',13,'continuous cost',13,'function.']],...
		'Mask Dialogue','Discrete Kalman state estimator\nfor continuous plant.\n(Requires Control Toolbox)|[A,B;C,D] Matrices:|Number of states:|State noise covar. E[ww'']:|Output noise covar. E[vv'']:|Sample time:')
set_param([sys,'/',['Discrete-Time state',13,'estimator with',13,'continuous cost',13,'function.']],...
		'Mask Translate','Ts=@5; n=@2; [ms,ns]=size(@1); a=@1(1:n,1:n); b=@1(1:n,n+1:ns); c=@1(n+1:ms,1:n); d=@1(1+n:ms,1+n:ns); [l,p]=lqed(a,eye(n,n),c,@3,@4,Ts); [ny,nu]=size(d); [ad,bd]=c2d(a,b,Ts); [ae,be,cexy,dexy]=destim(ad,bd,c,d,l,1:ny,1:nu); ce=cexy(ny+1:ny+n,:); de=dexy(ny+1:ny+n,:);')
set_param([sys,'/',['Discrete-Time state',13,'estimator with',13,'continuous cost',13,'function.']],...
		'Mask Help','Implements a discrete-time Kalman state estimator using lqed and estim commands. Mask and unmask this block to see how it works.')
set_param([sys,'/',['Discrete-Time state',13,'estimator with',13,'continuous cost',13,'function.']],...
		'Mask Entries','[a,b;c,d]\/length(a)\/0.001*b*b''\/eye(1)\/0.01\/',...
		'position',[50,235,150,285])

add_block('built-in/Discrete State-Space',[sys,'/',['Discrete-Time',13,'linear regulator',13,'with continuous',13,'cost function.']])
set_param([sys,'/',['Discrete-Time',13,'linear regulator',13,'with continuous',13,'cost function.']],...
		'A','[]',...
		'B','[]',...
		'C','[]',...
		'D','k',...
		'Sample time','Ts',...
		'Mask Display','K[n+1]')
set_param([sys,'/',['Discrete-Time',13,'linear regulator',13,'with continuous',13,'cost function.']],...
		'Mask Type','Discrete regulator')
set_param([sys,'/',['Discrete-Time',13,'linear regulator',13,'with continuous',13,'cost function.']],...
		'Mask Dialogue','Discrete linear regulator\nfor continuous plant.\n(Requires Control Toolbox)|[A,B;C,D] Matrices:|Number of states:|Output weighting, y''Qy:|Input weighting, u''Ru:|Sample time:')
set_param([sys,'/',['Discrete-Time',13,'linear regulator',13,'with continuous',13,'cost function.']],...
		'Mask Translate','Ts=@5; n=@2; [ms,ns]=size(@1); a=@1(1:n,1:n); b=@1(1:n,n+1:ns); c=@1(n+1:ms,1:n); d=@1(1+n:ms,1+n:ns); [k]=lqrd(a,b,c''*@3*c,@4+d''*@3*d,c''*@3*d,Ts); ')
set_param([sys,'/',['Discrete-Time',13,'linear regulator',13,'with continuous',13,'cost function.']],...
		'Mask Help','Implements a discrete-time linear quadratic regulator using lqrd command. Mask and unmask this block to see how it works.')
set_param([sys,'/',['Discrete-Time',13,'linear regulator',13,'with continuous',13,'cost function.']],...
		'Mask Entries','[a,b;c,d]\/length(a)\/10*eye(1,1)\/eye(1,1)\/0.01\/',...
		'position',[195,230,255,290])

add_block('built-in/State-Space',[sys,'/','State Estimator'])
set_param([sys,'/','State Estimator'],...
		'A','ae',...
		'B','be',...
		'C','ce',...
		'D','de',...
		'Mask Display','Kalman\nestimator',...
		'Mask Type','State estimator')
set_param([sys,'/','State Estimator'],...
		'Mask Dialogue','Continuous Kalman state estimator.\n(Requires Control Toolbox)|Plant A matrix:|Plant B matrix:|Plant C matrix:|Plant D matrix:|State noise covar. E[ww'']:|Output noise covar. E[vv'']:')
set_param([sys,'/','State Estimator'],...
		'Mask Translate','[nx,nu]=size(@2); [l,p]=lqe(@1,eye(nx,nx),@3,@5,@6); [ny,nx]=size(@3); [ae,be,cexy,dexy]=estim(@1,@2,@3,@4,l,1:ny,1:nu); ce=cexy(ny+1:ny+nx,:); de=dexy(ny+1:ny+nx,:);')
set_param([sys,'/','State Estimator'],...
		'Mask Help','Implements a Kalman state estimator using lqe and estim commands. Mask and unmask this block to see how it works.',...
		'Mask Entries','a\/b\/c\/d\/0.001*b*b''\/0.01*eye(1,1)\/')
set_param([sys,'/','State Estimator'],...
		'position',[40,85,130,135])

add_block('built-in/State-Space',[sys,'/',['Feedback Gain ',13,'using LQR Design.']])
set_param([sys,'/',['Feedback Gain ',13,'using LQR Design.']],...
		'A','[]',...
		'B','[]',...
		'C','[]',...
		'D','k',...
		'Mask Display','K',...
		'Mask Type','Regulator')
set_param([sys,'/',['Feedback Gain ',13,'using LQR Design.']],...
		'Mask Dialogue','Linear quadratic regulator.\n(Requires Control Toolbox)|Plant A matrix:|Plant B matrix:|Plant C matrix:|Plant D matrix:|Output weighting, y''Qy:|Input weighting, u''Ru:')
set_param([sys,'/',['Feedback Gain ',13,'using LQR Design.']],...
		'Mask Translate','[k]=lqry(@1,@2,@3,@4,@5,@6);',...
		'Mask Help','Implements a linear state quadratic regulator. Mask and unmask this block to see how it works.')
set_param([sys,'/',['Feedback Gain ',13,'using LQR Design.']],...
		'Mask Entries','a\/b\/c\/d\/eye(1,1)\/eye(1,1)\/',...
		'position',[190,75,260,135])

add_block('built-in/Discrete State-Space',[sys,'/','S-Domain to Z-Domain'])
set_param([sys,'/','S-Domain to Z-Domain'],...
		'A','ad',...
		'B','bd',...
		'C','c',...
		'D','d',...
		'Sample time','Ts',...
		'Mask Display','dpoly(nd,dd,''z'')',...
		'Mask Type','c2d')
set_param([sys,'/','S-Domain to Z-Domain'],...
		'Mask Dialogue','Vector expressions for numerator and \ndenominator in the s-domain\n(Requires Control Toolbox).|Numerator:|Denominator:|Sample time:')
set_param([sys,'/','S-Domain to Z-Domain'],...
		'Mask Translate','[a,b,c,d]=tf2ss(@1,@2); Ts=@3; [ad,bd]=c2d(a,b,Ts(1)); [nd,dd]=ss2tf(ad,bd,c,d,1);')
set_param([sys,'/','S-Domain to Z-Domain'],...
		'Mask Help','This block takes expressions for numerator in the s-domain and converts them to the z-domain using zero-order hold approximation. Need control toolbox''s c2d command.')
set_param([sys,'/','S-Domain to Z-Domain'],...
		'Mask Entries','[1]\/[1 1]\/1\/',...
		'position',[125,349,190,391])

% Return any arguments.
if (nargin | nargout)
	% Must use feval here to access system in memory
	if (nargin > 3)
		if (flag == 0)
			eval(['[ret,x0,str,ts,xts]=',sys,'(t,x,u,flag);'])
		else
			eval(['ret =', sys,'(t,x,u,flag);'])
		end
	else
		[ret,x0,str,ts,xts] = feval(sys);
	end
end
