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

add_block('built-in/Note',[sys,'/',['Schema di controllo P.I.D. con azione derivativa dall''uscita']])
set_param([sys,'/',['Schema di controllo P.I.D. con azione derivativa dall''uscita']],...
		'position',[350,10,355,15])

add_block('built-in/Note',[sys,'/',['Effetto della scelta del ''polo lontano'' ',13,'nell''approssimazione dell''azione derivativa']])
set_param([sys,'/',['Effetto della scelta del ''polo lontano'' ',13,'nell''approssimazione dell''azione derivativa']],...
		'position',[325,35,330,40])


%     Subsystem  ['Rumore in ',13,'uscita'].

new_system([sys,'/',['Rumore in ',13,'uscita']])
set_param([sys,'/',['Rumore in ',13,'uscita']],'Location',[54,341,339,470])

add_block('built-in/Outport',[sys,'/',['Rumore in ',13,'uscita/Out_1']])
set_param([sys,'/',['Rumore in ',13,'uscita/Out_1']],...
		'position',[230,40,250,60])

add_block('built-in/Gain',[sys,'/',['Rumore in ',13,'uscita/Gain']])
set_param([sys,'/',['Rumore in ',13,'uscita/Gain']],...
		'Gain','[sqrt(Cov)]/[sqrt(Ts)]',...
		'position',[155,31,195,69])

add_block('built-in/Zero-Order Hold',[sys,'/',['Rumore in ',13,'uscita/Zero-Order',13,'Hold']])
set_param([sys,'/',['Rumore in ',13,'uscita/Zero-Order',13,'Hold']],...
		'Sample time','Ts',...
		'position',[85,34,120,66])

add_block('built-in/White Noise',[sys,'/',['Rumore in ',13,'uscita/White Noise']])
set_param([sys,'/',['Rumore in ',13,'uscita/White Noise']],...
		'Seed','seed',...
		'position',[25,40,45,60])
add_line([sys,'/',['Rumore in ',13,'uscita']],[50,50;80,50])
add_line([sys,'/',['Rumore in ',13,'uscita']],[200,50;225,50])
add_line([sys,'/',['Rumore in ',13,'uscita']],[125,50;150,50])
set_param([sys,'/',['Rumore in ',13,'uscita']],...
		'Mask Display','plot(t(:),r2(:))',...
		'Mask Type','Continuous White Noise.')
set_param([sys,'/',['Rumore in ',13,'uscita']],...
		'Mask Dialogue','White noise for continuous (s-domain) systems.\nBand-limited using zero-order-hold.|Noise Power:|Sample Time:|Seed')
set_param([sys,'/',['Rumore in ',13,'uscita']],...
		'Mask Translate','Cov = @1; Ts = @2; seed = @3; r = rand(1,12); r2 = [r(1),r;r,r(12)]; t =[1:13;1:13];')
set_param([sys,'/',['Rumore in ',13,'uscita']],...
		'Mask Help','Implemented using white noise into Zero-Order Hold block. The seed and power can be vectors of the same length to produce a vector of white noise sources. For faster simulation, set sample time to the highest value possible but in accordance with the fastest dynamics of system.')
set_param([sys,'/',['Rumore in ',13,'uscita']],...
		'Mask Entries','[0.01]\/0.05\/[23341]\/')


%     Finished composite block ['Rumore in ',13,'uscita'].

set_param([sys,'/',['Rumore in ',13,'uscita']],...
		'position',[380,197,425,233])


%     Subsystem  ['ingresso ',13,'di controllo'].

new_system([sys,'/',['ingresso ',13,'di controllo']])
set_param([sys,'/',['ingresso ',13,'di controllo']],'Location',[0,59,274,252])

add_block('built-in/Inport',[sys,'/',['ingresso ',13,'di controllo/x']])
set_param([sys,'/',['ingresso ',13,'di controllo/x']],...
		'position',[65,55,85,75])

add_block('built-in/S-Function',[sys,'/',['ingresso ',13,'di controllo/S-function',13,'M-file which plots',13,'lines',13,'']])
set_param([sys,'/',['ingresso ',13,'di controllo/S-function',13,'M-file which plots',13,'lines',13,'']],...
		'function name','sfunyst',...
		'parameters','ax, color, npts, dt',...
		'position',[130,55,180,75])
add_line([sys,'/',['ingresso ',13,'di controllo']],[90,65;125,65])
set_param([sys,'/',['ingresso ',13,'di controllo']],...
		'Mask Display','plot(0,0,100,100,[83,76,63,52,42,38,28,16,11,84,11,11,11,90,90,11],[75,58,47,54,72,80,84,74,65,65,65,90,40,40,90,90])',...
		'Mask Type','Storage scope.')
set_param([sys,'/',['ingresso ',13,'di controllo']],...
		'Mask Dialogue','Storage scope using MATLAB graph window.\nEnter plotting ranges and line type.|Initial Time Range:|Initial y-min:|Initial y-max:|Storage pts.:|Line type (rgbw-.:xo):')
set_param([sys,'/',['ingresso ',13,'di controllo']],...
		'Mask Translate','npts = @4; color = @5; ax = [0, @1, @2, @3]; dt=-1;')
set_param([sys,'/',['ingresso ',13,'di controllo']],...
		'Mask Help','This block uses a MATLAB figure window to plot the input signal.  The graph limits are automatically scaled to the min and max values of the signal stored in the scope''s signal buffer.  Line type must be in quotes.  See the M-file sfunyst.m.')
set_param([sys,'/',['ingresso ',13,'di controllo']],...
		'Mask Entries','5\/-1\/1\/200000\/''y-/g--/c-./w:/m*/ro/b+''\/')


%     Finished composite block ['ingresso ',13,'di controllo'].

set_param([sys,'/',['ingresso ',13,'di controllo']],...
		'position',[790,510,820,550])

add_block('built-in/Mux',[sys,'/','Mux1'])
set_param([sys,'/','Mux1'],...
		'hide name',0,...
		'inputs','2',...
		'position',[735,376,765,409])


%     Subsystem  ['uscita',13,'controllata'].

new_system([sys,'/',['uscita',13,'controllata']])
set_param([sys,'/',['uscita',13,'controllata']],'Location',[0,59,274,252])

add_block('built-in/Inport',[sys,'/',['uscita',13,'controllata/x']])
set_param([sys,'/',['uscita',13,'controllata/x']],...
		'position',[65,55,85,75])

add_block('built-in/S-Function',[sys,'/',['uscita',13,'controllata/S-function',13,'M-file which plots',13,'lines',13,'']])
set_param([sys,'/',['uscita',13,'controllata/S-function',13,'M-file which plots',13,'lines',13,'']],...
		'function name','sfunyst',...
		'parameters','ax, color, npts, dt',...
		'position',[130,55,180,75])
add_line([sys,'/',['uscita',13,'controllata']],[90,65;125,65])
set_param([sys,'/',['uscita',13,'controllata']],...
		'Mask Display','plot(0,0,100,100,[83,76,63,52,42,38,28,16,11,84,11,11,11,90,90,11],[75,58,47,54,72,80,84,74,65,65,65,90,40,40,90,90])',...
		'Mask Type','Storage scope.')
set_param([sys,'/',['uscita',13,'controllata']],...
		'Mask Dialogue','Storage scope using MATLAB graph window.\nEnter plotting ranges and line type.|Initial Time Range:|Initial y-min:|Initial y-max:|Storage pts.:|Line type (rgbw-.:xo):')
set_param([sys,'/',['uscita',13,'controllata']],...
		'Mask Translate','npts = @4; color = @5; ax = [0, @1, @2, @3]; dt=-1;')
set_param([sys,'/',['uscita',13,'controllata']],...
		'Mask Help','This block uses a MATLAB figure window to plot the input signal.  The graph limits are automatically scaled to the min and max values of the signal stored in the scope''s signal buffer.  Line type must be in quotes.  See the M-file sfunyst.m.')
set_param([sys,'/',['uscita',13,'controllata']],...
		'Mask Entries','5\/-1\/1\/200000\/''y-/g--/c-./w:/m*/ro/b+''\/')


%     Finished composite block ['uscita',13,'controllata'].

set_param([sys,'/',['uscita',13,'controllata']],...
		'position',[795,375,825,415])

add_block('built-in/Sum',[sys,'/','Sum5'])
set_param([sys,'/','Sum5'],...
		'hide name',0,...
		'position',[615,574,635,621])

add_block('built-in/Sum',[sys,'/','Sum4'])
set_param([sys,'/','Sum4'],...
		'hide name',0,...
		'position',[615,359,635,406])

add_block('built-in/Gain',[sys,'/','i'])
set_param([sys,'/','i'],...
		'hide name',0,...
		'Gain','0.01',...
		'position',[460,200,580,230])

add_block('built-in/Mux',[sys,'/','Mux'])
set_param([sys,'/','Mux'],...
		'hide name',0,...
		'inputs','2',...
		'position',[715,511,745,544])

add_block('built-in/Zero-Pole',[sys,'/','P(s) '])
set_param([sys,'/','P(s) '],...
		'Zeros','[]',...
		'Poles','[-1;-1; -1]',...
		'position',[450,583,565,637])

add_block('built-in/Sum',[sys,'/','Sum2'])
set_param([sys,'/','Sum2'],...
		'hide name',0,...
		'inputs','++-',...
		'position',[340,516,365,704])

add_block('built-in/Zero-Pole',[sys,'/','R_I(s) '])
set_param([sys,'/','R_I(s) '],...
		'Zeros','[]',...
		'Poles','[0]',...
		'position',[255,592,300,628])

add_block('built-in/Gain',[sys,'/','R_P(s) '])
set_param([sys,'/','R_P(s) '],...
		'Gain','2',...
		'position',[260,538,295,562])

add_block('built-in/Sum',[sys,'/','Sum3'])
set_param([sys,'/','Sum3'],...
		'hide name',0,...
		'inputs','+-',...
		'position',[175,584,195,631])

add_block('built-in/Transfer Fcn',[sys,'/','R_D(s) '])
set_param([sys,'/','R_D(s) '],...
		'Numerator','[1, 0]',...
		'Denominator','[0.01 1]',...
		'position',[250,650,315,690])

add_block('built-in/Transfer Fcn',[sys,'/','R_D(s)'])
set_param([sys,'/','R_D(s)'],...
		'Numerator','[1, 0]',...
		'Denominator','[0.1 1]',...
		'position',[250,435,315,475])

add_block('built-in/Zero-Pole',[sys,'/','R_I(s)'])
set_param([sys,'/','R_I(s)'],...
		'Zeros','[]',...
		'Poles','[0]',...
		'position',[255,377,300,413])

add_block('built-in/Gain',[sys,'/','R_P(s)'])
set_param([sys,'/','R_P(s)'],...
		'Gain','2',...
		'position',[260,323,295,347])

add_block('built-in/Sum',[sys,'/','Sum'])
set_param([sys,'/','Sum'],...
		'hide name',0,...
		'inputs','++-',...
		'position',[340,301,365,489])

add_block('built-in/Zero-Pole',[sys,'/','P(s)'])
set_param([sys,'/','P(s)'],...
		'Zeros','[]',...
		'Poles','[-1;-1; -1]',...
		'position',[450,368,565,422])

add_block('built-in/Step Fcn',[sys,'/','Step Input'])
set_param([sys,'/','Step Input'],...
		'position',[55,370,75,390])

add_block('built-in/Sum',[sys,'/','Sum1'])
set_param([sys,'/','Sum1'],...
		'hide name',0,...
		'inputs','+-',...
		'position',[175,369,195,416])

add_block('built-in/Note',[sys,'/',['Notare come piu'' il polo e'' lontano piu'' il disturbo in uscita influenza',13,'l''andamento dell''ingresso di controllo, a parita'' di prestazioni']])
set_param([sys,'/',['Notare come piu'' il polo e'' lontano piu'' il disturbo in uscita influenza',13,'l''andamento dell''ingresso di controllo, a parita'' di prestazioni']],...
		'position',[375,95,380,100])
add_line(sys,[370,395;445,395])
add_line(sys,[305,395;335,395])
add_line(sys,[300,335;335,335])
add_line(sys,[320,455;335,455])
add_line(sys,[200,395;250,395])
add_line(sys,[200,395;225,395;225,335;255,335])
add_line(sys,[80,380;170,380])
add_line(sys,[370,610;445,610])
add_line(sys,[305,610;335,610])
add_line(sys,[300,550;335,550])
add_line(sys,[320,670;335,670])
add_line(sys,[200,610;250,610])
add_line(sys,[225,610;225,550;255,550])
add_line(sys,[115,380;115,595;170,595])
add_line(sys,[750,530;785,530])
add_line(sys,[405,395;405,305;700,305;710,520])
add_line(sys,[405,610;405,535;710,535])
add_line(sys,[770,395;790,395])
add_line(sys,[570,395;610,395])
add_line(sys,[640,385;730,385])
add_line(sys,[430,215;455,215])
add_line(sys,[585,215;590,215;590,370;610,370])
add_line(sys,[640,385;640,500;155,500;155,405;170,405])
add_line(sys,[570,610;610,610])
add_line(sys,[590,320;590,585;610,585])
add_line(sys,[640,600;670,600;670,400;730,400])
add_line(sys,[655,600;655,720;145,720;145,620;170,620])
add_line(sys,[225,720;225,670;245,670])
add_line(sys,[155,455;245,455])

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
