function [ret,x0,str]=lqgdemo1(t,x,u,flag);
%LQGDEMO1	is the M-file description of the SIMULINK system named LQGDEMO1.
%	The block-diagram can be displayed by typing: LQGDEMO1.
%
%	SYS=LQGDEMO1(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 LQGDEMO1 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 LQGDEMO1 with a FLAG of zero:
%	[SIZES]=LQGDEMO1([],[],[],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',[81,56,544,320])
     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/Scope',[sys,'/',['Floating',13,'Scope']])
set_param([sys,'/',['Floating',13,'Scope']],...
		'Vgain','2',...
		'Hgain','10.1',...
		'Window',[100,100,380,320],...
		'position',[40,172,60,198])

add_block('built-in/Note',[sys,'/',['LQG Demo. Start simulation.',13,'Try varying weighting parameters.']])
set_param([sys,'/',['LQG Demo. Start simulation.',13,'Try varying weighting parameters.']],...
		'position',[250,20,251,21])

add_block('built-in/Inport',[sys,'/','Inport'])
set_param([sys,'/','Inport'],...
		'Port','1',...
		'position',[45,35,65,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',[30,83,70,117])

add_block('built-in/Outport',[sys,'/','Outport'])
set_param([sys,'/','Outport'],...
		'Port','1',...
		'position',[400,15,420,35])

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',[215,172,280,228])

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


%     Subsystem  ['Plant with state',13,'feedback.'].

new_system([sys,'/',['Plant with state',13,'feedback.']])
set_param([sys,'/',['Plant with state',13,'feedback.']],'Location',[0,0,321,257])

add_block('built-in/State-space',[sys,'/',['Plant with state',13,'feedback./Plant']])
set_param([sys,'/',['Plant with state',13,'feedback./Plant']],...
		'A','a',...
		'B','b',...
		'C','[c;eye(length(a),length(a))]',...
		'D','[d;zeros(length(a),1)]',...
		'position',[70,42,150,88])

add_block('built-in/Inport',[sys,'/',['Plant with state',13,'feedback./u']])
set_param([sys,'/',['Plant with state',13,'feedback./u']],...
		'Port','1',...
		'position',[15,55,35,75])

add_block('built-in/Outport',[sys,'/',['Plant with state',13,'feedback./y']])
set_param([sys,'/',['Plant with state',13,'feedback./y']],...
		'Port','1',...
		'position',[255,45,275,65])

add_block('built-in/Demux',[sys,'/',['Plant with state',13,'feedback./Demux']])
set_param([sys,'/',['Plant with state',13,'feedback./Demux']],...
		'outputs','[1,length(a)]',...
		'position',[180,45,220,80])

add_block('built-in/Outport',[sys,'/',['Plant with state',13,'feedback./x']])
set_param([sys,'/',['Plant with state',13,'feedback./x']],...
		'Port','2',...
		'position',[260,105,280,125])
add_line([sys,'/',['Plant with state',13,'feedback.']],[40,65;65,65])
add_line([sys,'/',['Plant with state',13,'feedback.']],[155,65;175,65])
add_line([sys,'/',['Plant with state',13,'feedback.']],[225,55;250,55])
add_line([sys,'/',['Plant with state',13,'feedback.']],[225,70;235,70;235,115;255,115])


%     Finished composite block ['Plant with state',13,'feedback.'].

set_param([sys,'/',['Plant with state',13,'feedback.']],...
		'position',[230,72,260,123])

add_block('built-in/Note',[sys,'/',['State',13,'feedback']])
set_param([sys,'/',['State',13,'feedback']],...
		'position',[350,150,351,151])

add_block('built-in/Scope',[sys,'/','Output'])
set_param([sys,'/','Output'],...
		'Vgain','2',...
		'Hgain','10.1',...
		'Window',[193,304,473,524])
open_system([sys,'/','Output'])
set_param([sys,'/','Output'],...
		'position',[390,72,410,98])
add_line(sys,[210,200;120,200;120,110;140,110])
add_line(sys,[70,45;125,45;125,90;140,90])
add_line(sys,[75,100;140,100])
add_line(sys,[170,100;225,100])
add_line(sys,[265,110;320,110;320,200;285,200])
add_line(sys,[265,85;385,85])
add_line(sys,[365,85;365,25;395,25])
% 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
