function [ret,x0,str]=lqgdemo6(t,x,u,flag);
%LQGDEMO6	is the M-file description of the SIMULINK system named LQGDEMO6.
%	The block-diagram can be displayed by typing: LQGDEMO6.
%
%	SYS=LQGDEMO6(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 LQGDEMO6 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 LQGDEMO6 with a FLAG of zero:
%	[SIZES]=LQGDEMO6([],[],[],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,648,370])
     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/Discrete State-space',[sys,'/',['Discrete-time state',13,'estimator with',13,'continuous cost',13,'function.']])
set_param([sys,'/',['Discrete-time state',13,'estimator with',13,'continuous cost',13,'function.']],...
		'orientation',2,...
		'A','ae',...
		'B','be',...
		'C','ce',...
		'D','de',...
		'Sample time','Ts')
set_param([sys,'/',['Discrete-time state',13,'estimator with',13,'continuous cost',13,'function.']],...
		'Mask Display','Discrete\nKalman\nestimator',...
		'Mask Type','State estimator')
set_param([sys,'/',['Discrete-time state',13,'estimator with',13,'continuous cost',13,'function.']],...
		'Mask Dialogue','Discrete Kalman state estimator\n for continuous plant.\n(Requires Control Toolbox)|[A,B;C,D] Matrices:|Number of states:|State noise covar. E[ww'']:|Output noise covar. E[vv'']:|Sample time:')
set_param([sys,'/',['Discrete-time state',13,'estimator with',13,'continuous cost',13,'function.']],...
		'Mask Translate','Ts=@5; n=@2; [ms,ns]=size(@1); a=@1(1:n,1:n); b=@1(1:n,n+1:ns); c=@1(n+1:ms,1:n); d=@1(1+n:ms,1+n:ns); [l,p]=lqed(a,eye(n,n),c,@3,@4,Ts); [ny,nu]=size(d); [ad,bd]=c2d(a,b,Ts); [ae,be,cexy,dexy]=destim(ad,bd,c,d,l,1:ny,1:nu); ce=cexy(ny+1:ny+n,:); de=dexy(ny+1:ny+n,:);')
set_param([sys,'/',['Discrete-time state',13,'estimator with',13,'continuous cost',13,'function.']],...
		'Mask Help','Implements a discrete-time Kalman state estimator using lqed and estim commands. Mask and unmask this block to see how it works.')
set_param([sys,'/',['Discrete-time state',13,'estimator with',13,'continuous cost',13,'function.']],...
		'Mask Entries','[a,b;c,d]\/length(a)\/0.001*b*b''\/0.001*eye(1)\/0.1\/',...
		'position',[315,190,390,240])

add_block('built-in/Discrete State-space',[sys,'/',['Discrete-time',13,'linear regulator',13,'with continuous',13,'cost function.']])
set_param([sys,'/',['Discrete-time',13,'linear regulator',13,'with continuous',13,'cost function.']],...
		'orientation',2,...
		'A','[]',...
		'B','[]',...
		'C','[]',...
		'D','k',...
		'Sample time','Ts',...
		'Mask Display','K')
set_param([sys,'/',['Discrete-time',13,'linear regulator',13,'with continuous',13,'cost function.']],...
		'Mask Type','Discrete regulator')
set_param([sys,'/',['Discrete-time',13,'linear regulator',13,'with continuous',13,'cost function.']],...
		'Mask Dialogue','Discrete linear regulator\n for continuous plant.\n(Requires Control Toolbox)|[A,B;C,D] Matrices:|Number of states:|Output weighting, y''Qy:|Input weighting, u''Ru:|Sample time:')
set_param([sys,'/',['Discrete-time',13,'linear regulator',13,'with continuous',13,'cost function.']],...
		'Mask Translate','Ts=@5; n=@2; [ms,ns]=size(@1); a=@1(1:n,1:n); b=@1(1:n,n+1:ns); c=@1(n+1:ms,1:n); d=@1(1+n:ms,1+n:ns); [k]=lqrd(a,b,c''*@3*c,@4+d''*@3*d,c''*@3*d,Ts); ')
set_param([sys,'/',['Discrete-time',13,'linear regulator',13,'with continuous',13,'cost function.']],...
		'Mask Help','Implements a discrete-time linear quadratic regulator using lqrd command. Mask and unmask this block to see how it works.')
set_param([sys,'/',['Discrete-time',13,'linear regulator',13,'with continuous',13,'cost function.']],...
		'Mask Entries','[[a,zeros(length(a),1);-c,0], [b;0]; [c,0;zeros(1,length(a)),1], [d;0]]\/length(a)+1\/eye(2,2)\/eye(1,1)\/0.05\/')
set_param([sys,'/',['Discrete-time',13,'linear regulator',13,'with continuous',13,'cost function.']],...
		'position',[175,204,215,246])

add_block('built-in/Integrator',[sys,'/','Integrator2'])
set_param([sys,'/','Integrator2'],...
		'orientation',2,...
		'hide name',0,...
		'Initial','0',...
		'position',[385,292,405,328])

add_block('built-in/Note',[sys,'/','Integral action.'])
set_param([sys,'/','Integral action.'],...
		'position',[429,325,430,326])

add_block('built-in/Note',[sys,'/',['Servo control with LQG controller.',13,'Look at effect of varying sample time.']])
set_param([sys,'/',['Servo control with LQG controller.',13,'Look at effect of varying sample time.']],...
		'position',[300,7,301,8])

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

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',[410,194,440,236])

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

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

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

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',[15,93,55,127])

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

add_block('built-in/Sum',[sys,'/','Sum6'])
set_param([sys,'/','Sum6'],...
		'orientation',2,...
		'hide name',0,...
		'inputs','-+',...
		'position',[435,291,465,324])

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


%     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/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_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/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/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/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/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/Scope',[sys,'/',['Plant',13,'and actuator/Demanded Input',13,'']])
set_param([sys,'/',['Plant',13,'and actuator/Demanded Input',13,'']],...
		'Vgain','3',...
		'Hgain','10.1',...
		'Vmax','6',...
		'Hmax','20.2',...
		'Window',[0,0,280,220],...
		'position',[130,150,155,180])

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',...
		'Vmax','6',...
		'Hmax','20.2',...
		'Window',[0,0,280,220],...
		'position',[280,150,305,180])
add_line([sys,'/',['Plant',13,'and actuator']],[210,65;250,65])
add_line([sys,'/',['Plant',13,'and actuator']],[235,65;235,135;375,135])
add_line([sys,'/',['Plant',13,'and actuator']],[125,65;175,65])
add_line([sys,'/',['Plant',13,'and actuator']],[340,65;365,65])
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']],[220,65;220,165;275,165])


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

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

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

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

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

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

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',[560,87,580,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',[40,202,60,228])
add_line(sys,[310,215;275,215])
add_line(sys,[240,225;220,225])
add_line(sys,[405,215;395,215])
add_line(sys,[475,100;555,100])
add_line(sys,[505,100;505,50;560,50])
add_line(sys,[55,55;60,55;60,90;75,90])
add_line(sys,[430,310;410,310])
add_line(sys,[105,100;105,345;490,345;490,315;470,315])
add_line(sys,[105,100;200,100])
add_line(sys,[380,310;285,310;285,230;275,230])
add_line(sys,[235,100;325,100])
add_line(sys,[170,225;155,225;155,170;285,170;285,115;325,115])
add_line(sys,[60,110;75,110])
add_line(sys,[425,115;435,115])
add_line(sys,[360,110;360,120;395,120])
add_line(sys,[375,70;375,105;395,105])
add_line(sys,[475,125;495,125;495,205;445,205])
add_line(sys,[540,100;540,215;530,215])
add_line(sys,[500,225;445,225])
add_line(sys,[490,225;490,300;470,300])
add_line(sys,[555,230;530,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
