function [ret,x0,str,ts,xts]=example4(t,x,u,flag);
%EXAMPLE4	is the M-file description of the SIMULINK system named EXAMPLE4.
%	The block-diagram can be displayed by typing: EXAMPLE4.
%
%	SYS=EXAMPLE4(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 EXAMPLE4 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 EXAMPLE4 with a FLAG of zero:
%	[SIZES]=EXAMPLE4([],[],[],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,672,527])
     open_system(sys)
end;
set_param(sys,'algorithm',     'RK-45')
set_param(sys,'Start time',    '0.0')
set_param(sys,'Stop time',     '40')
set_param(sys,'Min step size', '0.0001')
set_param(sys,'Max step size', '0.1')
set_param(sys,'Relative error','1e-3')
set_param(sys,'Return vars',   '')
set_param(sys,'AssignWideVectorLines','on');


%     Subsystem  'Text'.

new_system([sys,'/','Text'])
set_param([sys,'/','Text'],'Location',[147,145,576,495])

add_block('built-in/Note',[sys,'/','Text/4) The noise level (the gain in the Noise Source block)'])
set_param([sys,'/','Text/4) The noise level (the gain in the Noise Source block)'],...
		'position',[225,250,230,255])

add_block('built-in/Note',[sys,'/',['Text/3) The transfer function W(s) in the receiver and in the',13,'Estimator block']])
set_param([sys,'/',['Text/3) The transfer function W(s) in the receiver and in the',13,'Estimator block']],...
		'position',[225,210,230,215])

add_block('built-in/Note',[sys,'/','Text/2) The normalization gain alpha in the Estimator block'])
set_param([sys,'/','Text/2) The normalization gain alpha in the Estimator block'],...
		'position',[225,170,230,175])

add_block('built-in/Note',[sys,'/','Text/1) The adaptive gain (gain_a1, gain_a2 in the Estimator block)'])
set_param([sys,'/','Text/1) The adaptive gain (gain_a1, gain_a2 in the Estimator block)'],...
		'position',[255,135,260,140])

add_block('built-in/Note',[sys,'/',['Text/You can use Start//Stop in the Simulation pull-down',13,'menu to run the program. Or you can change the',13,'following parameters to see how they affect the',13,'simulation results:',13,'']])
set_param([sys,'/',['Text/You can use Start//Stop in the Simulation pull-down',13,'menu to run the program. Or you can change the',13,'following parameters to see how they affect the',13,'simulation results:',13,'']],...
		'position',[220,60,225,65])

add_block('built-in/Note',[sys,'/',['Text/This program simulates the estimation algorithm',13,'discussed in Example 4.3.1. Please refer to page',13,'181 of the textbook for details. ']])
set_param([sys,'/',['Text/This program simulates the estimation algorithm',13,'discussed in Example 4.3.1. Please refer to page',13,'181 of the textbook for details. ']],...
		'position',[215,0,220,5])
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',[387,310,520,386])


%     Subsystem  'Signal Reconstruction'.

new_system([sys,'/','Signal Reconstruction'])
set_param([sys,'/','Signal Reconstruction'],'Location',[14,79,489,361])

add_block('built-in/Fcn',[sys,'/','Signal Reconstruction/Phase'])
set_param([sys,'/','Signal Reconstruction/Phase'],...
		'Expr','acos(u(1)/sqrt(u(1)*u(1)+u(2)*u(2)))',...
		'Mask Display','P',...
		'position',[65,120,105,140])

add_block('built-in/Fcn',[sys,'/','Signal Reconstruction/magnitude'])
set_param([sys,'/','Signal Reconstruction/magnitude'],...
		'Expr','sqrt(u(1)*u(1)+u(2)*u(2))',...
		'Mask Display','A',...
		'position',[65,22,105,48])

add_block('built-in/Outport',[sys,'/','Signal Reconstruction/est_mag'])
set_param([sys,'/','Signal Reconstruction/est_mag'],...
		'BackGround',2,...
		'position',[395,25,415,45])

add_block('built-in/Outport',[sys,'/','Signal Reconstruction/est_phase'])
set_param([sys,'/','Signal Reconstruction/est_phase'],...
		'BackGround',2,...
		'Port','2',...
		'position',[400,85,420,105])

add_block('built-in/Inport',[sys,'/','Signal Reconstruction/theta'])
set_param([sys,'/','Signal Reconstruction/theta'],...
		'BackGround',3,...
		'position',[10,65,30,85])

add_block('built-in/Fcn',[sys,'/','Signal Reconstruction/hat_y'])
set_param([sys,'/','Signal Reconstruction/hat_y'],...
		'Expr','u(1)*sin(2*u(3)+u(2))',...
		'Mask Display','Asin(wt+p)',...
		'position',[310,151,370,179])

add_block('built-in/Outport',[sys,'/','Signal Reconstruction/est_signal'])
set_param([sys,'/','Signal Reconstruction/est_signal'],...
		'BackGround',2,...
		'Port','3',...
		'position',[405,155,425,175])

add_block('built-in/Mux',[sys,'/','Signal Reconstruction/Mux2'])
set_param([sys,'/','Signal Reconstruction/Mux2'],...
		'inputs','3',...
		'Mask Display','',...
		'position',[235,145,275,185])

add_block('built-in/Clock',[sys,'/','Signal Reconstruction/Clock1'])
set_param([sys,'/','Signal Reconstruction/Clock1'],...
		'position',[195,210,215,230])
add_line([sys,'/','Signal Reconstruction'],[280,165;305,165])
add_line([sys,'/','Signal Reconstruction'],[220,220;230,180])
add_line([sys,'/','Signal Reconstruction'],[35,75;50,75;60,130])
add_line([sys,'/','Signal Reconstruction'],[110,130;150,130;150,165;230,165])
add_line([sys,'/','Signal Reconstruction'],[110,35;165,35;165,150;230,150])
add_line([sys,'/','Signal Reconstruction'],[150,130;150,95;395,95])
add_line([sys,'/','Signal Reconstruction'],[165,35;390,35])
add_line([sys,'/','Signal Reconstruction'],[50,75;60,35])
add_line([sys,'/','Signal Reconstruction'],[375,165;400,165])
set_param([sys,'/','Signal Reconstruction'],...
		'Mask Display','plot(sin([0.0:0.1:6.3]))')


%     Finished composite block 'Signal Reconstruction'.

set_param([sys,'/','Signal Reconstruction'],...
		'Drop Shadow',4,...
		'position',[425,154,485,206])

add_block('built-in/Clock',[sys,'/','Clock'])
set_param([sys,'/','Clock'],...
		'orientation',1,...
		'position',[520,80,540,100])

add_block('built-in/Mux',[sys,'/','Mux'])
set_param([sys,'/','Mux'],...
		'inputs','5',...
		'position',[565,98,605,172])

add_block('built-in/To Workspace',[sys,'/','To Workspace'])
set_param([sys,'/','To Workspace'],...
		'mat-name','yout',...
		'buffer','5000',...
		'position',[630,118,685,152])


%     Subsystem  'Estimator'.

new_system([sys,'/','Estimator'])
set_param([sys,'/','Estimator'],'Location',[14,110,696,615])

add_block('built-in/Outport',[sys,'/','Estimator/estimates_'])
set_param([sys,'/','Estimator/estimates_'],...
		'position',[615,110,635,130])

add_block('built-in/Gain',[sys,'/','Estimator/gain_a2'])
set_param([sys,'/','Estimator/gain_a2'],...
		'position',[405,130,430,160])

add_block('built-in/Integrator',[sys,'/','Estimator/int_a2'])
set_param([sys,'/','Estimator/int_a2'],...
		'position',[460,126,485,164])

add_block('built-in/Fcn',[sys,'/','Estimator/ns'])
set_param([sys,'/','Estimator/ns'],...
		'Expr','u(1)*u(1)+u(2)*u(2)',...
		'Mask Display','phi ''phi',...
		'position',[155,314,210,346])

add_block('built-in/Demux',[sys,'/','Estimator/Demux'])
set_param([sys,'/','Estimator/Demux'],...
		'outputs','2',...
		'position',[80,85,120,120])

add_block('built-in/Product',[sys,'/','Estimator/Product1'])
set_param([sys,'/','Estimator/Product1'],...
		'position',[340,127,370,158])

add_block('built-in/Gain',[sys,'/','Estimator/gain_a1'])
set_param([sys,'/','Estimator/gain_a1'],...
		'position',[405,65,430,95])

add_block('built-in/Product',[sys,'/','Estimator/Product'])
set_param([sys,'/','Estimator/Product'],...
		'position',[340,63,370,92])

add_block('built-in/Demux',[sys,'/','Estimator/Demux1'])
set_param([sys,'/','Estimator/Demux1'],...
		'outputs','2',...
		'position',[85,215,125,250])

add_block('built-in/Inport',[sys,'/','Estimator/z'])
set_param([sys,'/','Estimator/z'],...
		'position',[15,195,35,215])

add_block('built-in/Inport',[sys,'/','Estimator/phi'])
set_param([sys,'/','Estimator/phi'],...
		'Port','2',...
		'position',[15,245,35,265])

add_block('built-in/Transfer Fcn',[sys,'/','Estimator/W(s)'])
set_param([sys,'/','Estimator/W(s)'],...
		'Numerator','[2]',...
		'Denominator','[1 2]',...
		'position',[220,218,255,252])

add_block('built-in/Sum',[sys,'/','Estimator/Sum'])
set_param([sys,'/','Estimator/Sum'],...
		'inputs','+--',...
		'position',[290,218,315,252])

add_block('built-in/Transfer Fcn',[sys,'/','Estimator/W(s)_'])
set_param([sys,'/','Estimator/W(s)_'],...
		'orientation',2,...
		'Numerator','[2]',...
		'Denominator','[1 2]',...
		'position',[295,273,340,317])

add_block('built-in/Gain',[sys,'/','Estimator/normalization gain'])
set_param([sys,'/','Estimator/normalization gain'],...
		'orientation',2,...
		'position',[400,277,430,313])

add_block('built-in/Product',[sys,'/','Estimator/ns*epsilon'])
set_param([sys,'/','Estimator/ns*epsilon'],...
		'orientation',2,...
		'position',[520,276,550,309])

add_block('built-in/Fcn',[sys,'/',['Estimator/theta''phi']])
set_param([sys,'/',['Estimator/theta''phi']],...
		'Expr','u(1)*u(3)+u(2)*u(4)',...
		'position',[230,144,280,176])

add_block('built-in/Mux',[sys,'/','Estimator/Mux2'])
set_param([sys,'/','Estimator/Mux2'],...
		'position',[165,135,205,185])

add_block('built-in/Mux',[sys,'/','Estimator/Mux1'])
set_param([sys,'/','Estimator/Mux1'],...
		'inputs','2',...
		'position',[530,100,560,135])

add_block('built-in/Integrator',[sys,'/','Estimator/int_a1'])
set_param([sys,'/','Estimator/int_a1'],...
		'position',[465,58,490,102])

add_block('built-in/Note',[sys,'/','Estimator/epsilon'])
set_param([sys,'/','Estimator/epsilon'],...
		'position',[355,235,360,240])
add_line([sys,'/','Estimator'],[40,205;275,205;285,225])
add_line([sys,'/','Estimator'],[395,295;345,295])
add_line([sys,'/','Estimator'],[290,295;275,295;285,245])
add_line([sys,'/','Estimator'],[125,95;210,95;210,70;335,70])
add_line([sys,'/','Estimator'],[125,110;210,110;210,135;335,135])
add_line([sys,'/','Estimator'],[565,120;610,120])
add_line([sys,'/','Estimator'],[260,235;285,235])
add_line([sys,'/','Estimator'],[40,255;50,255;50,330;150,330])
add_line([sys,'/','Estimator'],[320,235;565,235;555,285])
add_line([sys,'/','Estimator'],[215,330;565,330;555,300])
add_line([sys,'/','Estimator'],[515,295;435,295])
add_line([sys,'/','Estimator'],[495,80;495,110;525,110])
add_line([sys,'/','Estimator'],[490,145;495,145;495,125;525,125])
add_line([sys,'/','Estimator'],[325,235;325,225;320,225;320,85;335,85])
add_line([sys,'/','Estimator'],[320,150;335,150])
add_line([sys,'/','Estimator'],[50,330;50,105;75,105])
add_line([sys,'/','Estimator'],[130,225;135,225;135,165;160,165])
add_line([sys,'/','Estimator'],[130,240;150,240;160,175])
add_line([sys,'/','Estimator'],[150,95;160,145])
add_line([sys,'/','Estimator'],[135,110;135,155;160,155])
add_line([sys,'/','Estimator'],[210,160;225,160])
add_line([sys,'/','Estimator'],[285,160;295,160;295,195;205,195;215,235])
add_line([sys,'/','Estimator'],[375,145;400,145])
add_line([sys,'/','Estimator'],[435,145;455,145])
add_line([sys,'/','Estimator'],[375,80;400,80])
add_line([sys,'/','Estimator'],[435,80;460,80])
add_line([sys,'/','Estimator'],[580,120;580,370;60,370;60,235;80,235])
set_param([sys,'/','Estimator'],...
		'Mask Display','Parameter\nEstimation')


%     Finished composite block 'Estimator'.

set_param([sys,'/','Estimator'],...
		'Drop Shadow',4,...
		'position',[275,150,360,205])

add_block('built-in/Scope',[sys,'/','Scope'])
set_param([sys,'/','Scope'],...
		'Vgain','20.000000',...
		'Hgain','50.000000',...
		'Vmax','40.000000',...
		'Hmax','100.000000',...
		'Window',[784,520,1106,900],...
		'position',[400,45,450,95])

add_block('built-in/Transfer Fcn',[sys,'/','W(s)'])
set_param([sys,'/','W(s)'],...
		'Numerator','[2]',...
		'Denominator','[1 2]',...
		'position',[210,87,245,123])

add_block('built-in/Sum',[sys,'/','Sum'])
set_param([sys,'/','Sum'],...
		'position',[150,89,175,116])


%     Subsystem  'Noise Source'.

new_system([sys,'/','Noise Source'])
set_param([sys,'/','Noise Source'],'Location',[20,85,334,228])

add_block('built-in/Outport',[sys,'/','Noise Source/n'])
set_param([sys,'/','Noise Source/n'],...
		'position',[215,55,235,75])

add_block('built-in/Gain',[sys,'/','Noise Source/Gain'])
set_param([sys,'/','Noise Source/Gain'],...
		'Gain','0',...
		'position',[145,50,175,80])

add_block('built-in/White Noise',[sys,'/','Noise Source/White Noise'])
set_param([sys,'/','Noise Source/White Noise'],...
		'position',[55,49,85,81])
add_line([sys,'/','Noise Source'],[90,65;140,65])
add_line([sys,'/','Noise Source'],[180,65;210,65])
set_param([sys,'/','Noise Source'],...
		'Mask Display','White noise')


%     Finished composite block 'Noise Source'.

set_param([sys,'/','Noise Source'],...
		'Drop Shadow',4,...
		'position',[25,14,115,66])


%     Subsystem  'Signal Generator'.

new_system([sys,'/','Signal Generator'])
set_param([sys,'/','Signal Generator'],'Location',[121,128,608,367])

add_block('built-in/Clock',[sys,'/','Signal Generator/Time'])
set_param([sys,'/','Signal Generator/Time'],...
		'position',[40,145,60,165])

add_block('built-in/Mux',[sys,'/','Signal Generator/Mux'])
set_param([sys,'/','Signal Generator/Mux'],...
		'inputs','3',...
		'position',[125,72,155,108])

add_block('built-in/Step Fcn',[sys,'/','Signal Generator/Phase'])
set_param([sys,'/','Signal Generator/Phase'],...
		'Time','20',...
		'Before','16',...
		'After','25',...
		'position',[35,77,65,103])

add_block('built-in/Fcn',[sys,'/','Signal Generator/Fcn'])
set_param([sys,'/','Signal Generator/Fcn'],...
		'Expr','u(1)*sin(2*u(3)+u(2)*pi/180)',...
		'Mask Display','A sin(wt+p)',...
		'position',[205,73,270,107])

add_block('built-in/Outport',[sys,'/','Signal Generator/sinusoidal signal'])
set_param([sys,'/','Signal Generator/sinusoidal signal'],...
		'position',[315,77,340,103])

add_block('built-in/Step Fcn',[sys,'/','Signal Generator/Magnitude'])
set_param([sys,'/','Signal Generator/Magnitude'],...
		'Time','20',...
		'Before','10',...
		'After','7',...
		'position',[35,7,65,33])
add_line([sys,'/','Signal Generator'],[160,90;200,90])
add_line([sys,'/','Signal Generator'],[70,90;120,90])
add_line([sys,'/','Signal Generator'],[275,90;310,90])
add_line([sys,'/','Signal Generator'],[70,20;95,20;95,80;120,80])
add_line([sys,'/','Signal Generator'],[65,155;95,155;95,100;120,100])
set_param([sys,'/','Signal Generator'],...
		'Mask Display','plot(sin([0.0:0.1:6.3])')


%     Finished composite block 'Signal Generator'.

set_param([sys,'/','Signal Generator'],...
		'Drop Shadow',4,...
		'position',[30,113,110,167])


%     Subsystem  'phi'.

new_system([sys,'/','phi'])
set_param([sys,'/','phi'],'Location',[145,99,510,375])

add_block('built-in/Sine Wave',[sys,'/','phi/sinwt'])
set_param([sys,'/','phi/sinwt'],...
		'frequency','2',...
		'position',[50,54,80,86])

add_block('built-in/Sine Wave',[sys,'/','phi/coswt'])
set_param([sys,'/','phi/coswt'],...
		'frequency','2',...
		'phase','0.5*pi',...
		'position',[50,119,80,151])

add_block('built-in/Mux',[sys,'/','phi/Mux'])
set_param([sys,'/','phi/Mux'],...
		'inputs','2',...
		'position',[160,85,190,120])

add_block('built-in/Outport',[sys,'/','phi/phi'])
set_param([sys,'/','phi/phi'],...
		'position',[275,95,295,115])
add_line([sys,'/','phi'],[195,105;270,105])
add_line([sys,'/','phi'],[85,70;130,70;130,95;155,95])
add_line([sys,'/','phi'],[85,135;130,135;130,110;155,110])
set_param([sys,'/','phi'],...
		'Mask Display','phi=[sinwt coswt]')


%     Finished composite block 'phi'.

set_param([sys,'/','phi'],...
		'Drop Shadow',4,...
		'position',[5,229,135,291])
add_line(sys,[180,105;190,105;190,240;535,240;535,165;560,165])
add_line(sys,[180,105;205,105])
add_line(sys,[250,105;260,105;270,165])
add_line(sys,[490,180;515,180;515,135;560,135])
add_line(sys,[610,135;625,135])
add_line(sys,[530,105;560,105])
add_line(sys,[120,40;130,40;130,95;145,95])
add_line(sys,[365,180;420,180])
add_line(sys,[365,180;365,70;395,70])
add_line(sys,[490,195;525,195;525,150;560,150])
add_line(sys,[490,165;505,165;505,120;560,120])
add_line(sys,[115,140;130,140;130,110;145,110])
add_line(sys,[140,260;260,260;270,190])

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
