function [ret,x0,str]=rlsest(t,x,u,flag);
%RLSEST	is the M-file description of the SIMULINK system named RLSEST.
%	The block-diagram can be displayed by typing: rlsest.
%
%	SYS=RLSEST(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 RLSEST to return state derivatives, FLAG=2
%	discrete states, FLAG=3 system outputs and FLAG=4 next sample
%	time. For more information and other options see SFUNC.
%
%	Calling RLSEST with a FLAG of zero:
%	[SIZES]=RLSEST([],[],[],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.

% 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',[14,63,707,464])
     open_system(sys)
end;
set_param(sys,'algorithm',		'RK-45')
set_param(sys,'Start time',	'0.0')
set_param(sys,'Stop time',		'99')
set_param(sys,'Min step size',	'1')
set_param(sys,'Max step size',	'10')
set_param(sys,'Relative error','1e-3')
set_param(sys,'Return vars',	'')

add_block('built-in/Note',[sys,'/','y[n]'])
set_param([sys,'/','y[n]'],...
		'position',[610,60,615,65])

add_block('built-in/Sum',[sys,'/','Sum'])
set_param([sys,'/','Sum'],...
		'hide name',0,...
		'inputs','-+',...
		'position',[160,60,180,95])

add_block('built-in/Note',[sys,'/','u[n]'])
set_param([sys,'/','u[n]'],...
		'position',[430,55,435,60])

add_block('built-in/Scope',[sys,'/','Setpoint//Plant'])
set_param([sys,'/','Setpoint//Plant'],...
		'Vgain','50.000000',...
		'Hgain','100.000000',...
		'Vmax','83.333333',...
		'Hmax','200.000000',...
		'Window',[452,446,715,619])
open_system([sys,'/','Setpoint//Plant'])
set_param([sys,'/','Setpoint//Plant'],...
		'position',[230,135,250,165])

add_block('built-in/Discrete Transfer Fcn',[sys,'/','Plant'])
set_param([sys,'/','Plant'],...
		'Numerator','[-3.4 1.5]',...
		'Denominator','[1 -1.6 0.8]',...
		'position',[480,62,580,98])

add_block('built-in/Scope',[sys,'/',['Plant',13,'Output']])
set_param([sys,'/',['Plant',13,'Output']],...
		'Vgain','10.000000',...
		'Hgain','100.000000',...
		'Vmax','20.408200',...
		'Hmax','200.000000',...
		'Window',[16,129,296,349],...
		'position',[655,67,675,93])

add_block('built-in/Discrete Transfer Fcn',[sys,'/','Pole Placement Controller'])
set_param([sys,'/','Pole Placement Controller'],...
		'Numerator','[-0.0750149790293569 0.0986938286399024 ]',...
		'Denominator','[1 -0.455050928699822 ]',...
		'Mask Display','',...
		'position',[240,63,370,97])

add_block('built-in/Signal Generator',[sys,'/','Setpoint'])
set_param([sys,'/','Setpoint'],...
		'Peak','5.000000',...
		'Peak Range','25.000000',...
		'Freq','0.100000',...
		'Freq Range','1.000000',...
		'Wave','Sqr',...
		'Units','Rads',...
		'position',[35,68,80,102])


%     Subsystem  'phi'.

new_system([sys,'/','phi'])
set_param([sys,'/','phi'],'Location',[203,129,617,252])

add_block('built-in/Mux',[sys,'/','phi/Mux1'])
set_param([sys,'/','phi/Mux1'],...
		'inputs','[1 1]',...
		'position',[65,21,95,139])

add_block('built-in/Inport',[sys,'/','phi/in_2'])
set_param([sys,'/','phi/in_2'],...
		'Port','2',...
		'position',[15,100,35,120])

add_block('built-in/Outport',[sys,'/','phi/out_1'])
set_param([sys,'/','phi/out_1'],...
		'position',[280,70,300,90])

add_block('built-in/Inport',[sys,'/','phi/in_1'])
set_param([sys,'/','phi/in_1'],...
		'position',[15,40,35,60])

add_block('built-in/Discrete State-space',[sys,'/','phi/Disc. State-space'])
set_param([sys,'/','phi/Disc. State-space'],...
		'A','[zeros(1,2*order);eye(order-1,2*order);zeros(1,2*order);zeros(order-1,order),eye(order-1,order)]',...
		'B','[1, zeros(1,2*order-1);zeros(1,order),-1,zeros(1,order-1)]''')
set_param([sys,'/','phi/Disc. State-space'],...
		'C','eye(2*order)',...
		'D','[zeros(2*order,order)]',...
		'Sample time','ts',...
		'position',[145,60,255,100])
add_line([sys,'/','phi'],[100,80;135,80])
add_line([sys,'/','phi'],[40,50;55,50])
add_line([sys,'/','phi'],[260,80;270,80])
add_line([sys,'/','phi'],[40,110;55,110])
set_param([sys,'/','phi'],...
		'Mask Display','State\nGenerator',...
		'Mask Type','State Generator',...
		'Mask Dialogue','State Generator:|Model Order:|Sample Time:',...
		'Mask Translate','order = @1;ts = @2;')
set_param([sys,'/','phi'],...
		'Mask Help','This block generates the state vector for use with the RLS parameter estimator.',...
		'Mask Entries','2\/1\/')


%     Finished composite block 'phi'.

set_param([sys,'/','phi'],...
		'orientation',1,...
		'move name',0,...
		'position',[479,150,586,190])


%     Subsystem  ['Recursive least squares',13,' Parameter Estimator'].

new_system([sys,'/',['Recursive least squares',13,' Parameter Estimator']])
set_param([sys,'/',['Recursive least squares',13,' Parameter Estimator']],'Location',[243,483,679,634])

add_block('built-in/S-function',[sys,'/',['Recursive least squares',13,' Parameter Estimator/rlsests']])
set_param([sys,'/',['Recursive least squares',13,' Parameter Estimator/rlsests']],...
		'function name','rlsests',...
		'parameters','nstates,lambda,ts',...
		'position',[175,80,225,100])

add_block('built-in/Outport',[sys,'/',['Recursive least squares',13,' Parameter Estimator/theta']])
set_param([sys,'/',['Recursive least squares',13,' Parameter Estimator/theta']],...
		'position',[265,80,285,100])

add_block('built-in/Inport',[sys,'/',['Recursive least squares',13,' Parameter Estimator/phi']])
set_param([sys,'/',['Recursive least squares',13,' Parameter Estimator/phi']],...
		'position',[15,40,35,60])

add_block('built-in/Inport',[sys,'/',['Recursive least squares',13,' Parameter Estimator/yk']])
set_param([sys,'/',['Recursive least squares',13,' Parameter Estimator/yk']],...
		'Port','2',...
		'position',[15,110,35,130])

add_block('built-in/Mux',[sys,'/',['Recursive least squares',13,' Parameter Estimator/Mux']])
set_param([sys,'/',['Recursive least squares',13,' Parameter Estimator/Mux']],...
		'inputs','[nstates 1]',...
		'position',[95,70,125,105])
add_line([sys,'/',['Recursive least squares',13,' Parameter Estimator']],[40,120;60,120;60,95;85,95])
add_line([sys,'/',['Recursive least squares',13,' Parameter Estimator']],[230,90;255,90])
add_line([sys,'/',['Recursive least squares',13,' Parameter Estimator']],[40,50;60,50;60,80;85,80])
add_line([sys,'/',['Recursive least squares',13,' Parameter Estimator']],[130,90;165,90])
set_param([sys,'/',['Recursive least squares',13,' Parameter Estimator']],...
		'Mask Display','RLS\nEstimator',...
		'Mask Type','RLS Estimator')
set_param([sys,'/',['Recursive least squares',13,' Parameter Estimator']],...
		'Mask Dialogue','Hook input 1 to a state vector, and input 2 to the system output.\n|Model Order:|Forgetting Factor:|Sample Time:')
set_param([sys,'/',['Recursive least squares',13,' Parameter Estimator']],...
		'Mask Translate','nstates = 2*@1;lambda=@2;ts=@3;')
set_param([sys,'/',['Recursive least squares',13,' Parameter Estimator']],...
		'Mask Help','Implements a Recursive Least Squares parameter estimator. Mask and unmask this block to see how it works.',...
		'Mask Entries','2\/.8\/1\/')


%     Finished composite block ['Recursive least squares',13,' Parameter Estimator'].

set_param([sys,'/',['Recursive least squares',13,' Parameter Estimator']],...
		'orientation',2,...
		'position',[405,223,490,272])

add_block('built-in/Mux',[sys,'/','Mux'])
set_param([sys,'/','Mux'],...
		'hide name',0,...
		'inputs','2',...
		'position',[155,128,185,172])

add_block('built-in/S-function',[sys,'/',['Pole Placement',13,'Controller Design']])
set_param([sys,'/',['Pole Placement',13,'Controller Design']],...
		'orientation',2,...
		'function name','rlspps',...
		'parameters','name,order,char_poly,ts',...
		'Mask Display','rlsppd',...
		'Mask Type','Pole Placement')
set_param([sys,'/',['Pole Placement',13,'Controller Design']],...
		'Mask Dialogue','Pole Placement|Block Name|Model Order|Characterstic Polynomial|Sample Time',...
		'Mask Translate','name=@1;order=@2;char_poly=@3;ts=@4;')
set_param([sys,'/',['Pole Placement',13,'Controller Design']],...
		'Mask Help','Implements a pole placement design scheme for a discrete time controller.  Change the characteristic polynomial to see how the closed loop behavior changes.')
set_param([sys,'/',['Pole Placement',13,'Controller Design']],...
		'Mask Entries','[''rlsest/Pole Placement Controller'']\/2\/poly([.6 .6 .6])\/10\/',...
		'position',[190,235,290,265])

add_block('built-in/Note',[sys,'/',['To run the simulation, select "Start"',13,'from the "Simulation" pull-down menu']])
set_param([sys,'/',['To run the simulation, select "Start"',13,'from the "Simulation" pull-down menu']],...
		'position',[155,360,160,365])

add_block('built-in/Note',[sys,'/',['Adaptive Control of Second Order ',13,'Discrete Time Transfer Function',13,'(Double click on the "?" for more info)']])
set_param([sys,'/',['Adaptive Control of Second Order ',13,'Discrete Time Transfer Function',13,'(Double click on the "?" for more info)']],...
		'position',[161,315,166,320])


%     Subsystem  'More Info'.

new_system([sys,'/','More Info'])
set_param([sys,'/','More Info'],'Location',[221,570,648,775])

add_block('built-in/Note',[sys,'/',['More Info/This demonstration shows how an adaptive controller can',13,'be designed and simulated.']])
set_param([sys,'/',['More Info/This demonstration shows how an adaptive controller can',13,'be designed and simulated.']],...
		'position',[205,20,210,25])

add_block('built-in/Note',[sys,'/',['More Info/This adaptive controller was implemented using the set_param ',13,'command in the M-file rlsppd. Unmask the other blocks ',13,'to see how they were implemented.']])
set_param([sys,'/',['More Info/This adaptive controller was implemented using the set_param ',13,'command in the M-file rlsppd. Unmask the other blocks ',13,'to see how they were implemented.']],...
		'position',[205,125,210,130])

add_block('built-in/Note',[sys,'/',['More Info/Modify the parameters of the Plant during a simulation and observe',13,'that the adaptive controller always maintains stability.']])
set_param([sys,'/',['More Info/Modify the parameters of the Plant during a simulation and observe',13,'that the adaptive controller always maintains stability.']],...
		'position',[219,65,224,70])
set_param([sys,'/','More Info'],...
		'Mask Display','?')


%     Finished composite block 'More Info'.

set_param([sys,'/','More Info'],...
		'Drop Shadow',4,...
		'position',[315,320,342,345])

add_block('built-in/Scope',[sys,'/',['Parameter',13,'Estimates']])
set_param([sys,'/',['Parameter',13,'Estimates']],...
		'orientation',2,...
		'Vgain','10.000000',...
		'Hgain','100.000000',...
		'Vmax','13.698630',...
		'Hmax','200.000000',...
		'Window',[164,443,435,617])
open_system([sys,'/',['Parameter',13,'Estimates']])
set_param([sys,'/',['Parameter',13,'Estimates']],...
		'position',[320,160,340,190])
add_line(sys,[535,195;535,235;500,235])
add_line(sys,[375,80;470,80])
add_line(sys,[430,80;430,125;505,125;505,140])
add_line(sys,[185,80;230,80])
add_line(sys,[400,250;300,250])
add_line(sys,[380,250;380,175;350,175])
add_line(sys,[585,80;645,80])
add_line(sys,[630,80;630,260;500,260])
add_line(sys,[630,120;560,120;560,140])
add_line(sys,[630,80;630,30;135,30;135,70;150,70])
add_line(sys,[135,70;135,140;145,140])
add_line(sys,[190,150;220,150])
add_line(sys,[85,85;150,85])
add_line(sys,[100,85;100,160;145,160])

% 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
