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

add_block('built-in/Scope',[sys,'/','Time Delay'])
set_param([sys,'/','Time Delay'],...
		'Vgain','1.100000',...
		'Hgain','12.000000',...
		'Vmax','2.200000',...
		'Hmax','24.000000',...
		'Window',[0,0,313,197],...
		'position',[430,57,450,83])

add_block('built-in/Transport Delay',[sys,'/','Transport Delay'])
set_param([sys,'/','Transport Delay'],...
		'Delay Time','1',...
		'position',[215,58,250,102])

add_block('built-in/Signal Generator',[sys,'/','Signal Gen.'])
set_param([sys,'/','Signal Gen.'],...
		'Peak','1.000000',...
		'Peak Range','5.000000',...
		'Freq','0.500000',...
		'Freq Range','0.657895',...
		'Wave','Sin',...
		'Units','Rads',...
		'position',[40,63,85,97])

add_block('built-in/To Workspace',[sys,'/','To Workspace2'])
set_param([sys,'/','To Workspace2'],...
		'mat-name','dout',...
		'buffer','10000',...
		'position',[420,112,470,128])

add_block('built-in/Mux',[sys,'/','Mux'])
set_param([sys,'/','Mux'],...
		'hide name',0,...
		'inputs','2',...
		'position',[330,52,360,88])

add_block('built-in/Variable Transport Delay',[sys,'/',['Variable',13,'Transport Delay']])
set_param([sys,'/',['Variable',13,'Transport Delay']],...
		'Maximum delay:','3',...
		'position',[230,236,280,314])

add_block('built-in/Sum',[sys,'/',['Sine-wave',13,'plus constant']])
set_param([sys,'/',['Sine-wave',13,'plus constant']],...
		'hide name',0,...
		'position',[125,283,145,307])

add_block('built-in/Note',[sys,'/','Delay Time'])
set_param([sys,'/','Delay Time'],...
		'position',[185,280,190,285])

add_block('built-in/Note',[sys,'/','Input'])
set_param([sys,'/','Input'],...
		'position',[185,240,190,245])

add_block('built-in/Mux',[sys,'/','Mux1'])
set_param([sys,'/','Mux1'],...
		'hide name',0,...
		'inputs','2',...
		'position',[340,247,370,283])

add_block('built-in/Constant',[sys,'/','Constant'])
set_param([sys,'/','Constant'],...
		'position',[45,290,65,310])

add_block('built-in/To Workspace',[sys,'/','To Workspace'])
set_param([sys,'/','To Workspace'],...
		'mat-name','vdout',...
		'buffer','10000',...
		'position',[420,317,470,333])

add_block('built-in/Scope',[sys,'/','Variable Time Delay'])
set_param([sys,'/','Variable Time Delay'],...
		'Vgain','1.100000',...
		'Hgain','12.000000',...
		'Vmax','2.200000',...
		'Hmax','24.000000',...
		'Window',[0,0,316,195],...
		'position',[430,252,450,278])

add_block('built-in/Outport',[sys,'/','outport'])
set_param([sys,'/','outport'],...
		'position',[430,20,450,40])

add_block('built-in/Outport',[sys,'/','outport1'])
set_param([sys,'/','outport1'],...
		'Port','2',...
		'position',[430,145,450,165])

add_block('built-in/Outport',[sys,'/','outport2'])
set_param([sys,'/','outport2'],...
		'Port','3',...
		'position',[430,210,450,230])

add_block('built-in/Outport',[sys,'/','outport3'])
set_param([sys,'/','outport3'],...
		'Port','4',...
		'position',[435,355,455,375])
add_line(sys,[365,70;365,120;415,120])
add_line(sys,[375,265;375,325;415,325])
add_line(sys,[255,80;325,80])
add_line(sys,[90,80;210,80])
add_line(sys,[140,80;140,255;225,255])
add_line(sys,[70,300;120,300])
add_line(sys,[90,80;90,290;120,290])
add_line(sys,[150,295;225,295])
add_line(sys,[365,70;425,70])
add_line(sys,[285,275;335,275])
add_line(sys,[375,265;425,265])
add_line(sys,[140,80;140,30;425,30])
add_line(sys,[290,30;290,60;325,60])
add_line(sys,[140,220;305,220;305,255;335,255])
add_line(sys,[305,220;425,220])
add_line(sys,[305,275;305,365;430,365])
add_line(sys,[290,80;290,155;425,155])

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
