function [ret,x0,str]=lqgdemo4(t,x,u,flag);
%LQGDEMO4	is the M-file description of the SIMULINK system named LQGDEMO4.
%	The block-diagram can be displayed by typing: LQGDEMO4.
%
%	SYS=LQGDEMO4(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 LQGDEMO4 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 LQGDEMO4 with a FLAG of zero:
%	[SIZES]=LQGDEMO4([],[],[],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,618,372])
     open_system(sys)
end;
set_param(sys,'algorithm',		'RK-23')
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/Gain',[sys,'/','FF Gain'])
set_param([sys,'/','FF Gain'],...
		'Gain','1/0.6879',...
		'position',[220,92,245,118])

add_block('built-in/Sum',[sys,'/','Sum6'])
set_param([sys,'/','Sum6'],...
		'orientation',2,...
		'hide name',0,...
		'inputs','-+',...
		'position',[450,286,480,319])

add_block('built-in/Sum',[sys,'/','Sum4'])
set_param([sys,'/','Sum4'],...
		'hide name',0,...
		'inputs','++',...
		'position',[95,87,115,123])

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',[35,98,75,132])

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

add_block('built-in/Sum',[sys,'/','Sum1'])
set_param([sys,'/','Sum1'],...
		'hide name',0,...
		'inputs','+-',...
		'position',[345,95,370,130])

add_block('built-in/State-space',[sys,'/','Plant'])
set_param([sys,'/','Plant'],...
		'A','a',...
		'B','b',...
		'C','c',...
		'D','d',...
		'position',[400,92,480,138])

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.001*b*b''\/0.001*eye(1,1)\/')
set_param([sys,'/','State estimator'],...
		'position',[325,196,385,244])

add_block('built-in/Outport',[sys,'/','Outport'])
set_param([sys,'/','Outport'],...
		'hide name',0,...
		'Port','1',...
		'position',[580,45,600,65])

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,zeros(length(a),1);-c,0]\/[b;0] \/[c,0;zeros(1,length(a)),1] \/[d;0]\/eye(2,2)\/eye(1,1)\/',...
		'position',[185,209,225,251])

add_block('built-in/Mux',[sys,'/',['Known inputs',13,'and sensors.']])
set_param([sys,'/',['Known inputs',13,'and sensors.']],...
		'orientation',2,...
		'hide name',0,...
		'inputs','[1,1]',...
		'position',[425,199,455,241])

add_block('built-in/Mux',[sys,'/','Mux'])
set_param([sys,'/','Mux'],...
		'orientation',2,...
		'hide name',0,...
		'inputs','[4,1]',...
		'position',[260,212,285,243])

add_block('built-in/Note',[sys,'/',['Servo control with LQG controller.',13,'Try varying Integral gain and FeedForward',13,'gains.']])
set_param([sys,'/',['Servo control with LQG controller.',13,'Try varying Integral gain and FeedForward',13,'gains.']],...
		'position',[315,12,316,13])

add_block('built-in/Note',[sys,'/','Integral action.'])
set_param([sys,'/','Integral action.'],...
		'position',[444,265,445,266])

add_block('built-in/Integrator',[sys,'/','Integrator2'])
set_param([sys,'/','Integrator2'],...
		'orientation',2,...
		'hide name',0,...
		'Initial','0',...
		'position',[400,287,420,323])

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',[575,102,595,128])

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',[55,217,75,243])
add_line(sys,[80,115;90,115])
add_line(sys,[180,230;155,230;155,175;300,175;300,120;340,120])
add_line(sys,[250,105;340,105])
add_line(sys,[395,305;300,305;300,235;290,235])
add_line(sys,[120,105;215,105])
add_line(sys,[120,105;120,340;505,340;505,310;485,310])
add_line(sys,[445,305;425,305])
add_line(sys,[75,50;80,50;80,95;90,95])
add_line(sys,[485,115;570,115])
add_line(sys,[520,115;520,55;575,55])
add_line(sys,[420,220;390,220])
add_line(sys,[255,230;230,230])
add_line(sys,[320,220;290,220])
add_line(sys,[375,115;395,115])
add_line(sys,[375,115;375,170;475,170;475,210;460,210])
add_line(sys,[520,115;520,230;460,230])
add_line(sys,[520,230;520,295;485,295])
% 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
