function [ret,x0,str]=lqgopt(t,x,u,flag);
%LQGOPT	is the M-file description of the SIMULINK system named LQGOPT.
%	The block-diagram can be displayed by typing: LQGOPT.
%
%	SYS=LQGOPT(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 LQGOPT 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 LQGOPT with a FLAG of zero:
%	[SIZES]=LQGOPT([],[],[],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,50,705,494])
     open_system(sys)
end;
set_param(sys,'algorithm',		'RK-23')
set_param(sys,'Start time',	'0.0')
set_param(sys,'Stop time',		'6')
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/Outport',[sys,'/','Outport1'])
set_param([sys,'/','Outport1'],...
		'hide name',0,...
		'position',[185,50,205,70])

add_block('built-in/State-space',[sys,'/','Matrix gain'])
set_param([sys,'/','Matrix gain'],...
		'orientation',2,...
		'A','[]',...
		'B','[]',...
		'C','[]',...
		'D','[-1.13 1.07 -0.00291 -0.433 0 ]',...
		'position',[190,205,250,245])

add_block('built-in/Note',[sys,'/','Optimization Toolbox is required.'])
set_param([sys,'/','Optimization Toolbox is required.'],...
		'position',[240,413,241,414])

add_block('built-in/Note',[sys,'/',['Double click on these icons to get to start optimization or get listings.',13,'']])
set_param([sys,'/',['Double click on these icons to get to start optimization or get listings.',13,'']],...
		'position',[360,395,361,396])


%     Subsystem  'CLF'.

new_system([sys,'/','CLF'])
set_param([sys,'/','CLF'],'Location',[-10,380,117,533])
set_param([sys,'/','CLF'],...
		'Mask Display','Cost Functions\nListing',...
		'Mask Type','',...
		'Mask Dialogue','eval(''type lqgcost'')',...
		'Mask Translate','',...
		'Mask Help','')


%     Finished composite block 'CLF'.

set_param([sys,'/','CLF'],...
		'hide name',0,...
		'position',[470,357,571,392])


%     Subsystem  'OSL'.

new_system([sys,'/','OSL'])
set_param([sys,'/','OSL'],'Location',[-10,380,117,533])
set_param([sys,'/','OSL'],...
		'Mask Display','Optimization Script\nListing',...
		'Mask Type','',...
		'Mask Dialogue','eval(''type lqgoptm'')',...
		'Mask Translate','',...
		'Mask Help','')


%     Finished composite block 'OSL'.

set_param([sys,'/','OSL'],...
		'hide name',0,...
		'position',[288,357,421,392])


%     Subsystem  ['Double click here',13,'to start optimizing',13,'Matrix Gain, FF Gain, and I Gain. '].

new_system([sys,'/',['Double click here',13,'to start optimizing',13,'Matrix Gain, FF Gain, and I Gain. ']])
set_param([sys,'/',['Double click here',13,'to start optimizing',13,'Matrix Gain, FF Gain, and I Gain. ']],'Location',[-10,380,117,533])
set_param([sys,'/',['Double click here',13,'to start optimizing',13,'Matrix Gain, FF Gain, and I Gain. ']],...
		'Mask Display','Start\nOptimization',...
		'Mask Type','')
set_param([sys,'/',['Double click here',13,'to start optimizing',13,'Matrix Gain, FF Gain, and I Gain. ']],...
		'Mask Dialogue','eval(''if ~exist(''''leastsq''''),fprintf(''''\nMust be licensed for Optimization Toolbox\n''''), else, fprintf(''''\nCtrl-C to abort Optimization\n\n''''); lqgoptm, end'')')
set_param([sys,'/',['Double click here',13,'to start optimizing',13,'Matrix Gain, FF Gain, and I Gain. ']],...
		'Mask Translate','',...
		'Mask Help','')


%     Finished composite block ['Double click here',13,'to start optimizing',13,'Matrix Gain, FF Gain, and I Gain. '].

set_param([sys,'/',['Double click here',13,'to start optimizing',13,'Matrix Gain, FF Gain, and I Gain. ']],...
		'hide name',0,...
		'position',[140,359,226,392])

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

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

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

add_block('built-in/Note',[sys,'/','Noise input'])
set_param([sys,'/','Noise input'],...
		'position',[400,45,401,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.000000',...
		'Hgain','10.100000',...
		'Vmax','6.000000',...
		'Hmax','20.200000',...
		'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.000000',...
		'Hgain','10.100000',...
		'Vmax','6.000000',...
		'Hmax','20.200000',...
		'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']],...
		'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']],...
		'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']],...
		'Lower Limit','-5',...
		'Upper 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;245,65])
add_line([sys,'/',['Plant',13,'and actuator']],[220,65;220,165;270,165])
add_line([sys,'/',['Plant',13,'and actuator']],[50,65;85,65])
add_line([sys,'/',['Plant',13,'and actuator']],[65,65;65,165;120,165])
add_line([sys,'/',['Plant',13,'and actuator']],[340,65;360,65])
add_line([sys,'/',['Plant',13,'and actuator']],[125,65;170,65])
add_line([sys,'/',['Plant',13,'and actuator']],[235,65;235,135;370,135])


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

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

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

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

add_block('built-in/Sum',[sys,'/','Sum4'])
set_param([sys,'/','Sum4'],...
		'hide name',0,...
		'position',[100,82,120,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',[30,93,70,127])

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

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

add_block('built-in/Outport',[sys,'/','Outport'])
set_param([sys,'/','Outport'],...
		'hide name',0,...
		'Port','2',...
		'position',[585,40,605,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',[430,194,460,236])

add_block('built-in/Mux',[sys,'/','Mux'])
set_param([sys,'/','Mux'],...
		'orientation',2,...
		'hide name',0,...
		'inputs','[4,1]',...
		'position',[265,207,290,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',[320,7,321,8])

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

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

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',[340,191,400,239])

add_block('built-in/Scope',[sys,'/','Scope'])
set_param([sys,'/','Scope'],...
		'Vgain','1.500000',...
		'Hgain','6.100000',...
		'Vmax','3.000000',...
		'Hmax','12.200000',...
		'Window',[553,126,833,346])
open_system([sys,'/','Scope'])
set_param([sys,'/','Scope'],...
		'position',[575,85,600,115])

add_block('built-in/Scope',[sys,'/',['Floating',13,'Scope']])
set_param([sys,'/',['Floating',13,'Scope']],...
		'Vgain','1.000000',...
		'Hgain','10.100000',...
		'Vmax','2.000000',...
		'Hmax','20.200000',...
		'Window',[0,0,280,220],...
		'position',[70,175,95,205])
add_line(sys,[125,100;215,100])
add_line(sys,[145,100;145,60;175,60])
add_line(sys,[145,100;145,340;520,340;520,305;495,305])
add_line(sys,[575,230;555,230])
add_line(sys,[520,225;470,225])
add_line(sys,[510,225;510,290;495,290])
add_line(sys,[495,100;565,100])
add_line(sys,[560,100;560,215;555,215])
add_line(sys,[495,125;515,125;515,205;470,205])
add_line(sys,[395,70;395,105;410,105])
add_line(sys,[380,110;380,120;410,120])
add_line(sys,[445,115;450,115])
add_line(sys,[75,110;90,110])
add_line(sys,[185,225;175,225;175,170;305,170;305,115;340,115])
add_line(sys,[255,100;340,100])
add_line(sys,[400,300;305,300;305,230;300,230])
add_line(sys,[450,300;435,300])
add_line(sys,[75,55;80,55;80,90;90,90])
add_line(sys,[525,100;525,50;575,50])
add_line(sys,[425,215;410,215])
add_line(sys,[260,225;260,225])
add_line(sys,[335,215;300,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,str]=',sys,'(t,x,u,flag);'])
		else
			eval(['ret =', sys,'(t,x,u,flag);'])
		end
	else
		[ret,x0,str] = feval(sys);
	end
end
