function [ret,x0,str]=lqgdemo2(t,x,u,flag);
%LQGDEMO2	is the M-file description of the SIMULINK system named LQGDEMO2.
%	The block-diagram can be displayed by typing: LQGDEMO2.
%
%	SYS=LQGDEMO2(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 LQGDEMO2 to return state derivitives, FLAG=2
%	discrete states, FLAG=3 system outputs and FLAG=4 next sample
%	time. For more information and other options see SFUNC.
%
%	Calling LQGDEMO2 with a FLAG of zero:
%	[SIZES]=LQGDEMO2([],[],[],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.

%	Copyright (c) 1990-94 by The MathWorks, Inc.

% 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',[0,0,641,311])
     open_system(sys)
end;
set_param(sys,'algorithm',		'Linear')
set_param(sys,'Start time',	'0.0')
set_param(sys,'Stop time',		'1000')
set_param(sys,'Min step size',	'0.00001')
set_param(sys,'Max step size',	'0.2')
set_param(sys,'Relative error','1e-3')
set_param(sys,'Return vars',	'')

add_block('built-in/State-space',[sys,'/',['State Observer',13,'(try this instead of Kalman',13,'estimator)']])
set_param([sys,'/',['State Observer',13,'(try this instead of Kalman',13,'estimator)']],...
		'orientation',2,...
		'A','a',...
		'B','[b, zeros(4,1)]',...
		'C','eye(4)',...
		'D','[zeros(4,1),zeros(4,1)]',...
		'position',[480,210,560,260])

add_block('built-in/State-space',[sys,'/','State estimator'])
set_param([sys,'/','State estimator'],...
		'orientation',2,...
		'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\/1000*a''*a\/eye(1,1)\/')
set_param([sys,'/','State estimator'],...
		'position',[260,190,350,240])

add_block('built-in/Note',[sys,'/',['LQG Demo. Start simulation.',13,'Try varying weighting parameters.',13,'Try giving plant an additive perturbation.']])
set_param([sys,'/',['LQG Demo. Start simulation.',13,'Try varying weighting parameters.',13,'Try giving plant an additive perturbation.']],...
		'position',[340,20,341,21])

add_block('built-in/State-space',[sys,'/','Plant'])
set_param([sys,'/','Plant'],...
		'A','a',...
		'B','b',...
		'C','c',...
		'D','d',...
		'position',[300,77,380,123])

add_block('built-in/Inport',[sys,'/','Inport'])
set_param([sys,'/','Inport'],...
		'Port','1',...
		'position',[60,35,80,55])

add_block('built-in/Signal Generator',[sys,'/','Signal Gen.'])
set_param([sys,'/','Signal Gen.'],...
		'Peak','1.000000',...
		'Peak Range','5.000000',...
		'Freq','1.000000',...
		'Freq Range','5.000000',...
		'Wave','Sqr',...
		'Units','Rads',...
		'position',[45,83,85,117])

add_block('built-in/Outport',[sys,'/','Outport'])
set_param([sys,'/','Outport'],...
		'Port','1',...
		'position',[525,35,545,55])

add_block('built-in/State-space',[sys,'/',['Feedback gain ',13,'using LQR design.']])
set_param([sys,'/',['Feedback gain ',13,'using LQR design.']],...
		'orientation',2,...
		'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',[150,187,215,243])

add_block('built-in/Sum',[sys,'/','Sum'])
set_param([sys,'/','Sum'],...
		'inputs','++-',...
		'position',[190,82,210,118])

add_block('built-in/Mux',[sys,'/',['Inputs',13,'and sensors.']])
set_param([sys,'/',['Inputs',13,'and sensors.']],...
		'orientation',2,...
		'inputs','[1,1]',...
		'position',[390,194,420,236])

add_block('built-in/Note',[sys,'/','Known inputs'])
set_param([sys,'/','Known inputs'],...
		'position',[340,155,341,156])

add_block('built-in/Scope',[sys,'/','Output'])
set_param([sys,'/','Output'],...
		'Vgain','2',...
		'Hgain','10.1',...
		'Vmax','2',...
		'Hmax','20.2',...
		'Window',[0,0,280,220])
open_system([sys,'/','Output'])
set_param([sys,'/','Output'],...
		'position',[535,87,555,113])

add_block('built-in/Scope',[sys,'/',['Floating',13,'Scope']])
set_param([sys,'/',['Floating',13,'Scope']],...
		'Vgain','2',...
		'Hgain','10.1',...
		'Vmax','2',...
		'Hmax','20.2',...
		'Window',[100,100,380,320],...
		'position',[70,172,90,198])
add_line(sys,[145,215;125,215;125,110;185,110])
add_line(sys,[385,100;530,100])
add_line(sys,[505,100;505,45;520,45])
add_line(sys,[455,100;455,225;425,225])
add_line(sys,[85,45;160,45;160,90;185,90])
add_line(sys,[90,100;185,100])
add_line(sys,[215,100;295,100])
add_line(sys,[235,100;235,170;445,170;445,205;425,205])
add_line(sys,[385,215;355,215])
add_line(sys,[255,215;220,215])
% Return any arguments.
if (nargin | nargout)
	% Must use feval here to access system in memory
	if (nargin > 3)
		if (flag == 0)
			eval(['[ret,x0,xstr]=',sys,'(t,x,u,flag);'])
		else
			eval(['ret =', sys,'(t,x,u,flag);'])
		end
	else
		[ret,x0,str] = feval(sys);
	end
end
