function [ret,x0,str,ts,xts]=example3(t,x,u,flag);
%EXAMPLE3	is the M-file description of the SIMULINK system named EXAMPLE3.
%	The block-diagram can be displayed by typing: EXAMPLE3.
%
%	SYS=EXAMPLE3(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 EXAMPLE3 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 EXAMPLE3 with a FLAG of zero:
%	[SIZES]=EXAMPLE3([],[],[],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',[28,58,760,567])
     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/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','Sin',...
		'Units','Rads',...
		'Mask Display','',...
		'position',[20,66,85,114])


%     Subsystem  'Plant Model'.

new_system([sys,'/','Plant Model'])
set_param([sys,'/','Plant Model'],'Location',[260,142,582,369])

add_block('built-in/Outport',[sys,'/','Plant Model/x'])
set_param([sys,'/','Plant Model/x'],...
		'BackGround',2,...
		'position',[240,85,260,105])

add_block('built-in/Integrator',[sys,'/','Plant Model/int_x'])
set_param([sys,'/','Plant Model/int_x'],...
		'position',[145,77,170,113])

add_block('built-in/Gain',[sys,'/','Plant Model/unknown_a'])
set_param([sys,'/','Plant Model/unknown_a'],...
		'orientation',2,...
		'Gain','0',...
		'position',[135,137,165,163])

add_block('built-in/Sum',[sys,'/','Plant Model/Sum'])
set_param([sys,'/','Plant Model/Sum'],...
		'inputs','+-',...
		'position',[90,77,110,108])

add_block('built-in/Inport',[sys,'/','Plant Model/u'])
set_param([sys,'/','Plant Model/u'],...
		'BackGround',3,...
		'position',[40,75,60,95])
add_line([sys,'/','Plant Model'],[130,150;75,150;85,100])
add_line([sys,'/','Plant Model'],[175,95;190,95;190,150;170,150])
add_line([sys,'/','Plant Model'],[175,95;235,95])
add_line([sys,'/','Plant Model'],[65,85;85,85])
add_line([sys,'/','Plant Model'],[115,95;140,95])
set_param([sys,'/','Plant Model'],...
		'Mask Display','1\n-----------\ns+a')


%     Finished composite block 'Plant Model'.

set_param([sys,'/','Plant Model'],...
		'position',[145,38,215,82])

add_block('built-in/Sum',[sys,'/','epsilon_1'])
set_param([sys,'/','epsilon_1'],...
		'inputs','+-',...
		'position',[280,94,305,121])

add_block('built-in/Note',[sys,'/','a'])
set_param([sys,'/','a'],...
		'position',[455,50,460,55])


%     Subsystem  'Text'.

new_system([sys,'/','Text'])
set_param([sys,'/','Text'],'Location',[244,117,648,463])

add_block('built-in/Note',[sys,'/',['Text/4) The initial estimate of a (the initial condition for the ',13,'integrator in the Estimator block']])
set_param([sys,'/',['Text/4) The initial estimate of a (the initial condition for the ',13,'integrator in the Estimator block']],...
		'position',[200,280,205,285])

add_block('built-in/Note',[sys,'/','Text/3) The adaptive gain in the Estimator block'])
set_param([sys,'/','Text/3) The adaptive gain in the Estimator block'],...
		'position',[170,250,175,255])

add_block('built-in/Note',[sys,'/',['Text/2) The plant parameter a in the Plant Model blcok',13,'(marked with "unknown_a")']])
set_param([sys,'/',['Text/2) The plant parameter a in the Plant Model blcok',13,'(marked with "unknown_a")']],...
		'position',[190,205,195,210])

add_block('built-in/Note',[sys,'/','Text/1) The normalization gain in the Estimator block. '])
set_param([sys,'/','Text/1) The normalization gain in the Estimator block. '],...
		'position',[190,170,195,175])

add_block('built-in/Note',[sys,'/',['Text/Use Start//Stop under the pull-down menu Simulation to',13,'run the program.  You can change the following',13,'parameters before running the simulation']])
set_param([sys,'/',['Text/Use Start//Stop under the pull-down menu Simulation to',13,'run the program.  You can change the following',13,'parameters before running the simulation']],...
		'position',[195,90,200,95])

add_block('built-in/Note',[sys,'/',['Text/This simulink program uses a first order example to',13,'demonstrate the effects of normalization on the',13,'performance of the estimator (see discussion in ',13,'subsection 4.3.2 of the textbook for details)']])
set_param([sys,'/',['Text/This simulink program uses a first order example to',13,'demonstrate the effects of normalization on the',13,'performance of the estimator (see discussion in ',13,'subsection 4.3.2 of the textbook for details)']],...
		'position',[185,10,190,15])
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',[335,270,465,336])


%     Subsystem  'Estimation Model'.

new_system([sys,'/','Estimation Model'])
set_param([sys,'/','Estimation Model'],'Location',[90,163,680,471])

add_block('built-in/Inport',[sys,'/','Estimation Model/u'])
set_param([sys,'/','Estimation Model/u'],...
		'position',[30,51,55,79])

add_block('built-in/Inport',[sys,'/','Estimation Model/estimate_a'])
set_param([sys,'/','Estimation Model/estimate_a'],...
		'Port','3',...
		'position',[30,147,55,173])

add_block('built-in/Inport',[sys,'/','Estimation Model/x'])
set_param([sys,'/','Estimation Model/x'],...
		'Port','2',...
		'position',[30,97,55,123])

add_block('built-in/Fcn',[sys,'/','Estimation Model/Nonlinear'])
set_param([sys,'/','Estimation Model/Nonlinear'],...
		'Expr','(1-u(2))*u(1)',...
		'Mask Display','(a_m-hat_a)*x',...
		'position',[200,103,315,137])

add_block('built-in/Sum',[sys,'/','Estimation Model/Sum'])
set_param([sys,'/','Estimation Model/Sum'],...
		'inputs','++-',...
		'position',[365,57,385,93])

add_block('built-in/Integrator',[sys,'/','Estimation Model/Integrator'])
set_param([sys,'/','Estimation Model/Integrator'],...
		'position',[415,54,440,96])

add_block('built-in/Outport',[sys,'/','Estimation Model/hat_x'])
set_param([sys,'/','Estimation Model/hat_x'],...
		'position',[490,62,515,88])

add_block('built-in/Gain',[sys,'/','Estimation Model/Gain'])
set_param([sys,'/','Estimation Model/Gain'],...
		'orientation',2,...
		'position',[370,127,395,163])

add_block('built-in/Mux',[sys,'/','Estimation Model/Mux'])
set_param([sys,'/','Estimation Model/Mux'],...
		'inputs','2',...
		'position',[115,120,145,155])
add_line([sys,'/','Estimation Model'],[60,65;360,65])
add_line([sys,'/','Estimation Model'],[445,75;470,75;470,145;400,145])
add_line([sys,'/','Estimation Model'],[390,75;410,75])
add_line([sys,'/','Estimation Model'],[445,75;485,75])
add_line([sys,'/','Estimation Model'],[365,145;340,145;340,85;360,85])
add_line([sys,'/','Estimation Model'],[60,110;75,110;75,130;110,130])
add_line([sys,'/','Estimation Model'],[60,160;75,160;75,145;110,145])
add_line([sys,'/','Estimation Model'],[320,120;325,120;325,75;360,75])
add_line([sys,'/','Estimation Model'],[150,140;170,140;170,120;195,120])
set_param([sys,'/','Estimation Model'],...
		'Mask Display','Series-Parallel\nModel')


%     Finished composite block 'Estimation Model'.

set_param([sys,'/','Estimation Model'],...
		'Drop Shadow',4,...
		'position',[140,125,250,185])


%     Subsystem  'Gradient with Normalization'.

new_system([sys,'/','Gradient with Normalization'])
set_param([sys,'/','Gradient with Normalization'],'Location',[35,58,704,431])

add_block('built-in/Product',[sys,'/','Gradient with Normalization/Product1'])
set_param([sys,'/','Gradient with Normalization/Product1'],...
		'position',[335,54,360,81])

add_block('built-in/Inport',[sys,'/','Gradient with Normalization/x'])
set_param([sys,'/','Gradient with Normalization/x'],...
		'position',[35,27,60,53])

add_block('built-in/Product',[sys,'/','Gradient with Normalization/ns'])
set_param([sys,'/','Gradient with Normalization/ns'],...
		'position',[110,58,135,87])

add_block('built-in/Product',[sys,'/','Gradient with Normalization/Product'])
set_param([sys,'/','Gradient with Normalization/Product'],...
		'orientation',2,...
		'position',[230,115,255,150])

add_block('built-in/Transfer Fcn',[sys,'/','Gradient with Normalization/1//(s+am)'])
set_param([sys,'/','Gradient with Normalization/1//(s+am)'],...
		'orientation',2,...
		'Denominator','[1 2]',...
		'position',[155,116,205,154])

add_block('built-in/Sum',[sys,'/','Gradient with Normalization/epsilon'])
set_param([sys,'/','Gradient with Normalization/epsilon'],...
		'inputs','-+',...
		'position',[195,188,220,217])

add_block('built-in/Inport',[sys,'/','Gradient with Normalization/epsilon_1'])
set_param([sys,'/','Gradient with Normalization/epsilon_1'],...
		'Port','2',...
		'position',[35,197,60,223])

add_block('built-in/Outport',[sys,'/','Gradient with Normalization/estimate_a'])
set_param([sys,'/','Gradient with Normalization/estimate_a'],...
		'position',[590,58,615,82])

add_block('built-in/Integrator',[sys,'/','Gradient with Normalization/int_a'])
set_param([sys,'/','Gradient with Normalization/int_a'],...
		'Initial','4',...
		'position',[505,51,535,89])

add_block('built-in/Gain',[sys,'/','Gradient with Normalization/Adaptive_gain'])
set_param([sys,'/','Gradient with Normalization/Adaptive_gain'],...
		'Gain','-1',...
		'position',[420,54,450,86])

add_block('built-in/Gain',[sys,'/','Gradient with Normalization/normalizing gain'])
set_param([sys,'/','Gradient with Normalization/normalizing gain'],...
		'Gain','10',...
		'position',[185,56,220,94])
add_line([sys,'/','Gradient with Normalization'],[65,40;300,40;300,60;330,60])
add_line([sys,'/','Gradient with Normalization'],[365,70;415,70])
add_line([sys,'/','Gradient with Normalization'],[455,70;500,70])
add_line([sys,'/','Gradient with Normalization'],[540,70;585,70])
add_line([sys,'/','Gradient with Normalization'],[85,40;85,80;105,80])
add_line([sys,'/','Gradient with Normalization'],[85,65;105,65])
add_line([sys,'/','Gradient with Normalization'],[140,75;180,75])
add_line([sys,'/','Gradient with Normalization'],[225,75;280,75;280,125;260,125])
add_line([sys,'/','Gradient with Normalization'],[225,205;300,205;300,140;260,140])
add_line([sys,'/','Gradient with Normalization'],[225,135;210,135])
add_line([sys,'/','Gradient with Normalization'],[65,210;190,210])
add_line([sys,'/','Gradient with Normalization'],[150,135;100,135;100,195;190,195])
add_line([sys,'/','Gradient with Normalization'],[300,140;300,75;330,75])
set_param([sys,'/','Gradient with Normalization'],...
		'Mask Display','Estimator')


%     Finished composite block 'Gradient with Normalization'.

set_param([sys,'/','Gradient with Normalization'],...
		'Drop Shadow',4,...
		'position',[395,48,470,97])

add_block('built-in/Scope',[sys,'/','Scope'])
set_param([sys,'/','Scope'],...
		'Vgain','5.000000',...
		'Hgain','50.000000',...
		'Vmax','10.000000',...
		'Hmax','100.000000',...
		'Window',[872,536,1152,756],...
		'position',[550,58,590,92])

add_block('built-in/Clock',[sys,'/','Clock'])
set_param([sys,'/','Clock'],...
		'orientation',1,...
		'position',[515,150,535,170])

add_block('built-in/To Workspace',[sys,'/','To Workspace'])
set_param([sys,'/','To Workspace'],...
		'mat-name','yout',...
		'buffer','10000',...
		'position',[640,208,685,232])

add_block('built-in/Mux',[sys,'/','Mux1'])
set_param([sys,'/','Mux1'],...
		'inputs','3',...
		'position',[540,198,575,242])
add_line(sys,[475,75;545,75])
add_line(sys,[580,220;635,220])
add_line(sys,[525,175;535,205])
add_line(sys,[90,90;105,90;105,135;135,135])
add_line(sys,[105,90;105,60;140,60])
add_line(sys,[220,60;260,60;260,100;125,100;135,155])
add_line(sys,[260,85;260,100;275,100])
add_line(sys,[255,155;260,155;260,115;275,115])
add_line(sys,[260,60;390,60])
add_line(sys,[310,110;325,110;325,85;390,85])
add_line(sys,[475,75;485,75;485,195;125,195;135,175])
add_line(sys,[325,110;325,220;535,220])
add_line(sys,[455,195;455,235;535,235])

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
