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

add_block('built-in/Note',[sys,'/','u'])
set_param([sys,'/','u'],...
		'position',[80,95,85,100])


%     Subsystem  'Noise Source'.

new_system([sys,'/','Noise Source'])
set_param([sys,'/','Noise Source'],'Location',[35,119,300,258])

add_block('built-in/White Noise',[sys,'/','Noise Source/White Noise'])
set_param([sys,'/','Noise Source/White Noise'],...
		'position',[45,35,75,65])

add_block('built-in/Gain',[sys,'/','Noise Source/Gain'])
set_param([sys,'/','Noise Source/Gain'],...
		'Gain','0.05',...
		'position',[110,35,145,65])

add_block('built-in/Outport',[sys,'/','Noise Source/n'])
set_param([sys,'/','Noise Source/n'],...
		'position',[195,34,220,66])
add_line([sys,'/','Noise Source'],[80,50;105,50])
add_line([sys,'/','Noise Source'],[150,50;190,50])
set_param([sys,'/','Noise Source'],...
		'Mask Display','White noise')


%     Finished composite block 'Noise Source'.

set_param([sys,'/','Noise Source'],...
		'Drop Shadow',4,...
		'position',[5,15,100,65])

add_block('built-in/To Workspace',[sys,'/','To Workspace'])
set_param([sys,'/','To Workspace'],...
		'mat-name','yout',...
		'buffer','10000',...
		'position',[675,216,725,244])

add_block('built-in/Clock',[sys,'/','Clock'])
set_param([sys,'/','Clock'],...
		'orientation',1,...
		'position',[565,170,585,190])

add_block('built-in/Mux',[sys,'/','Mux1'])
set_param([sys,'/','Mux1'],...
		'position',[590,208,635,252])


%     Subsystem  'Estimated Model'.

new_system([sys,'/','Estimated Model'])
set_param([sys,'/','Estimated Model'],'Location',[91,197,592,449])

add_block('built-in/Product',[sys,'/','Estimated Model/Product'])
set_param([sys,'/','Estimated Model/Product'],...
		'position',[220,29,250,56])

add_block('built-in/Inport',[sys,'/','Estimated Model/x'])
set_param([sys,'/','Estimated Model/x'],...
		'Port','2',...
		'position',[30,75,50,95])

add_block('built-in/Outport',[sys,'/','Estimated Model/hat_x'])
set_param([sys,'/','Estimated Model/hat_x'],...
		'position',[470,65,490,85])

add_block('built-in/Sum',[sys,'/','Estimated Model/Sum'])
set_param([sys,'/','Estimated Model/Sum'],...
		'inputs','++-',...
		'position',[330,57,355,93])

add_block('built-in/Gain',[sys,'/','Estimated Model/a_m'])
set_param([sys,'/','Estimated Model/a_m'],...
		'orientation',2,...
		'position',[365,122,390,148])


%     Subsystem  'Estimated Model/nonlinear'.

new_system([sys,'/','Estimated Model/nonlinear'])
set_param([sys,'/','Estimated Model/nonlinear'],'Location',[50,193,288,340])

add_block('built-in/Outport',[sys,'/','Estimated Model/nonlinear/out_1'])
set_param([sys,'/','Estimated Model/nonlinear/out_1'],...
		'position',[195,65,215,85])

add_block('built-in/Fcn',[sys,'/','Estimated Model/nonlinear/Fcn'])
set_param([sys,'/','Estimated Model/nonlinear/Fcn'],...
		'Expr','(1-u(2))*u(1)',...
		'position',[125,65,165,85])

add_block('built-in/Mux',[sys,'/','Estimated Model/nonlinear/Mux'])
set_param([sys,'/','Estimated Model/nonlinear/Mux'],...
		'inputs','2',...
		'position',[65,55,95,90])

add_block('built-in/Inport',[sys,'/','Estimated Model/nonlinear/x'])
set_param([sys,'/','Estimated Model/nonlinear/x'],...
		'position',[5,35,25,55])

add_block('built-in/Inport',[sys,'/','Estimated Model/nonlinear/hat_a'])
set_param([sys,'/','Estimated Model/nonlinear/hat_a'],...
		'Port','2',...
		'position',[5,85,25,105])
add_line([sys,'/','Estimated Model/nonlinear'],[100,75;120,75])
add_line([sys,'/','Estimated Model/nonlinear'],[30,45;60,65])
add_line([sys,'/','Estimated Model/nonlinear'],[170,75;190,75])
add_line([sys,'/','Estimated Model/nonlinear'],[30,95;60,80])
set_param([sys,'/','Estimated Model/nonlinear'],...
		'Mask Display','(a_m-hat_a)x')


%     Finished composite block 'Estimated Model/nonlinear'.

set_param([sys,'/','Estimated Model/nonlinear'],...
		'position',[170,76,280,114])

add_block('built-in/Demux',[sys,'/','Estimated Model/Demux'])
set_param([sys,'/','Estimated Model/Demux'],...
		'outputs','2',...
		'position',[75,150,120,190])

add_block('built-in/Inport',[sys,'/','Estimated Model/u'])
set_param([sys,'/','Estimated Model/u'],...
		'position',[30,25,50,45])

add_block('built-in/Inport',[sys,'/','Estimated Model/Estimates'])
set_param([sys,'/','Estimated Model/Estimates'],...
		'Port','3',...
		'position',[30,160,50,180])

add_block('built-in/Integrator',[sys,'/','Estimated Model/int_x'])
set_param([sys,'/','Estimated Model/int_x'],...
		'position',[380,52,415,98])
add_line([sys,'/','Estimated Model'],[55,85;165,85])
add_line([sys,'/','Estimated Model'],[420,75;465,75])
add_line([sys,'/','Estimated Model'],[360,75;375,75])
add_line([sys,'/','Estimated Model'],[420,75;445,75;445,135;395,135])
add_line([sys,'/','Estimated Model'],[55,170;70,170])
add_line([sys,'/','Estimated Model'],[55,35;215,35])
add_line([sys,'/','Estimated Model'],[125,180;145,180;145,50;215,50])
add_line([sys,'/','Estimated Model'],[125,160;130,160;130,105;165,105])
add_line([sys,'/','Estimated Model'],[285,95;285,75;325,75])
add_line([sys,'/','Estimated Model'],[360,135;305,135;305,85;325,85])
add_line([sys,'/','Estimated Model'],[255,45;285,45;285,65;325,65])
set_param([sys,'/','Estimated Model'],...
		'Mask Display','Series-parallel\nModel')


%     Finished composite block 'Estimated Model'.

set_param([sys,'/','Estimated Model'],...
		'Drop Shadow',4,...
		'position',[200,192,310,258])

add_block('built-in/Scope',[sys,'/','Scope'])
set_param([sys,'/','Scope'],...
		'Vgain','3.000000',...
		'Hgain','30.000000',...
		'Vmax','6.000000',...
		'Hmax','60.000000',...
		'Window',[840,584,1120,804],...
		'position',[590,113,630,147])


%     Subsystem  ['Text',13,''].

new_system([sys,'/',['Text',13,'']])
set_param([sys,'/',['Text',13,'']],'Location',[207,84,664,487])

add_block('built-in/Note',[sys,'/',['Text',13,'/5) Change the initial estimates of a and b by changing the ',13,'initial conditions for the integrators in the Estimator block.']])
set_param([sys,'/',['Text',13,'/5) Change the initial estimates of a and b by changing the ',13,'initial conditions for the integrators in the Estimator block.']],...
		'position',[215,320,220,325])

add_block('built-in/Note',[sys,'/',['Text',13,'/4) Change the estimation algorithm by double clicking the',13,' Estimator block and changing the gains.']])
set_param([sys,'/',['Text',13,'/4) Change the estimation algorithm by double clicking the',13,' Estimator block and changing the gains.']],...
		'position',[215,275,220,280])

add_block('built-in/Note',[sys,'/',['Text',13,'/3) Change the plant by changing the transfer function in the',13,'plant model ( first order transfer function).']])
set_param([sys,'/',['Text',13,'/3) Change the plant by changing the transfer function in the',13,'plant model ( first order transfer function).']],...
		'position',[220,225,225,230])

add_block('built-in/Note',[sys,'/',['Text',13,'/2) Change the noise level by double clicking the Noise ',13,'Source block and changing the gain.']])
set_param([sys,'/',['Text',13,'/2) Change the noise level by double clicking the Noise ',13,'Source block and changing the gain.']],...
		'position',[210,180,215,185])

add_block('built-in/Note',[sys,'/',['Text',13,'/1) Change the excitation input by changing the parameters',13,' in the Signal Generator.']])
set_param([sys,'/',['Text',13,'/1) Change the excitation input by changing the parameters',13,' in the Signal Generator.']],...
		'position',[225,140,230,145])

add_block('built-in/Note',[sys,'/',['Text',13,'/You can use Start//Stop in the Simulation pull down menu',13,' to run this program. Or you can choose to make the following',13,'changes before you start the simulation:  ']])
set_param([sys,'/',['Text',13,'/You can use Start//Stop in the Simulation pull down menu',13,' to run this program. Or you can choose to make the following',13,'changes before you start the simulation:  ']],...
		'position',[225,75,230,80])

add_block('built-in/Note',[sys,'/',['Text',13,'/',13,'This program simulates the series-parallel estimation ',13,'algorithm for a first order plant discussed in Chapter 4.']])
set_param([sys,'/',['Text',13,'/',13,'This program simulates the series-parallel estimation ',13,'algorithm for a first order plant discussed in Chapter 4.']],...
		'position',[230,10,235,15])
set_param([sys,'/',['Text',13,'']],...
		'Mask Display','Double click here\nfor help to run\nthe simulation')


%     Finished composite block ['Text',13,''].

set_param([sys,'/',['Text',13,'']],...
		'Drop Shadow',4,...
		'position',[497,345,635,411])

add_block('built-in/Transfer Fcn',[sys,'/','Plant Model'])
set_param([sys,'/','Plant Model'],...
		'Denominator','[1 2]',...
		'position',[180,105,240,145])

add_block('built-in/Sum',[sys,'/','x+n'])
set_param([sys,'/','x+n'],...
		'position',[270,103,295,132])



add_block('built-in/Note',[sys,'/','x'])
set_param([sys,'/','x'],...
		'position',[255,120,260,125])

add_block('built-in/Demux',[sys,'/','Demux'])
set_param([sys,'/','Demux'],...
		'outputs','2',...
		'position',[425,242,485,278])

add_block('built-in/Signal Generator',[sys,'/',['Signal',13,'Generator']])
set_param([sys,'/',['Signal',13,'Generator']],...
		'Peak','1.000000',...
		'Peak Range','5.000000',...
		'Freq','5.000000',...
		'Freq Range','5.000000',...
		'Wave','Sin',...
		'Units','Rads',...
		'position',[5,106,60,144])

add_block('built-in/Sum',[sys,'/','epsilon'])
set_param([sys,'/','epsilon'],...
		'inputs','+-',...
		'position',[335,111,370,144])


%     Subsystem  'Series-Parallel alg.'.

new_system([sys,'/','Series-Parallel alg.'])
set_param([sys,'/','Series-Parallel alg.'],'Location',[123,191,620,460])

add_block('built-in/Product',[sys,'/','Series-Parallel alg./Product'])
set_param([sys,'/','Series-Parallel alg./Product'],...
		'position',[145,33,170,62])

add_block('built-in/Product',[sys,'/','Series-Parallel alg./Product1'])
set_param([sys,'/','Series-Parallel alg./Product1'],...
		'position',[140,104,165,131])

add_block('built-in/Gain',[sys,'/','Series-Parallel alg./b_gain'])
set_param([sys,'/','Series-Parallel alg./b_gain'],...
		'position',[195,104,230,136])

add_block('built-in/Gain',[sys,'/','Series-Parallel alg./a_gain'])
set_param([sys,'/','Series-Parallel alg./a_gain'],...
		'Gain','-1',...
		'position',[195,35,230,65])

add_block('built-in/Outport',[sys,'/','Series-Parallel alg./estimates'])
set_param([sys,'/','Series-Parallel alg./estimates'],...
		'position',[455,75,480,105])

add_block('built-in/Mux',[sys,'/','Series-Parallel alg./Mux'])
set_param([sys,'/','Series-Parallel alg./Mux'],...
		'inputs','2',...
		'position',[385,71,415,104])

add_block('built-in/Integrator',[sys,'/','Series-Parallel alg./int_b'])
set_param([sys,'/','Series-Parallel alg./int_b'],...
		'Initial','2',...
		'position',[270,98,300,142])

add_block('built-in/Integrator',[sys,'/','Series-Parallel alg./int_a'])
set_param([sys,'/','Series-Parallel alg./int_a'],...
		'Initial','0.5',...
		'position',[270,29,300,71])

add_block('built-in/Inport',[sys,'/','Series-Parallel alg./x'])
set_param([sys,'/','Series-Parallel alg./x'],...
		'Port','2',...
		'position',[50,79,80,111])

add_block('built-in/Inport',[sys,'/','Series-Parallel alg./u'])
set_param([sys,'/','Series-Parallel alg./u'],...
		'Port','3',...
		'position',[50,144,80,176])

add_block('built-in/Inport',[sys,'/','Series-Parallel alg./epsilon'])
set_param([sys,'/','Series-Parallel alg./epsilon'],...
		'position',[50,24,80,56])
add_line([sys,'/','Series-Parallel alg.'],[170,120;190,120])
add_line([sys,'/','Series-Parallel alg.'],[175,50;190,50])
add_line([sys,'/','Series-Parallel alg.'],[305,50;340,50;340,80;380,80])
add_line([sys,'/','Series-Parallel alg.'],[305,120;340,120;340,95;380,95])
add_line([sys,'/','Series-Parallel alg.'],[85,40;115,40;115,110;135,110])
add_line([sys,'/','Series-Parallel alg.'],[85,40;140,40])
add_line([sys,'/','Series-Parallel alg.'],[420,90;450,90])
add_line([sys,'/','Series-Parallel alg.'],[235,50;265,50])
add_line([sys,'/','Series-Parallel alg.'],[235,120;265,120])
add_line([sys,'/','Series-Parallel alg.'],[85,95;105,95;105,55;140,55])
add_line([sys,'/','Series-Parallel alg.'],[85,160;115,160;115,125;135,125])
set_param([sys,'/','Series-Parallel alg.'],...
		'Mask Display','Estimator')


%     Finished composite block 'Series-Parallel alg.'.

set_param([sys,'/','Series-Parallel alg.'],...
		'Drop Shadow',4,...
		'position',[425,102,510,158])
add_line(sys,[575,195;585,215])
add_line(sys,[640,230;670,230])
add_line(sys,[65,125;175,125])
add_line(sys,[515,130;585,130])
add_line(sys,[245,125;265,125])
add_line(sys,[300,120;330,120])
add_line(sys,[315,225;320,225;330,135])
add_line(sys,[375,130;390,130;390,110;420,110])
add_line(sys,[90,125;90,205;195,205])
add_line(sys,[515,130;540,130;540,300;110,300;110,245;195,245])
add_line(sys,[300,120;305,120;305,180;110,180;110,225;195,225])
add_line(sys,[310,120;310,100;405,100;405,130;420,130])
add_line(sys,[395,300;395,260;420,260])
add_line(sys,[490,250;515,250;515,235;585,235])
add_line(sys,[490,270;520,270;520,245;585,245])
add_line(sys,[90,160;390,160;390,150;420,150])
add_line(sys,[105,40;250,40;250,110;265,110])
add_line(sys,[380,130;380,225;585,225])

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
