function [ret,x0,str]=lqgdemo5(t,x,u,flag);
%LQGDEMO5	is the M-file description of the SIMULINK system named LQGDEMO5.
%	The block-diagram can be displayed by typing: LQGDEMO5.
%
%	SYS=LQGDEMO5(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 LQGDEMO5 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 LQGDEMO5 with a FLAG of zero:
%	[SIZES]=LQGDEMO5([],[],[],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',[6,44,648,413])
     open_system(sys)
end;
set_param(sys,'algorithm',		'RK-45')
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',[25,177,45,203])

add_block('built-in/Scope',[sys,'/','Output'])
set_param([sys,'/','Output'],...
		'Vgain','2',...
		'Hgain','10.1',...
		'Window',[57,351,337,571])
open_system([sys,'/','Output'])
set_param([sys,'/','Output'],...
		'position',[550,87,570,113])

add_block('built-in/Sum',[sys,'/','Sum3'])
set_param([sys,'/','Sum3'],...
		'orientation',2,...
		'hide name',0,...
		'inputs','++',...
		'position',[495,209,515,236])

add_block('built-in/Note',[sys,'/',['Sensor noise',13,'input']])
set_param([sys,'/',['Sensor noise',13,'input']],...
		'position',[585,218,586,219])

add_block('built-in/Sum',[sys,'/','Sum2'])
set_param([sys,'/','Sum2'],...
		'hide name',0,...
		'inputs','++',...
		'position',[390,99,410,126])

add_block('built-in/Note',[sys,'/','Noise input'])
set_param([sys,'/','Noise input'],...
		'position',[370,45,371,46])


%     Subsystem  ['Plant',13,'and actuator'].

new_system([sys,'/',['Plant',13,'and actuator']])
set_param([sys,'/',['Plant',13,'and actuator']],'Location',[0,0,511,212])

add_block('built-in/Scope',[sys,'/',['Plant',13,'and actuator/Actual Input',13,'']])
set_param([sys,'/',['Plant',13,'and actuator/Actual Input',13,'']],...
		'Vgain','3',...
		'Hgain','10.1',...
		'Window',[0,0,280,220],...
		'position',[280,150,305,180])

add_block('built-in/Scope',[sys,'/',['Plant',13,'and actuator/Demanded Input',13,'']])
set_param([sys,'/',['Plant',13,'and actuator/Demanded Input',13,'']],...
		'Vgain','3',...
		'Hgain','10.1',...
		'Window',[0,0,280,220],...
		'position',[130,150,155,180])

add_block('built-in/State-space',[sys,'/',['Plant',13,'and actuator/Plant']])
set_param([sys,'/',['Plant',13,'and actuator/Plant']],...
		'A','a',...
		'B','b',...
		'C','c',...
		'D','d',...
		'position',[255,42,335,88])

add_block('built-in/Inport',[sys,'/',['Plant',13,'and actuator/in_1']])
set_param([sys,'/',['Plant',13,'and actuator/in_1']],...
		'Port','1',...
		'position',[25,55,45,75])

add_block('built-in/Outport',[sys,'/',['Plant',13,'and actuator/out_1']])
set_param([sys,'/',['Plant',13,'and actuator/out_1']],...
		'Port','1',...
		'position',[370,55,390,75])

add_block('built-in/Outport',[sys,'/',['Plant',13,'and actuator/out_2']])
set_param([sys,'/',['Plant',13,'and actuator/out_2']],...
		'Port','2',...
		'position',[380,125,400,145])

add_block('built-in/Saturation',[sys,'/',['Plant',13,'and actuator/Saturation']])
set_param([sys,'/',['Plant',13,'and actuator/Saturation']],...
		'Upper Limit','5',...
		'Lower Limit','-5',...
		'position',[95,55,120,75])

add_block('built-in/Rate Limiter',[sys,'/',['Plant',13,'and actuator/Rate Limiter']])
set_param([sys,'/',['Plant',13,'and actuator/Rate Limiter']],...
		'Rising Slew Limit','10',...
		'Falling Slew Limit','-10',...
		'position',[180,55,205,75])
add_line([sys,'/',['Plant',13,'and actuator']],[210,65;250,65])
add_line([sys,'/',['Plant',13,'and actuator']],[220,65;220,165;275,165])
add_line([sys,'/',['Plant',13,'and actuator']],[50,65;90,65])
add_line([sys,'/',['Plant',13,'and actuator']],[65,65;65,165;125,165])
add_line([sys,'/',['Plant',13,'and actuator']],[340,65;365,65])
add_line([sys,'/',['Plant',13,'and actuator']],[125,65;175,65])
add_line([sys,'/',['Plant',13,'and actuator']],[235,65;235,135;375,135])


%     Finished composite block ['Plant',13,'and actuator'].

set_param([sys,'/',['Plant',13,'and actuator']],...
		'position',[430,87,460,138])

add_block('built-in/Gain',[sys,'/','FF Gain'])
set_param([sys,'/','FF Gain'],...
		'Gain','1/0.6879',...
		'position',[195,87,220,113])

add_block('built-in/Sum',[sys,'/','Sum6'])
set_param([sys,'/','Sum6'],...
		'orientation',2,...
		'hide name',0,...
		'inputs','-+',...
		'position',[425,281,455,314])

add_block('built-in/Sum',[sys,'/','Sum4'])
set_param([sys,'/','Sum4'],...
		'hide name',0,...
		'inputs','++',...
		'position',[70,82,90,118])

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',[5,93,45,127])

add_block('built-in/Inport',[sys,'/','Inport'])
set_param([sys,'/','Inport'],...
		'hide name',0,...
		'Port','1',...
		'position',[20,45,40,65])

add_block('built-in/Sum',[sys,'/','Sum1'])
set_param([sys,'/','Sum1'],...
		'hide name',0,...
		'inputs','+-',...
		'position',[320,90,345,125])

add_block('built-in/Outport',[sys,'/','Outport'])
set_param([sys,'/','Outport'],...
		'hide name',0,...
		'Port','1',...
		'position',[555,40,575,60])

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',[400,194,430,236])

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

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',[290,7,291,8])

add_block('built-in/Note',[sys,'/','Integral action.'])
set_param([sys,'/','Integral action.'],...
		'position',[419,320,420,321])

add_block('built-in/Integrator',[sys,'/','Integrator2'])
set_param([sys,'/','Integrator2'],...
		'orientation',2,...
		'hide name',0,...
		'Initial','0',...
		'position',[375,282,395,318])

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',[170,204,210,246])

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',[310,191,370,239])
add_line(sys,[545,230;520,230])
add_line(sys,[490,225;435,225])
add_line(sys,[480,225;480,290;460,290])
add_line(sys,[465,100;545,100])
add_line(sys,[530,100;530,215;520,215])
add_line(sys,[465,125;485,125;485,205;435,205])
add_line(sys,[365,70;365,105;385,105])
add_line(sys,[350,110;350,120;385,120])
add_line(sys,[415,115;425,115])
add_line(sys,[50,110;65,110])
add_line(sys,[165,225;145,225;145,170;275,170;275,115;315,115])
add_line(sys,[225,100;315,100])
add_line(sys,[370,300;275,300;275,230;265,230])
add_line(sys,[95,100;190,100])
add_line(sys,[95,100;95,335;480,335;480,305;460,305])
add_line(sys,[420,300;400,300])
add_line(sys,[45,55;50,55;50,90;65,90])
add_line(sys,[495,100;495,50;550,50])
add_line(sys,[395,215;375,215])
add_line(sys,[230,225;215,225])
add_line(sys,[305,215;265,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
