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

add_block('built-in/Note',[sys,'/','Blocks for use with linmod:'])
set_param([sys,'/','Blocks for use with linmod:'],...
		'position',[85,35,90,40])

add_block('built-in/Note',[sys,'/','Requires control toolbox:'])
set_param([sys,'/','Requires control toolbox:'],...
		'position',[85,170,90,175])


%     Subsystem  ['Switched ',13,'transport delay',13,'for linearization'].

new_system([sys,'/',['Switched ',13,'transport delay',13,'for linearization']])
set_param([sys,'/',['Switched ',13,'transport delay',13,'for linearization']],'Location',[421,74,910,339])

add_block('built-in/Gain',[sys,'/',['Switched ',13,'transport delay',13,'for linearization/~ swtich']])
set_param([sys,'/',['Switched ',13,'transport delay',13,'for linearization/~ swtich']],...
		'Gain','~switch',...
		'position',[145,162,170,188])

add_block('built-in/Gain',[sys,'/',['Switched ',13,'transport delay',13,'for linearization/Switch']])
set_param([sys,'/',['Switched ',13,'transport delay',13,'for linearization/Switch']],...
		'Gain','switch',...
		'position',[145,77,170,103])

add_block('built-in/Inport',[sys,'/',['Switched ',13,'transport delay',13,'for linearization/in_1']])
set_param([sys,'/',['Switched ',13,'transport delay',13,'for linearization/in_1']],...
		'position',[25,120,45,140])

add_block('built-in/Outport',[sys,'/',['Switched ',13,'transport delay',13,'for linearization/out_1']])
set_param([sys,'/',['Switched ',13,'transport delay',13,'for linearization/out_1']],...
		'position',[415,120,435,140])

add_block('built-in/Transport Delay',[sys,'/',['Switched ',13,'transport delay',13,'for linearization/Transport',13,'delay']])
set_param([sys,'/',['Switched ',13,'transport delay',13,'for linearization/Transport',13,'delay']],...
		'Delay Time','delay',...
		'Initial Input','u0',...
		'Buffer size:','Npts',...
		'position',[250,160,295,190])

add_block('built-in/Transfer Fcn',[sys,'/',['Switched ',13,'transport delay',13,'for linearization/Pade',13,'approximation']])
set_param([sys,'/',['Switched ',13,'transport delay',13,'for linearization/Pade',13,'approximation']],...
		'Numerator','num ',...
		'Denominator','den',...
		'position',[245,69,295,111])

add_block('built-in/Sum',[sys,'/',['Switched ',13,'transport delay',13,'for linearization/Sum']])
set_param([sys,'/',['Switched ',13,'transport delay',13,'for linearization/Sum']],...
		'position',[365,120,385,140])
add_line([sys,'/',['Switched ',13,'transport delay',13,'for linearization']],[50,130;90,130;90,175;140,175])
add_line([sys,'/',['Switched ',13,'transport delay',13,'for linearization']],[175,175;245,175])
add_line([sys,'/',['Switched ',13,'transport delay',13,'for linearization']],[175,90;240,90])
add_line([sys,'/',['Switched ',13,'transport delay',13,'for linearization']],[50,130;90,130;90,90;140,90])
add_line([sys,'/',['Switched ',13,'transport delay',13,'for linearization']],[390,130;410,130])
add_line([sys,'/',['Switched ',13,'transport delay',13,'for linearization']],[300,90;325,90;325,125;360,125])
add_line([sys,'/',['Switched ',13,'transport delay',13,'for linearization']],[300,175;325,175;325,135;360,135])
set_param([sys,'/',['Switched ',13,'transport delay',13,'for linearization']],...
		'Mask Display','Transport\ndelay',...
		'Mask Type','Delay for linearization.')
set_param([sys,'/',['Switched ',13,'transport delay',13,'for linearization']],...
		'Mask Dialogue','Switched tranpsort delay with pade\napproximation for linearization.|Time delay:|Initial input:|Switch (1 for linearization, 0 for simulation):|Pade order (for linearization):|Code generation storage buffer size:')
set_param([sys,'/',['Switched ',13,'transport delay',13,'for linearization']],...
		'Mask Translate','delay = @1; u0 = @2; switch = @3; [num, den] = pade(delay, @4); Npts = @5;')
set_param([sys,'/',['Switched ',13,'transport delay',13,'for linearization']],...
		'Mask Help','This block should be used in place of the transport delay when you are linearizing the model. It uses a pade approximation duirng linearization using the pade command in the Control System Toolbox.')
set_param([sys,'/',['Switched ',13,'transport delay',13,'for linearization']],...
		'Mask Entries','1\/0\/0\/2\/1024\/')


%     Finished composite block ['Switched ',13,'transport delay',13,'for linearization'].

set_param([sys,'/',['Switched ',13,'transport delay',13,'for linearization']],...
		'position',[80,203,160,257])


%     Subsystem  ['Switched ',13,'derivative for',13,'linearization'].

new_system([sys,'/',['Switched ',13,'derivative for',13,'linearization']])
set_param([sys,'/',['Switched ',13,'derivative for',13,'linearization']],'Location',[716,443,1205,708])

add_block('built-in/Derivative',[sys,'/',['Switched ',13,'derivative for',13,'linearization/Derivative']])
set_param([sys,'/',['Switched ',13,'derivative for',13,'linearization/Derivative']],...
		'position',[210,165,240,185])

add_block('built-in/Transfer Fcn',[sys,'/',['Switched ',13,'derivative for',13,'linearization/Transfer function',13,'approximation']])
set_param([sys,'/',['Switched ',13,'derivative for',13,'linearization/Transfer function',13,'approximation']],...
		'Numerator','num ',...
		'Denominator','den',...
		'position',[205,69,255,111])

add_block('built-in/Gain',[sys,'/',['Switched ',13,'derivative for',13,'linearization/Switch']])
set_param([sys,'/',['Switched ',13,'derivative for',13,'linearization/Switch']],...
		'Gain','switch',...
		'position',[115,77,140,103])

add_block('built-in/Gain',[sys,'/',['Switched ',13,'derivative for',13,'linearization/~ switch']])
set_param([sys,'/',['Switched ',13,'derivative for',13,'linearization/~ switch']],...
		'Gain','~switch',...
		'position',[115,162,140,188])

add_block('built-in/Inport',[sys,'/',['Switched ',13,'derivative for',13,'linearization/in_1']])
set_param([sys,'/',['Switched ',13,'derivative for',13,'linearization/in_1']],...
		'position',[25,120,45,140])

add_block('built-in/Sum',[sys,'/',['Switched ',13,'derivative for',13,'linearization/Sum']])
set_param([sys,'/',['Switched ',13,'derivative for',13,'linearization/Sum']],...
		'position',[365,120,385,140])

add_block('built-in/Outport',[sys,'/',['Switched ',13,'derivative for',13,'linearization/out_1']])
set_param([sys,'/',['Switched ',13,'derivative for',13,'linearization/out_1']],...
		'position',[415,120,435,140])
add_line([sys,'/',['Switched ',13,'derivative for',13,'linearization']],[50,130;75,130;75,175;110,175])
add_line([sys,'/',['Switched ',13,'derivative for',13,'linearization']],[50,130;75,130;75,90;110,90])
add_line([sys,'/',['Switched ',13,'derivative for',13,'linearization']],[145,90;200,90])
add_line([sys,'/',['Switched ',13,'derivative for',13,'linearization']],[145,175;205,175])
add_line([sys,'/',['Switched ',13,'derivative for',13,'linearization']],[245,175;325,175;325,135;360,135])
add_line([sys,'/',['Switched ',13,'derivative for',13,'linearization']],[260,90;325,90;325,125;360,125])
add_line([sys,'/',['Switched ',13,'derivative for',13,'linearization']],[390,130;410,130])
set_param([sys,'/',['Switched ',13,'derivative for',13,'linearization']],...
		'Mask Display','du/dt',...
		'Mask Type','Derivative for linearization')
set_param([sys,'/',['Switched ',13,'derivative for',13,'linearization']],...
		'Mask Dialogue','Switched derivative with proper transfer function approximation for linearization.|Switch value (1 for linearization, 0 for simulation):|Derivative constant  Ns/(s+N):')
set_param([sys,'/',['Switched ',13,'derivative for',13,'linearization']],...
		'Mask Translate','switch = @1; num = [@2,0]; den = [1, @2];')
set_param([sys,'/',['Switched ',13,'derivative for',13,'linearization']],...
		'Mask Help','This block should be used in place of the derivative when you are linearizing the model. It approximates the derivative with a proper transfer function.')
set_param([sys,'/',['Switched ',13,'derivative for',13,'linearization']],...
		'Mask Entries','0\/100\/')


%     Finished composite block ['Switched ',13,'derivative for',13,'linearization'].

set_param([sys,'/',['Switched ',13,'derivative for',13,'linearization']],...
		'position',[80,73,155,127])

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
