function [ret,x0,str,ts,xts]=example1(t,x,u,flag);
%EXAMPLE1	is the M-file description of the SIMULINK system named EXAMPLE1.
%	The block-diagram can be displayed by typing: EXAMPLE1.
%
%	SYS=EXAMPLE1(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 EXAMPLE1 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 EXAMPLE1 with a FLAG of zero:
%	[SIZES]=EXAMPLE1([],[],[],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',[52,146,960,791])
     open_system(sys)
end;
set_param(sys,'algorithm',     'RK-45')
set_param(sys,'Start time',    '0.0')
set_param(sys,'Stop time',     '200')
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',   '')


%     Subsystem  'Text'.

new_system([sys,'/','Text'])
set_param([sys,'/','Text'],'Location',[84,58,536,415])

add_block('built-in/Note',[sys,'/','Text/4). The excitation signal and the noise level'])
set_param([sys,'/','Text/4). The excitation signal and the noise level'],...
		'position',[195,280,200,285])

add_block('built-in/Note',[sys,'/',['Text/3). The initial estimates (the initial conditions for the',13,'integrator in the estimation algorithm block)']])
set_param([sys,'/',['Text/3). The initial estimates (the initial conditions for the',13,'integrator in the estimation algorithm block)']],...
		'position',[225,240,230,245])

add_block('built-in/Note',[sys,'/',['Text/2). The adaptive gains (the parameters Gain and Gain1',13,'in the estimation algorithm block)']])
set_param([sys,'/',['Text/2). The adaptive gains (the parameters Gain and Gain1',13,'in the estimation algorithm block)']],...
		'position',[240,195,245,200])

add_block('built-in/Note',[sys,'/',['Text/1). The plant parameters a and b by changing',13,' the transfer function of the plant',13,' ']])
set_param([sys,'/',['Text/1). The plant parameters a and b by changing',13,' the transfer function of the plant',13,' ']],...
		'position',[210,145,215,150])

add_block('built-in/Note',[sys,'/',['Text/You can change the following parameters before',13,'running the simulation and see their effects on the',13,'estimator performance:']])
set_param([sys,'/',['Text/You can change the following parameters before',13,'running the simulation and see their effects on the',13,'estimator performance:']],...
		'position',[220,85,225,90])

add_block('built-in/Note',[sys,'/',['Text/This simulink program is used to implement the parallel',13,'estimator discussed in section 4.2.2 of Chapter 4 for a',13,'first order example. Start the simulation using Start//Stop',13,' in the simulink Simulation pull-down manu.']])
set_param([sys,'/',['Text/This simulink program is used to implement the parallel',13,'estimator discussed in section 4.2.2 of Chapter 4 for a',13,'first order example. Start the simulation using Start//Stop',13,' in the simulink Simulation pull-down manu.']],...
		'position',[230,10,235,15])
set_param([sys,'/','Text'],...
		'Mask Display','Double click here\n for help to run\nthis simulation')


%     Finished composite block 'Text'.

set_param([sys,'/','Text'],...
		'Drop Shadow',4,...
		'position',[577,457,715,526])

add_block('built-in/To Workspace',[sys,'/','To Workspace'])
set_param([sys,'/','To Workspace'],...
		'mat-name','yout',...
		'buffer','10000',...
		'position',[825,263,875,287])

add_block('built-in/Mux',[sys,'/','Mux1'])
set_param([sys,'/','Mux1'],...
		'position',[725,245,775,300])

add_block('built-in/Demux',[sys,'/','Demux'])
set_param([sys,'/','Demux'],...
		'outputs','2',...
		'position',[615,327,680,363])


%     Subsystem  'Parallel Model'.

new_system([sys,'/','Parallel Model'])
set_param([sys,'/','Parallel Model'],'Location',[60,58,620,479])

add_block('built-in/Product',[sys,'/','Parallel Model/Product1'])
set_param([sys,'/','Parallel Model/Product1'],...
		'orientation',2,...
		'position',[370,152,400,188])

add_block('built-in/Integrator',[sys,'/','Parallel Model/Integrator'])
set_param([sys,'/','Parallel Model/Integrator'],...
		'position',[385,77,420,123])

add_block('built-in/Demux',[sys,'/','Parallel Model/Demux'])
set_param([sys,'/','Parallel Model/Demux'],...
		'outputs','2',...
		'position',[115,219,175,266])

add_block('built-in/Sum',[sys,'/','Parallel Model/Sum'])
set_param([sys,'/','Parallel Model/Sum'],...
		'inputs','+-',...
		'position',[325,80,355,120])

add_block('built-in/Product',[sys,'/','Parallel Model/Product'])
set_param([sys,'/','Parallel Model/Product'],...
		'position',[240,70,270,105])


add_block('built-in/Inport',[sys,'/','Parallel Model/estimates'])
set_param([sys,'/','Parallel Model/estimates'],...
		'Port','2',...
		'position',[35,227,75,263])

add_block('built-in/Outport',[sys,'/','Parallel Model/hat_x'])
set_param([sys,'/','Parallel Model/hat_x'],...
		'position',[505,82,545,118])

add_block('built-in/Inport',[sys,'/','Parallel Model/u'])
set_param([sys,'/','Parallel Model/u'],...
		'position',[35,64,70,96])
add_line([sys,'/','Parallel Model'],[365,170;310,170;320,110])
add_line([sys,'/','Parallel Model'],[275,90;320,90])
add_line([sys,'/','Parallel Model'],[425,100;450,100;450,160;405,160])
add_line([sys,'/','Parallel Model'],[360,100;380,100])
add_line([sys,'/','Parallel Model'],[75,80;235,80])
add_line([sys,'/','Parallel Model'],[425,100;500,100])
add_line([sys,'/','Parallel Model'],[80,245;110,245])
add_line([sys,'/','Parallel Model'],[180,230;450,230;450,180;405,180])
add_line([sys,'/','Parallel Model'],[180,255;205,255;205,95;235,95])
set_param([sys,'/','Parallel Model'],...
		'Mask Display','Estimated Model')


%     Finished composite block 'Parallel Model'.

set_param([sys,'/','Parallel Model'],...
		'Drop Shadow',4,...
		'position',[140,303,280,367])


%     Subsystem  'Estimation Algorithm'.

new_system([sys,'/','Estimation Algorithm'])
set_param([sys,'/','Estimation Algorithm'],'Location',[92,122,716,395])

add_block('built-in/Product',[sys,'/','Estimation Algorithm/Product1'])
set_param([sys,'/','Estimation Algorithm/Product1'],...
		'position',[225,117,255,148])

add_block('built-in/Mux',[sys,'/','Estimation Algorithm/Mux'])
set_param([sys,'/','Estimation Algorithm/Mux'],...
		'inputs','2',...
		'position',[490,66,520,99])

add_block('built-in/Gain',[sys,'/','Estimation Algorithm/Gain1'])
set_param([sys,'/','Estimation Algorithm/Gain1'],...
		'position',[300,118,330,152])

add_block('built-in/Integrator',[sys,'/','Estimation Algorithm/int_b'])
set_param([sys,'/','Estimation Algorithm/int_b'],...
		'Initial','2',...
		'position',[375,109,415,161])

add_block('built-in/Gain',[sys,'/','Estimation Algorithm/Gain'])
set_param([sys,'/','Estimation Algorithm/Gain'],...
		'Gain','-1',...
		'position',[300,36,330,64])

add_block('built-in/Integrator',[sys,'/','Estimation Algorithm/int_a'])
set_param([sys,'/','Estimation Algorithm/int_a'],...
		'Initial','0.5',...
		'position',[370,27,410,73])

add_block('built-in/Outport',[sys,'/','Estimation Algorithm/estimates'])
set_param([sys,'/','Estimation Algorithm/estimates'],...
		'position',[565,69,590,101])

add_block('built-in/Inport',[sys,'/','Estimation Algorithm/x_hat'])
set_param([sys,'/','Estimation Algorithm/x_hat'],...
		'Port','2',...
		'position',[50,78,80,112])

add_block('built-in/Inport',[sys,'/','Estimation Algorithm/u'])
set_param([sys,'/','Estimation Algorithm/u'],...
		'Port','3',...
		'position',[50,134,80,166])

add_block('built-in/Product',[sys,'/','Estimation Algorithm/Product'])
set_param([sys,'/','Estimation Algorithm/Product'],...
		'position',[230,33,260,62])

add_block('built-in/Inport',[sys,'/','Estimation Algorithm/epsilon'])
set_param([sys,'/','Estimation Algorithm/epsilon'],...
		'position',[50,24,80,56])
add_line([sys,'/','Estimation Algorithm'],[85,40;225,40])
add_line([sys,'/','Estimation Algorithm'],[85,40;200,40;200,125;220,125])
add_line([sys,'/','Estimation Algorithm'],[420,135;445,135;445,90;485,90])
add_line([sys,'/','Estimation Algorithm'],[265,50;295,50])
add_line([sys,'/','Estimation Algorithm'],[335,50;365,50])
add_line([sys,'/','Estimation Algorithm'],[335,135;370,135])
add_line([sys,'/','Estimation Algorithm'],[260,135;295,135])
add_line([sys,'/','Estimation Algorithm'],[85,95;130,95;130,55;225,55])
add_line([sys,'/','Estimation Algorithm'],[415,50;445,50;445,75;485,75])
add_line([sys,'/','Estimation Algorithm'],[525,85;560,85])
add_line([sys,'/','Estimation Algorithm'],[85,150;200,150;200,140;220,140])
set_param([sys,'/','Estimation Algorithm'],...
		'Mask Display','Estimator')


%     Finished composite block 'Estimation Algorithm'.

set_param([sys,'/','Estimation Algorithm'],...
		'Drop Shadow',4,...
		'position',[485,133,575,187])

add_block('built-in/Sum',[sys,'/','y'])
set_param([sys,'/','y'],...
		'position',[280,75,315,110])

add_block('built-in/Scope',[sys,'/','Estimates'])
set_param([sys,'/','Estimates'],...
		'Vgain','3.000000',...
		'Hgain','100.000000',...
		'Vmax','6.000000',...
		'Hmax','200.000000',...
		'Window',[880,608,1160,828],...
		'position',[710,85,760,145])

add_block('built-in/Transfer Fcn',[sys,'/','Plant Model'])
set_param([sys,'/','Plant Model'],...
		'Denominator','[1 2]',...
		'position',[145,133,220,177])

add_block('built-in/Signal Generator',[sys,'/','Signal Gen.'])
set_param([sys,'/','Signal Gen.'],...
		'Peak','1.000000',...
		'Peak Range','5.000000',...
		'Freq','5.000000',...
		'Freq Range','5.000000',...
		'Wave','Sin',...
		'Units','Rads',...
		'position',[10,141,90,189])


%     Subsystem  'Noise'.

new_system([sys,'/','Noise'])
set_param([sys,'/','Noise'],'Location',[25,265,360,455])

add_block('built-in/White Noise',[sys,'/','Noise/White Noise'])
set_param([sys,'/','Noise/White Noise'],...
		'position',[45,39,85,91])

add_block('built-in/Gain',[sys,'/','Noise/Gain'])
set_param([sys,'/','Noise/Gain'],...
		'Gain','0.1',...
		'position',[130,50,160,80])

add_block('built-in/Outport',[sys,'/','Noise/n_y'])
set_param([sys,'/','Noise/n_y'],...
		'position',[225,45,260,85])
add_line([sys,'/','Noise'],[90,65;125,65])
add_line([sys,'/','Noise'],[165,65;220,65])
set_param([sys,'/','Noise'],...
		'Mask Display','White noise')


%     Finished composite block 'Noise'.

set_param([sys,'/','Noise'],...
		'Drop Shadow',4,...
		'position',[10,54,110,116])

add_block('built-in/Sum',[sys,'/','epsilon'])
set_param([sys,'/','epsilon'],...
		'inputs','+-',...
		'position',[375,86,410,124])

add_block('built-in/Clock',[sys,'/','Clock'])
set_param([sys,'/','Clock'],...
		'orientation',1,...
		'position',[700,190,720,210])
add_line(sys,[780,275;820,275])
add_line(sys,[710,215;720,250])
add_line(sys,[95,165;120,165;120,265;280,265;280,180;480,180])
add_line(sys,[120,265;120,320;135,320])
add_line(sys,[120,265;120,155;140,155])
add_line(sys,[115,85;275,85])
add_line(sys,[225,155;255,155;255,100;275,100])
add_line(sys,[580,160;635,160;635,115;705,115])
add_line(sys,[600,160;600,420;125,421;135,350])
add_line(sys,[285,335;335,335;335,115;370,115])
add_line(sys,[335,165;400,165;400,160;480,160])
add_line(sys,[320,95;370,95])
add_line(sys,[415,105;450,105;450,140;480,140])
add_line(sys,[450,140;450,265;720,265])
add_line(sys,[600,345;610,345])
add_line(sys,[685,335;690,335;690,280;720,280])
add_line(sys,[685,355;700,355;700,295;720,295])

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
