function [ret,x0,str]=lqgdemo3(t,x,u,flag);
%LQGDEMO3	is the M-file description of the SIMULINK system named LQGDEMO3.
%	The block-diagram can be displayed by typing: LQGDEMO3.
%
%	SYS=LQGDEMO3(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 LQGDEMO3 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 LQGDEMO3 with a FLAG of zero:
%	[SIZES]=LQGDEMO3([],[],[],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',[8,57,608,362])
     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.1')
set_param(sys,'Max step size',	'0.1')
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',[45,172,65,198])

add_block('built-in/Scope',[sys,'/','Output'])
set_param([sys,'/','Output'],...
		'Vgain','2',...
		'Hgain','10.1',...
		'Window',[154,358,434,578])
open_system([sys,'/','Output'])
set_param([sys,'/','Output'],...
		'position',[530,97,550,123])


%     Subsystem  ['Sensor',13,'Noise'].

new_system([sys,'/',['Sensor',13,'Noise']])
set_param([sys,'/',['Sensor',13,'Noise']],'Location',[0,0,262,236])

add_block('built-in/Discrete Transfer Fcn',[sys,'/',['Sensor',13,'Noise/Zero Order Hold']])
set_param([sys,'/',['Sensor',13,'Noise/Zero Order Hold']],...
		'Numerator','[sqrt(Cov)]',...
		'Denominator','[sqrt(Ts)]',...
		'Sample time','Ts',...
		'position',[140,61,205,99])

add_block('built-in/White Noise',[sys,'/',['Sensor',13,'Noise/White Noise']])
set_param([sys,'/',['Sensor',13,'Noise/White Noise']],...
		'Seed','seed',...
		'position',[55,70,75,90])

add_block('built-in/Outport',[sys,'/',['Sensor',13,'Noise/out_1']])
set_param([sys,'/',['Sensor',13,'Noise/out_1']],...
		'Port','1',...
		'position',[235,70,255,90])
add_line([sys,'/',['Sensor',13,'Noise']],[80,80;135,80])
add_line([sys,'/',['Sensor',13,'Noise']],[210,80;230,80])
set_param([sys,'/',['Sensor',13,'Noise']],...
		'Mask Display','plot(1:20,rand(1,20))',...
		'Mask Type','Continuous White Noise.')
set_param([sys,'/',['Sensor',13,'Noise']],...
		'Mask Dialogue','White noise for continuous (s-domain) systems.\nBand-limited using zero-order-hold.|Noise Variance:|Sample Time:|Seed',...
		'Mask Translate','Cov = @1; Ts = @2; seed = @3;')
set_param([sys,'/',['Sensor',13,'Noise']],...
		'Mask Help','Implemented using white noise into Zero Order Hold block. For faster simulation set sample time to highest value possible but in accordance with the fastest dynamics of system.')
set_param([sys,'/',['Sensor',13,'Noise']],...
		'Mask Entries','0.0001\/0.1\/23341\/')


%     Finished composite block ['Sensor',13,'Noise'].

set_param([sys,'/',['Sensor',13,'Noise']],...
		'orientation',2,...
		'position',[530,230,560,260])

add_block('built-in/Sum',[sys,'/','Sum3'])
set_param([sys,'/','Sum3'],...
		'orientation',2,...
		'hide name',0,...
		'inputs','++',...
		'position',[455,224,475,251])

add_block('built-in/Sum',[sys,'/','Sum1'])
set_param([sys,'/','Sum1'],...
		'hide name',0,...
		'inputs','++',...
		'position',[250,94,270,121])


%     Subsystem  ['Input',13,'Noise',13,''].

new_system([sys,'/',['Input',13,'Noise',13,'']])
set_param([sys,'/',['Input',13,'Noise',13,'']],'Location',[0,0,262,236])

add_block('built-in/Outport',[sys,'/',['Input',13,'Noise',13,'/out_1']])
set_param([sys,'/',['Input',13,'Noise',13,'/out_1']],...
		'Port','1',...
		'position',[235,70,255,90])

add_block('built-in/White Noise',[sys,'/',['Input',13,'Noise',13,'/White Noise']])
set_param([sys,'/',['Input',13,'Noise',13,'/White Noise']],...
		'Seed','seed',...
		'position',[55,70,75,90])

add_block('built-in/Discrete Transfer Fcn',[sys,'/',['Input',13,'Noise',13,'/Zero Order Hold']])
set_param([sys,'/',['Input',13,'Noise',13,'/Zero Order Hold']],...
		'Numerator','[sqrt(Cov)]',...
		'Denominator','[sqrt(Ts)]',...
		'Sample time','Ts',...
		'position',[140,61,205,99])
add_line([sys,'/',['Input',13,'Noise',13,'']],[210,80;230,80])
add_line([sys,'/',['Input',13,'Noise',13,'']],[80,80;135,80])
set_param([sys,'/',['Input',13,'Noise',13,'']],...
		'Mask Display','plot(1:20,rand(1,20))',...
		'Mask Type','Continuous White Noise.')
set_param([sys,'/',['Input',13,'Noise',13,'']],...
		'Mask Dialogue','White noise for continuous (s-domain) systems.\nBand-limited using zero-order-hold.|Noise Variance:|Sample Time:|Seed',...
		'Mask Translate','Cov = @1; Ts = @2; seed = @3;')
set_param([sys,'/',['Input',13,'Noise',13,'']],...
		'Mask Help','Implemented using white noise into Zero Order Hold block. For faster simulation set sample time to highest value possible but in accordance with the fastest dynamics of system.')
set_param([sys,'/',['Input',13,'Noise',13,'']],...
		'Mask Entries','0.0001\/0.1\/1234\/')


%     Finished composite block ['Input',13,'Noise',13,''].

set_param([sys,'/',['Input',13,'Noise',13,'']],...
		'position',[185,23,210,57])

add_block('built-in/State-space',[sys,'/','Plant'])
set_param([sys,'/','Plant'],...
		'A','a',...
		'B','b',...
		'C','c',...
		'D','d',...
		'position',[295,87,375,133])

add_block('built-in/Inport',[sys,'/','Inport'])
set_param([sys,'/','Inport'],...
		'hide name',0,...
		'Port','1',...
		'position',[55,50,75,70])

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',[40,98,80,132])

add_block('built-in/Outport',[sys,'/','Outport'])
set_param([sys,'/','Outport'],...
		'hide name',0,...
		'Port','1',...
		'position',[530,50,550,70])

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',[145,202,210,258])

add_block('built-in/Sum',[sys,'/','Sum'])
set_param([sys,'/','Sum'],...
		'hide name',0,...
		'inputs','++-',...
		'position',[185,97,205,133])

add_block('built-in/Mux',[sys,'/',['Known inputs',13,'and sensors.']])
set_param([sys,'/',['Known inputs',13,'and sensors.']],...
		'orientation',2,...
		'inputs','[1,1]',...
		'position',[385,209,415,251])

add_block('built-in/Note',[sys,'/',['LQG demo with band-limited white noise.',13,'Try varying noise levels and estimator',13,'weightings. Try Luenberger observer.']])
set_param([sys,'/',['LQG demo with band-limited white noise.',13,'Try varying noise levels and estimator',13,'weightings. Try Luenberger observer.']],...
		'position',[380,20,381,21])

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\/0.0001*b*b''\/0.0001*eye(1,1)\/')
set_param([sys,'/','State estimator'],...
		'position',[260,205,350,255])
add_line(sys,[380,110;525,110])
add_line(sys,[500,110;500,230;480,230])
add_line(sys,[525,245;480,245])
add_line(sys,[450,240;420,240])
add_line(sys,[215,40;230,40;230,100;245,100])
add_line(sys,[275,110;290,110])
add_line(sys,[500,110;500,60;525,60])
add_line(sys,[140,230;125,230;125,125;180,125])
add_line(sys,[80,60;155,60;155,105;180,105])
add_line(sys,[85,115;180,115])
add_line(sys,[210,115;245,115])
add_line(sys,[230,115;230,185;440,185;440,220;420,220])
add_line(sys,[380,230;355,230])
add_line(sys,[255,230;215,230])
% 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
