function [ret,x0,str,ts,xts]=example6(t,x,u,flag);
%EXAMPLE6	is the M-file description of the SIMULINK system named EXAMPLE6.
%	The block-diagram can be displayed by typing: EXAMPLE6.
%
%	SYS=EXAMPLE6(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 EXAMPLE6 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 EXAMPLE6 with a FLAG of zero:
%	[SIZES]=EXAMPLE6([],[],[],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
%		SIZES(5) number of roots (currently unsupported)
%		SIZES(6) direct feedthrough flag
%		SIZES(7) number of sample times
%
%	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',[4,58,816,631])
     open_system(sys)
end;
set_param(sys,'algorithm',     'RK-45')
set_param(sys,'Start time',    '0.0')
set_param(sys,'Stop time',     '50')
set_param(sys,'Min step size', '0.001')
set_param(sys,'Max step size', '10')
set_param(sys,'Relative error','1e-3')
set_param(sys,'Return vars',   '')
set_param(sys,'AssignWideVectorLines','on');

add_block('built-in/Scope',[sys,'/','Tracking error'])
set_param([sys,'/','Tracking error'],...
		'Vgain','15.000000',...
		'Hgain','50.000000',...
		'Vmax','30.000000',...
		'Hmax','100.000000',...
		'Window',[808,616,1130,996],...
		'position',[725,83,760,117])

add_block('built-in/Sum',[sys,'/','Sum1'])
set_param([sys,'/','Sum1'],...
		'inputs','-+',...
		'position',[655,82,685,118])

add_block('built-in/Transfer Fcn',[sys,'/','Reference Model'])
set_param([sys,'/','Reference Model'],...
		'position',[330,43,410,87])


%     Subsystem  'Estimator'.

new_system([sys,'/','Estimator'])
set_param([sys,'/','Estimator'],'Location',[4,58,752,551])

add_block('built-in/Integrator',[sys,'/','Estimator/Integrator1'])
set_param([sys,'/','Estimator/Integrator1'],...
		'Initial','18',...
		'position',[405,117,435,163])

add_block('built-in/Demux',[sys,'/','Estimator/Demux'])
set_param([sys,'/','Estimator/Demux'],...
		'position',[260,144,300,186])

add_block('built-in/Product',[sys,'/','Estimator/Product'])
set_param([sys,'/','Estimator/Product'],...
		'position',[210,147,240,183])

add_block('built-in/Inport',[sys,'/','Estimator/e1'])
set_param([sys,'/','Estimator/e1'],...
		'position',[30,47,55,73])

add_block('built-in/Inport',[sys,'/','Estimator/omega'])
set_param([sys,'/','Estimator/omega'],...
		'Port','2',...
		'position',[25,162,50,188])

add_block('built-in/Gain',[sys,'/','Estimator/Gain1'])
set_param([sys,'/','Estimator/Gain1'],...
		'Gain','-1',...
		'position',[345,125,370,155])

add_block('built-in/Mux',[sys,'/','Estimator/Mux'])
set_param([sys,'/','Estimator/Mux'],...
		'position',[570,77,600,123])

add_block('built-in/Integrator',[sys,'/','Estimator/Integrator'])
set_param([sys,'/','Estimator/Integrator'],...
		'Initial','3',...
		'position',[405,16,435,64])

add_block('built-in/Gain',[sys,'/','Estimator/Gain'])
set_param([sys,'/','Estimator/Gain'],...
		'Gain','-1',...
		'position',[350,27,375,53])

add_block('built-in/Demux',[sys,'/','Estimator/Demux1'])
set_param([sys,'/','Estimator/Demux1'],...
		'position',[390,369,430,411])

add_block('built-in/Integrator',[sys,'/','Estimator/Integrator3'])
set_param([sys,'/','Estimator/Integrator3'],...
		'Initial','3',...
		'position',[405,300,435,350])

add_block('built-in/Gain',[sys,'/','Estimator/Gain3'])
set_param([sys,'/','Estimator/Gain3'],...
		'Gain','-1',...
		'position',[340,310,370,340])

add_block('built-in/Mux',[sys,'/','Estimator/Mux1'])
set_param([sys,'/','Estimator/Mux1'],...
		'inputs','8',...
		'position',[565,217,595,303])

add_block('built-in/Integrator',[sys,'/','Estimator/Integrator2'])
set_param([sys,'/','Estimator/Integrator2'],...
		'Initial','-8',...
		'position',[405,195,435,245])

add_block('built-in/Gain',[sys,'/','Estimator/Gain2'])
set_param([sys,'/','Estimator/Gain2'],...
		'Gain','-1',...
		'position',[345,207,370,233])

add_block('built-in/Fcn',[sys,'/','Estimator/theta_dot*phi'])
set_param([sys,'/','Estimator/theta_dot*phi'],...
		'Expr','u(1)*u(2)+u(3)*u(4)+u(5)*u(6)+u(7)*u(8)',...
		'position',[630,250,670,270])

add_block('built-in/Outport',[sys,'/','Estimator/u_a'])
set_param([sys,'/','Estimator/u_a'],...
		'Port','2',...
		'position',[720,250,740,270])


%     Subsystem  'Estimator/phi'.

new_system([sys,'/','Estimator/phi'])
set_param([sys,'/','Estimator/phi'],'Location',[40,60,508,384])

add_block('built-in/Transfer Fcn',[sys,'/','Estimator/phi/F(s)'])
set_param([sys,'/','Estimator/phi/F(s)'],...
		'Denominator','[1 1]',...
		'position',[230,22,285,58])

add_block('built-in/Transfer Fcn',[sys,'/','Estimator/phi/F(s)1'])
set_param([sys,'/','Estimator/phi/F(s)1'],...
		'Denominator','[1 1]',...
		'position',[230,77,285,113])

add_block('built-in/Transfer Fcn',[sys,'/','Estimator/phi/F(s)2'])
set_param([sys,'/','Estimator/phi/F(s)2'],...
		'Denominator','[1 1]',...
		'position',[230,142,290,178])

add_block('built-in/Transfer Fcn',[sys,'/','Estimator/phi/F(s)3'])
set_param([sys,'/','Estimator/phi/F(s)3'],...
		'Denominator','[1 1]',...
		'position',[230,212,290,248])

add_block('built-in/Demux',[sys,'/','Estimator/phi/Demux'])
set_param([sys,'/','Estimator/phi/Demux'],...
		'position',[95,98,155,142])

add_block('built-in/Mux',[sys,'/','Estimator/phi/Mux'])
set_param([sys,'/','Estimator/phi/Mux'],...
		'position',[370,100,405,150])

add_block('built-in/Outport',[sys,'/','Estimator/phi/phi'])
set_param([sys,'/','Estimator/phi/phi'],...
		'position',[435,112,460,138])

add_block('built-in/Inport',[sys,'/','Estimator/phi/omega'])
set_param([sys,'/','Estimator/phi/omega'],...
		'position',[25,107,50,133])
add_line([sys,'/','Estimator/phi'],[55,120;90,120])
add_line([sys,'/','Estimator/phi'],[160,115;215,115;225,95])
add_line([sys,'/','Estimator/phi'],[160,105;205,105;205,40;225,40])
add_line([sys,'/','Estimator/phi'],[160,125;215,125;225,160])
add_line([sys,'/','Estimator/phi'],[160,135;205,135;205,230;225,230])
add_line([sys,'/','Estimator/phi'],[295,160;340,160;340,130;365,130])
add_line([sys,'/','Estimator/phi'],[295,230;350,230;350,140;365,140])
add_line([sys,'/','Estimator/phi'],[290,95;340,95;340,120;365,120])
add_line([sys,'/','Estimator/phi'],[290,40;350,40;350,110;365,110])
add_line([sys,'/','Estimator/phi'],[410,125;430,125])
set_param([sys,'/','Estimator/phi'],...
		'Mask Display','Regression \nvector')


%     Finished composite block 'Estimator/phi'.

set_param([sys,'/','Estimator/phi'],...
		'Drop Shadow',4,...
		'position',[70,146,170,204])

add_block('built-in/Outport',[sys,'/','Estimator/theta'])
set_param([sys,'/','Estimator/theta'],...
		'position',[695,90,715,110])
add_line([sys,'/','Estimator'],[380,40;400,40])
add_line([sys,'/','Estimator'],[375,140;400,140])
add_line([sys,'/','Estimator'],[375,220;400,220])
add_line([sys,'/','Estimator'],[375,325;400,325])
add_line([sys,'/','Estimator'],[55,175;65,175])
add_line([sys,'/','Estimator'],[605,100;690,100])
add_line([sys,'/','Estimator'],[440,140;515,140;515,95;565,95])
add_line([sys,'/','Estimator'],[440,40;460,40;460,85;565,85])
add_line([sys,'/','Estimator'],[380,40;380,110;445,110;445,235;560,235])
add_line([sys,'/','Estimator'],[375,220;375,275;560,275])
add_line([sys,'/','Estimator'],[600,260;625,260])
add_line([sys,'/','Estimator'],[675,260;715,260])
add_line([sys,'/','Estimator'],[175,175;205,175])
add_line([sys,'/','Estimator'],[60,60;185,60;185,155;205,155])
add_line([sys,'/','Estimator'],[245,165;255,165])
add_line([sys,'/','Estimator'],[305,170;330,170;340,220])
add_line([sys,'/','Estimator'],[305,160;330,160;340,140])
add_line([sys,'/','Estimator'],[305,150;320,150;320,40;345,40])
add_line([sys,'/','Estimator'],[305,180;320,180;320,325;335,325])
add_line([sys,'/','Estimator'],[435,375;470,375;470,225;560,225])
add_line([sys,'/','Estimator'],[435,385;480,385;480,245;560,245])
add_line([sys,'/','Estimator'],[435,395;490,395;490,265;560,265])
add_line([sys,'/','Estimator'],[435,405;500,405;500,285;560,285])
add_line([sys,'/','Estimator'],[175,175;175,390;385,390])
add_line([sys,'/','Estimator'],[375,140;375,185;490,185;490,255;560,255])
add_line([sys,'/','Estimator'],[440,220;525,220;525,105;565,105])
add_line([sys,'/','Estimator'],[375,325;375,295;560,295])
add_line([sys,'/','Estimator'],[440,325;535,325;535,115;565,115])
set_param([sys,'/','Estimator'],...
		'Mask Display','Estimator\n(generates theta\nand u_a)')


%     Finished composite block 'Estimator'.

set_param([sys,'/','Estimator'],...
		'orientation',2,...
		'Drop Shadow',4,...
		'position',[380,264,505,326])

add_block('built-in/Transfer Fcn',[sys,'/','Plant Model'])
set_param([sys,'/','Plant Model'],...
		'Denominator','[1  3 -10]',...
		'position',[510,161,595,209])


%     Subsystem  'Controller'.

new_system([sys,'/','Controller'])
set_param([sys,'/','Controller'],'Location',[113,108,624,447])

add_block('built-in/Inport',[sys,'/','Controller/omega'])
set_param([sys,'/','Controller/omega'],...
		'position',[15,32,50,68])

add_block('built-in/Inport',[sys,'/','Controller/theta'])
set_param([sys,'/','Controller/theta'],...
		'Port','2',...
		'position',[15,117,50,153])

add_block('built-in/Demux',[sys,'/','Controller/Demux'])
set_param([sys,'/','Controller/Demux'],...
		'position',[95,26,145,74])

add_block('built-in/Demux',[sys,'/','Controller/Demux1'])
set_param([sys,'/','Controller/Demux1'],...
		'position',[100,111,150,159])

add_block('built-in/Mux',[sys,'/','Controller/Mux2'])
set_param([sys,'/','Controller/Mux2'],...
		'inputs','9',...
		'position',[275,49,320,151])

add_block('built-in/Outport',[sys,'/','Controller/u'])
set_param([sys,'/','Controller/u'],...
		'position',[450,82,485,118])

add_block('built-in/Fcn',[sys,'/','Controller/omega*theta+u_a'])
set_param([sys,'/','Controller/omega*theta+u_a'],...
		'Expr','u(1)*u(5)+u(2)*u(6)+u(3)*u(7)+u(4)*u(8)+u(9)',...
		'position',[360,84,420,116])

add_block('built-in/Inport',[sys,'/','Controller/u_a'])
set_param([sys,'/','Controller/u_a'],...
		'Port','3',...
		'position',[15,202,50,238])
add_line([sys,'/','Controller'],[55,135;95,135])
add_line([sys,'/','Controller'],[55,50;90,50])
add_line([sys,'/','Controller'],[150,45;230,45;230,70;270,70])
add_line([sys,'/','Controller'],[150,55;220,55;220,80;270,80])
add_line([sys,'/','Controller'],[150,65;210,65;210,90;270,90])
add_line([sys,'/','Controller'],[155,120;210,120;210,100;270,100])
add_line([sys,'/','Controller'],[155,130;220,130;220,110;270,110])
add_line([sys,'/','Controller'],[155,140;230,140;230,120;270,120])
add_line([sys,'/','Controller'],[155,150;240,150;240,130;270,130])
add_line([sys,'/','Controller'],[150,35;240,35;240,60;270,60])
add_line([sys,'/','Controller'],[55,220;250,220;250,140;270,140])
add_line([sys,'/','Controller'],[325,100;355,100])
add_line([sys,'/','Controller'],[425,100;445,100])
set_param([sys,'/','Controller'],...
		'Mask Display','Controller\n(generates u)')


%     Finished composite block 'Controller'.

set_param([sys,'/','Controller'],...
		'Drop Shadow',4,...
		'position',[375,155,475,215])


%     Subsystem  'Filter'.

new_system([sys,'/','Filter'])
set_param([sys,'/','Filter'],'Location',[88,58,552,391])

add_block('built-in/Mux',[sys,'/','Filter/Mux'])
set_param([sys,'/','Filter/Mux'],...
		'position',[320,62,350,108])

add_block('built-in/Inport',[sys,'/','Filter/u'])
set_param([sys,'/','Filter/u'],...
		'position',[20,14,50,46])

add_block('built-in/Inport',[sys,'/','Filter/y'])
set_param([sys,'/','Filter/y'],...
		'Port','2',...
		'position',[20,89,50,121])

add_block('built-in/Inport',[sys,'/','Filter/r'])
set_param([sys,'/','Filter/r'],...
		'Port','3',...
		'position',[20,174,50,206])

add_block('built-in/Transfer Fcn',[sys,'/','Filter/F(s)_'])
set_param([sys,'/','Filter/F(s)_'],...
		'Denominator','[1 2]',...
		'position',[100,82,165,128])

add_block('built-in/Transfer Fcn',[sys,'/','Filter/F(s)'])
set_param([sys,'/','Filter/F(s)'],...
		'Denominator','[1 2]',...
		'position',[100,7,165,53])

add_block('built-in/Outport',[sys,'/','Filter/omega'])
set_param([sys,'/','Filter/omega'],...
		'position',[385,69,415,101])
add_line([sys,'/','Filter'],[355,85;380,85])
add_line([sys,'/','Filter'],[55,30;95,30])
add_line([sys,'/','Filter'],[170,30;290,30;290,70;315,70])
add_line([sys,'/','Filter'],[55,105;95,105])
add_line([sys,'/','Filter'],[170,105;280,105;280,80;315,80])
add_line([sys,'/','Filter'],[75,105;75,150;290,150;290,90;315,90])
add_line([sys,'/','Filter'],[55,190;300,190;300,100;315,100])
set_param([sys,'/','Filter'],...
		'Mask Display','Filter\n(generates omega)')


%     Finished composite block 'Filter'.

set_param([sys,'/','Filter'],...
		'Drop Shadow',4,...
		'position',[190,157,330,233])


%     Subsystem  'Input r'.

new_system([sys,'/','Input r'])
set_param([sys,'/','Input r'],'Location',[139,93,550,411])

add_block('built-in/Sine Wave',[sys,'/','Input r/Sine Wave1'])
set_param([sys,'/','Input r/Sine Wave1'],...
		'amplitude','0.7',...
		'phase','1.5',...
		'position',[35,20,65,60])

add_block('built-in/Sine Wave',[sys,'/','Input r/Sine Wave'])
set_param([sys,'/','Input r/Sine Wave'],...
		'frequency','4.9',...
		'position',[35,82,65,118])

add_block('built-in/Step Fcn',[sys,'/','Input r/Step Fcn'])
set_param([sys,'/','Input r/Step Fcn'],...
		'Time','0',...
		'position',[35,153,65,187])

add_block('built-in/Gain',[sys,'/','Input r/Gain'])
set_param([sys,'/','Input r/Gain'],...
		'Gain','0.5',...
		'position',[115,22,145,58])

add_block('built-in/Gain',[sys,'/','Input r/Gain1'])
set_param([sys,'/','Input r/Gain1'],...
		'Gain','3',...
		'position',[115,82,145,118])

add_block('built-in/Gain',[sys,'/','Input r/Gain2'])
set_param([sys,'/','Input r/Gain2'],...
		'Gain','0',...
		'position',[110,152,140,188])

add_block('built-in/Sum',[sys,'/','Input r/Sum'])
set_param([sys,'/','Input r/Sum'],...
		'inputs','+++',...
		'position',[245,78,275,122])

add_block('built-in/Outport',[sys,'/','Input r/r'])
set_param([sys,'/','Input r/r'],...
		'position',[360,84,390,116])
add_line([sys,'/','Input r'],[70,100;110,100])
add_line([sys,'/','Input r'],[150,100;240,100])
add_line([sys,'/','Input r'],[70,40;110,40])
add_line([sys,'/','Input r'],[70,170;105,170])
add_line([sys,'/','Input r'],[145,170;170,170;170,115;240,115])
add_line([sys,'/','Input r'],[150,40;170,40;170,85;240,85])
add_line([sys,'/','Input r'],[280,100;355,100])
set_param([sys,'/','Input r'],...
		'Mask Display','Excitation signal')


%     Finished composite block 'Input r'.

set_param([sys,'/','Input r'],...
		'Drop Shadow',4,...
		'position',[10,157,140,223])

add_block('built-in/To Workspace',[sys,'/','To Workspace'])
set_param([sys,'/','To Workspace'],...
		'mat-name','yout',...
		'position',[720,388,770,412])

add_block('built-in/Mux',[sys,'/','Mux1'])
set_param([sys,'/','Mux1'],...
		'inputs','7',...
		'position',[660,367,690,433])

add_block('built-in/Clock',[sys,'/','Clock'])
set_param([sys,'/','Clock'],...
		'orientation',1,...
		'position',[635,340,655,360])

add_block('built-in/Demux',[sys,'/','Demux'])
set_param([sys,'/','Demux'],...
		'position',[550,390,590,440])


%     Subsystem  'Text'.

new_system([sys,'/','Text'])
set_param([sys,'/','Text'],'Location',[143,116,560,479])

add_block('built-in/Note',[sys,'/',['Text/This program simulates the adaptive control scheme',13,'discussed in Example 6.4.2. Please refer to Example',13,'6.4.2 on page 370 and Table 6.4.2 on page 368',13,'of the textbook for further details of the problem and ',13,'the algorithm.',13,'']])
set_param([sys,'/',['Text/This program simulates the adaptive control scheme',13,'discussed in Example 6.4.2. Please refer to Example',13,'6.4.2 on page 370 and Table 6.4.2 on page 368',13,'of the textbook for further details of the problem and ',13,'the algorithm.',13,'']],...
		'position',[195,0,200,5])

add_block('built-in/Note',[sys,'/',['Text/5). The adaptive gain and the initial estimates of the',13,'controller parameter']])
set_param([sys,'/',['Text/5). The adaptive gain and the initial estimates of the',13,'controller parameter']],...
		'position',[200,320,205,325])

add_block('built-in/Note',[sys,'/','Text/4) The parameters of the filter'])
set_param([sys,'/','Text/4) The parameters of the filter'],...
		'position',[125,290,130,295])

add_block('built-in/Note',[sys,'/',['Text/3) The plant transfer function. It should be also a second',13,'order transfer function with relative degree equal to two ']])
set_param([sys,'/',['Text/3) The plant transfer function. It should be also a second',13,'order transfer function with relative degree equal to two ']],...
		'position',[215,240,220,245])

add_block('built-in/Note',[sys,'/',['Text/2) The reference model transfer function. It has to be',13,' second order with relative degree equal to two',13,'']])
set_param([sys,'/',['Text/2) The reference model transfer function. It has to be',13,' second order with relative degree equal to two',13,'']],...
		'position',[205,190,210,195])

add_block('built-in/Note',[sys,'/','Text/1) The excitation signal r.'])
set_param([sys,'/','Text/1) The excitation signal r.'],...
		'position',[115,160,120,165])

add_block('built-in/Note',[sys,'/',['Text/You can change the following parameters in the program',13,'to see their effects on the performance of the adaptive',13,'control systems:',13,'']])
set_param([sys,'/',['Text/You can change the following parameters in the program',13,'to see their effects on the performance of the adaptive',13,'control systems:',13,'']],...
		'position',[205,90,210,95])
set_param([sys,'/','Text'],...
		'Mask Display','Double click here\nfor help to run\nthe simulation')


%     Finished composite block 'Text'.

set_param([sys,'/','Text'],...
		'Drop Shadow',4,...
		'position',[87,400,225,481])
add_line(sys,[645,365;655,370])
add_line(sys,[695,400;715,400])
add_line(sys,[145,190;155,190;155,65;325,65])
add_line(sys,[480,185;505,185])
add_line(sys,[490,185;490,130;175,130;185,170])
add_line(sys,[600,185;630,185;630,110;650,110])
add_line(sys,[415,65;630,65;630,90;650,90])
add_line(sys,[690,100;720,100])
add_line(sys,[335,195;345,195;345,165;370,165])
add_line(sys,[375,280;355,280;355,185;370,185])
add_line(sys,[375,310;345,310;345,205;370,205])
add_line(sys,[335,195;335,385;525,385;525,310;510,310])
add_line(sys,[700,100;700,280;510,280])
add_line(sys,[615,65;615,380;655,380])
add_line(sys,[600,185;600,390;655,390])
add_line(sys,[595,400;655,400])
add_line(sys,[595,410;655,410])
add_line(sys,[595,420;655,420])
add_line(sys,[595,430;655,430])
add_line(sys,[355,280;355,415;545,415])
add_line(sys,[155,190;155,220;185,220])
add_line(sys,[630,110;165,110;165,195;185,195])

drawnow

% 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,ts,xts]=',sys,'(t,x,u,flag);'])
		else
			eval(['ret =', sys,'(t,x,u,flag);'])
		end
	else
		[ret,x0,str,ts,xts] = feval(sys);
	end
else
	drawnow % Flash up the model and execute load callback
end
