function [ret,x0,str,ts,xts]=extrpid(t,x,u,flag);
%EXTRPID	is the M-file description of the SIMULINK system named EXTRPID.
%	The block-diagram can be displayed by typing: EXTRPID.
%
%	SYS=EXTRPID(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 EXTRPID 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 EXTRPID with a FLAG of zero:
%	[SIZES]=EXTRPID([],[],[],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',[317,555,479,814])
     open_system(sys)
end;
set_param(sys,'algorithm',     'RK-45')
set_param(sys,'Start time',    '0')
set_param(sys,'Stop time',     '10')
set_param(sys,'Min step size', '0.001')
set_param(sys,'Max step size', '0.01')
set_param(sys,'Relative error','0.001')
set_param(sys,'Return vars',   '')


%     Subsystem  ['PID with',13,'Approximate',13,'Derivative'].

new_system([sys,'/',['PID with',13,'Approximate',13,'Derivative']])
set_param([sys,'/',['PID with',13,'Approximate',13,'Derivative']],'Location',[14,59,339,276])

add_block('built-in/Sum',[sys,'/',['PID with',13,'Approximate',13,'Derivative/Sum']])
set_param([sys,'/',['PID with',13,'Approximate',13,'Derivative/Sum']],...
		'inputs','+++',...
		'position',[245,57,265,93])

add_block('built-in/Gain',[sys,'/',['PID with',13,'Approximate',13,'Derivative/Proportional']])
set_param([sys,'/',['PID with',13,'Approximate',13,'Derivative/Proportional']],...
		'Gain','P',...
		'position',[120,13,140,37])

add_block('built-in/Transfer Fcn',[sys,'/',['PID with',13,'Approximate',13,'Derivative/Integral']])
set_param([sys,'/',['PID with',13,'Approximate',13,'Derivative/Integral']],...
		'Numerator','[I]',...
		'Denominator','[1 0]',...
		'position',[110,57,145,93])

add_block('built-in/Outport',[sys,'/',['PID with',13,'Approximate',13,'Derivative/Out_1']])
set_param([sys,'/',['PID with',13,'Approximate',13,'Derivative/Out_1']],...
		'position',[290,65,310,85])

add_block('built-in/Inport',[sys,'/',['PID with',13,'Approximate',13,'Derivative/In_1']])
set_param([sys,'/',['PID with',13,'Approximate',13,'Derivative/In_1']],...
		'position',[25,65,45,85])

add_block('built-in/Transfer Fcn',[sys,'/',['PID with',13,'Approximate',13,'Derivative/Derivative']])
set_param([sys,'/',['PID with',13,'Approximate',13,'Derivative/Derivative']],...
		'Numerator','[D, 0]',...
		'Denominator','[1/N 1]',...
		'position',[110,120,160,160])
add_line([sys,'/',['PID with',13,'Approximate',13,'Derivative']],[270,75;285,75])
add_line([sys,'/',['PID with',13,'Approximate',13,'Derivative']],[50,75;105,75])
add_line([sys,'/',['PID with',13,'Approximate',13,'Derivative']],[65,75;65,140;105,140])
add_line([sys,'/',['PID with',13,'Approximate',13,'Derivative']],[80,75;80,25;115,25])
add_line([sys,'/',['PID with',13,'Approximate',13,'Derivative']],[165,140;215,140;215,85;240,85])
add_line([sys,'/',['PID with',13,'Approximate',13,'Derivative']],[150,75;240,75])
add_line([sys,'/',['PID with',13,'Approximate',13,'Derivative']],[145,25;210,25;210,65;240,65])
set_param([sys,'/',['PID with',13,'Approximate',13,'Derivative']],...
		'Mask Display','PID',...
		'Mask Type','PID(2) Controller')
set_param([sys,'/',['PID with',13,'Approximate',13,'Derivative']],...
		'Mask Dialogue','Enter Proportional(P), Integral(I), and Derivative(D) and divisor (N) terms.\nP+I/s+Ds/(1/Ns+1)|Proportional:|Integral|Derivative:|Derivative divisor(N):')
set_param([sys,'/',['PID with',13,'Approximate',13,'Derivative']],...
		'Mask Translate','P=@1; I=@2; D=@3; N=@4;')
set_param([sys,'/',['PID with',13,'Approximate',13,'Derivative']],...
		'Mask Help','This block implements a PID controller where parameters are entered for the Proportional, Integral and Derivative and divisor terms. Unmask this block to see how it works. The derivative term is implemented using an:\ns/(s/N +1)  transfer function block. Making N large can create a "stiff" system.')
set_param([sys,'/',['PID with',13,'Approximate',13,'Derivative']],...
		'Mask Entries','1\/1\/0\/100\/')


%     Finished composite block ['PID with',13,'Approximate',13,'Derivative'].

set_param([sys,'/',['PID with',13,'Approximate',13,'Derivative']],...
		'position',[55,104,95,136])


%     Subsystem  ['Set point PID ',13,'with Anti-Windup'].

new_system([sys,'/',['Set point PID ',13,'with Anti-Windup']])
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup']],'Location',[102,202,901,679])

add_block('built-in/Inport',[sys,'/',['Set point PID ',13,'with Anti-Windup/System Output']])
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup/System Output']],...
		'Port','2',...
		'position',[70,225,90,245])

add_block('built-in/Gain',[sys,'/',['Set point PID ',13,'with Anti-Windup/Anti Windup Gain']])
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup/Anti Windup Gain']],...
		'Gain','1/Tt',...
		'position',[260,114,310,146])

add_block('built-in/Sum',[sys,'/',['Set point PID ',13,'with Anti-Windup/Sum3']])
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup/Sum3']],...
		'hide name',0,...
		'inputs','+-',...
		'position',[215,112,235,148])

add_block('built-in/Outport',[sys,'/',['Set point PID ',13,'with Anti-Windup/Input to system']])
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup/Input to system']],...
		'position',[710,335,730,355])

add_block('built-in/Saturation',[sys,'/',['Set point PID ',13,'with Anti-Windup/Saturation']])
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup/Saturation']],...
		'Lower Limit','ulow',...
		'Upper Limit','uhigh',...
		'position',[155,130,180,150])

add_block('built-in/Inport',[sys,'/',['Set point PID ',13,'with Anti-Windup/Set point']])
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup/Set point']],...
		'position',[70,175,90,195])

add_block('built-in/Gain',[sys,'/',['Set point PID ',13,'with Anti-Windup/set point weighting']])
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup/set point weighting']],...
		'Gain','b',...
		'position',[275,230,320,260])

add_block('built-in/Note',[sys,'/',['Set point PID ',13,'with Anti-Windup/ysp']])
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup/ysp']],...
		'position',[205,171,210,176])

add_block('built-in/Sum',[sys,'/',['Set point PID ',13,'with Anti-Windup/Sum']])
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup/Sum']],...
		'inputs','+-',...
		'position',[415,305,435,325])

add_block('built-in/Sum',[sys,'/',['Set point PID ',13,'with Anti-Windup/error']])
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup/error']],...
		'inputs','-+-',...
		'position',[365,167,385,203])

add_block('built-in/Note',[sys,'/',['Set point PID ',13,'with Anti-Windup/y']])
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup/y']],...
		'position',[145,211,150,216])

add_block('built-in/Transfer Fcn',[sys,'/',['Set point PID ',13,'with Anti-Windup/Derivative']])
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup/Derivative']],...
		'Numerator','[-Td, 0]',...
		'Denominator','[Td/N 1]',...
		'position',[355,377,450,423])

add_block('built-in/Sum',[sys,'/',['Set point PID ',13,'with Anti-Windup/P+I+D']])
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup/P+I+D']],...
		'inputs','+++',...
		'position',[550,327,570,363])

add_block('built-in/Gain',[sys,'/',['Set point PID ',13,'with Anti-Windup/Proportional']])
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup/Proportional']],...
		'Gain','K',...
		'position',[600,329,650,361])

add_block('built-in/Transfer Fcn',[sys,'/',['Set point PID ',13,'with Anti-Windup/Integrator']])
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup/Integrator']],...
		'Denominator','[Ti 0]',...
		'position',[425,168,485,202])

add_block('built-in/Note',[sys,'/',['Set point PID ',13,'with Anti-Windup/PID controller with set point weighting and anti-windup.']])
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup/PID controller with set point weighting and anti-windup.']],...
		'position',[410,41,415,46])
add_line([sys,'/',['Set point PID ',13,'with Anti-Windup']],[325,245;370,245;370,310;410,310])
add_line([sys,'/',['Set point PID ',13,'with Anti-Windup']],[655,345;705,345])
add_line([sys,'/',['Set point PID ',13,'with Anti-Windup']],[665,345;665,91;125,91;125,140;150,140])
add_line([sys,'/',['Set point PID ',13,'with Anti-Windup']],[125,120;210,120])
add_line([sys,'/',['Set point PID ',13,'with Anti-Windup']],[95,235;250,235;250,195;360,195])
add_line([sys,'/',['Set point PID ',13,'with Anti-Windup']],[140,235;140,320;410,320])
add_line([sys,'/',['Set point PID ',13,'with Anti-Windup']],[140,320;140,400;350,400])
add_line([sys,'/',['Set point PID ',13,'with Anti-Windup']],[315,130;335,130;335,175;360,175])
add_line([sys,'/',['Set point PID ',13,'with Anti-Windup']],[240,130;255,130])
add_line([sys,'/',['Set point PID ',13,'with Anti-Windup']],[185,140;210,140])
add_line([sys,'/',['Set point PID ',13,'with Anti-Windup']],[390,185;420,185])
add_line([sys,'/',['Set point PID ',13,'with Anti-Windup']],[95,185;360,185])
add_line([sys,'/',['Set point PID ',13,'with Anti-Windup']],[180,185;180,245;270,245])
add_line([sys,'/',['Set point PID ',13,'with Anti-Windup']],[440,315;485,315;485,345;545,345])
add_line([sys,'/',['Set point PID ',13,'with Anti-Windup']],[455,400;485,400;485,355;545,355])
add_line([sys,'/',['Set point PID ',13,'with Anti-Windup']],[575,345;595,345])
add_line([sys,'/',['Set point PID ',13,'with Anti-Windup']],[490,185;520,185;520,335;545,335])
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup']],...
		'Mask Display','plot(-3,-0.3,4,1.3,[0,1,2,3],[0,0,1,1]);   PID     ',...
		'Mask Type','Anti windup PID')
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup']],...
		'Mask Dialogue','Setpoint PID with anti-windup: e=(ysp-y)\nP=K*(b*ysp-y)  D=(KTds/(1+Td/Ns)\ndI/dt=(K/Ti)*e+1/Tt(v-u) v=sat(P+I+D)|Gain K|Setpoint b|Integral Ti|Antiwindup gain Tt|Antiwindup saturation [low,high]|Derivative gain and divisor [Td,N]')
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup']],...
		'Mask Translate','K=@1 ; b =@2 ; Ti=@3 ; Tt=@4 ; ulow=@5(1); uhigh=@5(2); Td=@6(1); N=@6(2);')
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup']],...
		'Mask Help','Unmask this block to see how it works. Attach the set point to input 1, the system output to input 2, and the output of the block to the system''s input.')
set_param([sys,'/',['Set point PID ',13,'with Anti-Windup']],...
		'Mask Entries','1\/1\/1\/1\/[-1,1]\/[1,1000]\/')


%     Finished composite block ['Set point PID ',13,'with Anti-Windup'].

set_param([sys,'/',['Set point PID ',13,'with Anti-Windup']],...
		'position',[55,192,100,228])


%     Subsystem  'PID Controller'.

new_system([sys,'/','PID Controller'])
set_param([sys,'/','PID Controller'],'Location',[0,0,362,244])

add_block('built-in/Inport',[sys,'/','PID Controller/In_1'])
set_param([sys,'/','PID Controller/In_1'],...
		'position',[25,65,45,85])

add_block('built-in/Outport',[sys,'/','PID Controller/Out_1'])
set_param([sys,'/','PID Controller/Out_1'],...
		'position',[290,65,310,85])

add_block('built-in/Derivative',[sys,'/','PID Controller/Derivative'])
set_param([sys,'/','PID Controller/Derivative'],...
		'position',[150,128,190,152])

add_block('built-in/Transfer Fcn',[sys,'/','PID Controller/Integral'])
set_param([sys,'/','PID Controller/Integral'],...
		'Numerator','[I]',...
		'Denominator','[1 0]',...
		'position',[110,57,145,93])

add_block('built-in/Gain',[sys,'/','PID Controller/Proportional'])
set_param([sys,'/','PID Controller/Proportional'],...
		'Gain','P',...
		'position',[120,13,140,37])

add_block('built-in/Gain',[sys,'/','PID Controller/D'])
set_param([sys,'/','PID Controller/D'],...
		'Gain','D',...
		'position',[95,129,115,151])

add_block('built-in/Sum',[sys,'/','PID Controller/Sum'])
set_param([sys,'/','PID Controller/Sum'],...
		'inputs','+++',...
		'position',[245,57,265,93])
add_line([sys,'/','PID Controller'],[145,25;210,25;210,65;240,65])
add_line([sys,'/','PID Controller'],[150,75;240,75])
add_line([sys,'/','PID Controller'],[195,140;215,140;215,85;240,85])
add_line([sys,'/','PID Controller'],[50,75;105,75])
add_line([sys,'/','PID Controller'],[80,75;80,25;115,25])
add_line([sys,'/','PID Controller'],[65,75;65,140;90,140])
add_line([sys,'/','PID Controller'],[270,75;285,75])
add_line([sys,'/','PID Controller'],[120,140;145,140])
set_param([sys,'/','PID Controller'],...
		'Mask Display','PID',...
		'Mask Type','PID Controller',...
		'Mask Dialogue','Enter expressions for proportional, integral, and derivative terms.\nP+I/s+Ds|Proportional:|Integral|Derivative:')
set_param([sys,'/','PID Controller'],...
		'Mask Translate','P=@1; I=@2; D=@3;')
set_param([sys,'/','PID Controller'],...
		'Mask Help','This block implements a PID controller where parameters are entered for the Proportional, Integral and Derivative terms. Unmask this block to see how it works. The derivative term is implemented using a true derivative block.')
set_param([sys,'/','PID Controller'],...
		'Mask Entries','1\/1\/0\/')


%     Finished composite block 'PID Controller'.

set_param([sys,'/','PID Controller'],...
		'position',[55,44,95,76])

add_block('built-in/Note',[sys,'/','PID Controllers'])
set_param([sys,'/','PID Controllers'],...
		'position',[70,10,75,15])

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
