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

add_block('built-in/Transfer Fcn',[sys,'/','Transfer Fcn1'])
set_param([sys,'/','Transfer Fcn1'],...
		'Numerator','[1.52]',...
		'Denominator','[25 1]',...
		'position',[205,27,240,63])

add_block('built-in/Transport Delay',[sys,'/',['Transport',13,'Delay']])
set_param([sys,'/',['Transport',13,'Delay']],...
		'Delay Time','15',...
		'position',[290,30,330,60])

add_block('built-in/Sum',[sys,'/','Sum'])
set_param([sys,'/','Sum'],...
		'position',[350,120,370,140])

add_block('built-in/Transport Delay',[sys,'/',['Transport',13,'Delay1']])
set_param([sys,'/',['Transport',13,'Delay1']],...
		'Delay Time','14',...
		'position',[290,120,330,150])

add_block('built-in/Transfer Fcn',[sys,'/','Transfer Fcn'])
set_param([sys,'/','Transfer Fcn'],...
		'Numerator','[5.72]',...
		'Denominator','[60 1]',...
		'position',[210,117,245,153])

add_block('built-in/To Workspace',[sys,'/','y'])
set_param([sys,'/','y'],...
		'mat-name','y',...
		'position',[405,122,455,138])

add_block('built-in/To Workspace',[sys,'/','u'])
set_param([sys,'/','u'],...
		'mat-name','u',...
		'position',[405,187,455,203])

add_block('built-in/Clock',[sys,'/','Clock'])
set_param([sys,'/','Clock'],...
		'position',[240,235,260,255])

add_block('built-in/To Workspace',[sys,'/','time'])
set_param([sys,'/','time'],...
		'mat-name','t',...
		'position',[320,237,370,253])

add_block('built-in/S-Function',[sys,'/','nlmpcsim'])
set_param([sys,'/','nlmpcsim'],...
		'function name','nlmpcsim',...
		'parameters','modelpd,Kmpc,r,usat,tfilter,ud0',...
		'Mask Display','nlmpcsim',...
		'Mask Type','MPCSIM-SIMULINK link')
set_param([sys,'/','nlmpcsim'],...
		'Mask Dialogue','Masked MATLAB function block calls nlmpcsim, links MPCSIM and SIMULINK.|modelpd:|Kmpc:|r:|usat:|tfilter:|ud0:',...
		'Mask Translate','modelpd=@1;Kmpc=@2;r=@3;usat=@4;tfilter=@5;ud0=@6')
set_param([sys,'/','nlmpcsim'],...
		'Mask Help','This links CMPC from MPC-toolbox and SIMULINK. Time must be input into block as last variable in input vector. For information on mask inputs, type help MPCSIM..')
set_param([sys,'/','nlmpcsim'],...
		'Mask Entries','[model dmodel]\/Kmpc1\/0\/[]\/[]\/[]\/',...
		'position',[120,120,185,150])


%     Subsystem  'More Info'.

new_system([sys,'/','More Info'])
set_param([sys,'/','More Info'],'Location',[4,104,390,264])

add_block('built-in/Note',[sys,'/','More Info/Run first mpctutst to evaluate model, dmodel, and Kmpc1'])
set_param([sys,'/','More Info/Run first mpctutst to evaluate model, dmodel, and Kmpc1'],...
		'position',[185,70,190,75])

add_block('built-in/Note',[sys,'/',['More Info/This test shows how masked variables are ',13,'specified  for nlmpcsim.  See the manual for details.']])
set_param([sys,'/',['More Info/This test shows how masked variables are ',13,'specified  for nlmpcsim.  See the manual for details.']],...
		'position',[180,20,185,25])
set_param([sys,'/','More Info'],...
		'Mask Display','?')


%     Finished composite block 'More Info'.

set_param([sys,'/','More Info'],...
		'hide name',0,...
		'Drop Shadow',4,...
		'position',[420,30,447,55])

add_block('built-in/Mux',[sys,'/','Mux'])
set_param([sys,'/','Mux'],...
		'inputs','2',...
		'position',[70,116,100,149])

add_block('built-in/Step Fcn',[sys,'/','d'])
set_param([sys,'/','d'],...
		'position',[30,35,50,55])
add_line(sys,[55,45;200,45])
add_line(sys,[245,45;285,45])
add_line(sys,[335,45;345,125])
add_line(sys,[335,135;345,135])
add_line(sys,[250,135;285,135])
add_line(sys,[375,130;400,130])
add_line(sys,[250,135;250,195;400,195])
add_line(sys,[265,245;315,245])
add_line(sys,[190,135;205,135])
add_line(sys,[105,135;115,135])
add_line(sys,[385,130;385,215;25,215;25,125;65,125])

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
