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

add_block('built-in/Sum',[sys,'/','Sum1'])
set_param([sys,'/','Sum1'],...
		'hide name',0,...
		'inputs','+-',...
		'position',[150,234,170,281])

add_block('built-in/Step Fcn',[sys,'/','Step Input'])
set_param([sys,'/','Step Input'],...
		'position',[30,235,50,255])

add_block('built-in/Zero-Pole',[sys,'/','P(s)'])
set_param([sys,'/','P(s)'],...
		'Zeros','[]',...
		'Poles','[-1;-1; -1]',...
		'position',[425,233,540,287])

add_block('built-in/Sum',[sys,'/','Sum'])
set_param([sys,'/','Sum'],...
		'hide name',0,...
		'inputs','+++',...
		'position',[315,166,340,354])

add_block('built-in/Gain',[sys,'/','R_P(s)'])
set_param([sys,'/','R_P(s)'],...
		'Gain','2',...
		'position',[235,188,270,212])

add_block('built-in/Zero-Pole',[sys,'/','R_I(s)'])
set_param([sys,'/','R_I(s)'],...
		'Zeros','[]',...
		'Poles','[0]',...
		'position',[230,242,275,278])

add_block('built-in/Transfer Fcn',[sys,'/','R_D(s)'])
set_param([sys,'/','R_D(s)'],...
		'Numerator','[1, 0]',...
		'Denominator','[0.1 1]',...
		'position',[225,300,290,340])

add_block('built-in/Transfer Fcn',[sys,'/','R_D(s) '])
set_param([sys,'/','R_D(s) '],...
		'Numerator','[1, 0]',...
		'Denominator','[0.1 1]',...
		'position',[225,515,290,555])

add_block('built-in/Sum',[sys,'/','Sum3'])
set_param([sys,'/','Sum3'],...
		'hide name',0,...
		'inputs','+-',...
		'position',[150,449,170,496])

add_block('built-in/Gain',[sys,'/','R_P(s) '])
set_param([sys,'/','R_P(s) '],...
		'Gain','2',...
		'position',[235,403,270,427])

add_block('built-in/Zero-Pole',[sys,'/','R_I(s) '])
set_param([sys,'/','R_I(s) '],...
		'Zeros','[]',...
		'Poles','[0]',...
		'position',[230,457,275,493])

add_block('built-in/Sum',[sys,'/','Sum2'])
set_param([sys,'/','Sum2'],...
		'hide name',0,...
		'inputs','++-',...
		'position',[315,381,340,569])

add_block('built-in/Zero-Pole',[sys,'/','P(s) '])
set_param([sys,'/','P(s) '],...
		'Zeros','[]',...
		'Poles','[-1;-1; -1]',...
		'position',[425,448,540,502])


%     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',[745,375,775,415])

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

add_block('built-in/Mux',[sys,'/','Mux1'])
set_param([sys,'/','Mux1'],...
		'hide name',0,...
		'inputs','2',...
		'position',[690,251,720,284])


%     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',[745,250,775,290])

add_block('built-in/Note',[sys,'/',['Schema P.I.D.',13,'con schema a derivazione dell''errore']])
set_param([sys,'/',['Schema P.I.D.',13,'con schema a derivazione dell''errore']],...
		'position',[335,115,340,120])

add_block('built-in/Note',[sys,'/',['Schema P.I.D.',13,'con schema a derivazione dell''uscita']])
set_param([sys,'/',['Schema P.I.D.',13,'con schema a derivazione dell''uscita']],...
		'position',[320,605,325,610])

add_block('built-in/Note',[sys,'/',['Esempio di controllo P.I.D. tramite due schemi alternativi:',13,'- parte derivativa con derivazione dell''errore',13,'- parte derivativa con derivazione dell''uscita',13,'']])
set_param([sys,'/',['Esempio di controllo P.I.D. tramite due schemi alternativi:',13,'- parte derivativa con derivazione dell''errore',13,'- parte derivativa con derivazione dell''uscita',13,'']],...
		'position',[220,20,225,25])

add_block('built-in/Note',[sys,'/',['Notare la notevole differenza nell''andamento',13,'dell''ingresso di controllo a parita'' di',13,'prestazioni in termini di variabile controllata']])
set_param([sys,'/',['Notare la notevole differenza nell''andamento',13,'dell''ingresso di controllo a parita'' di',13,'prestazioni in termini di variabile controllata']],...
		'position',[640,20,645,25])
add_line(sys,[345,260;420,260])
add_line(sys,[280,260;310,260])
add_line(sys,[275,200;310,200])
add_line(sys,[295,320;310,320])
add_line(sys,[175,260;225,260])
add_line(sys,[175,260;200,260;200,200;230,200])
add_line(sys,[175,260;200,260;200,320;220,320])
add_line(sys,[55,245;145,245])
add_line(sys,[345,475;420,475])
add_line(sys,[280,475;310,475])
add_line(sys,[275,415;310,415])
add_line(sys,[295,535;310,535])
add_line(sys,[175,475;225,475])
add_line(sys,[200,475;200,415;230,415])
add_line(sys,[545,260;685,260])
add_line(sys,[545,260;585,260;585,360;125,360;125,270;145,270])
add_line(sys,[545,475;620,475;615,275;685,275])
add_line(sys,[90,245;90,460;145,460])
add_line(sys,[585,475;585,585;125,585;125,485;145,485])
add_line(sys,[200,585;200,535;220,535])
add_line(sys,[725,395;740,395])
add_line(sys,[380,260;380,170;675,170;685,385])
add_line(sys,[380,475;380,400;685,400])
add_line(sys,[725,270;740,270])

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
