function [ret,x0,str,ts,xts]=mpctest(t,x,u,flag);
%MPCTEST	is the M-file description of the SIMULINK system named MPCTEST.
%	The block-diagram can be displayed by typing: MPCTEST.
%
%	SYS=MPCTEST(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 MPCTEST 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 MPCTEST with a FLAG of zero:
%	[SIZES]=MPCTEST([],[],[],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',[225,160,768,481])
     open_system(sys)
end;
set_param(sys,'algorithm',     'Gear')
set_param(sys,'Start time',    '0.0')
set_param(sys,'Stop time',     '1')
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,'/',[' nlmpcsim test',13,'(Double click on the "?" for more info)']])
set_param([sys,'/',[' nlmpcsim test',13,'(Double click on the "?" for more info)']],...
		'position',[175,5,180,10])

add_block('built-in/S-Function',[sys,'/','nlmpcsim'])
set_param([sys,'/','nlmpcsim'],...
		'orientation',2,...
		'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]\/Kmpc\/r\/usat\/tfilter\/u0\/',...
		'position',[230,215,295,245])

add_block('built-in/Constant',[sys,'/','u'])
set_param([sys,'/','u'],...
		'Value','15',...
		'position',[90,120,110,140])

add_block('built-in/Step Fcn',[sys,'/','disturb'])
set_param([sys,'/','disturb'],...
		'Time','0.3',...
		'Before','5.1',...
		'After','5.1',...
		'position',[150,145,170,165])

add_block('built-in/To Workspace',[sys,'/','output'])
set_param([sys,'/','output'],...
		'mat-name','y',...
		'position',[470,142,520,158])

add_block('built-in/Mux',[sys,'/','Mux'])
set_param([sys,'/','Mux'],...
		'inputs','2',...
		'position',[215,131,250,164])


%     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/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])

add_block('built-in/Note',[sys,'/','More Info/Double click on the "Load Data" before running this demo..'])
set_param([sys,'/','More Info/Double click on the "Load Data" before running this demo..'],...
		'position',[185,70,190,75])
set_param([sys,'/','More Info'],...
		'Mask Display','?')


%     Finished composite block 'More Info'.

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


%     Subsystem  'Load Data'.

new_system([sys,'/','Load Data'])
set_param([sys,'/','Load Data'],'Location',[0,80,386,240])
set_param([sys,'/','Load Data'],...
		'Mask Display','Load\nData',...
		'Mask Dialogue','eval(''vdv_dat'');')


%     Finished composite block 'Load Data'.

set_param([sys,'/','Load Data'],...
		'hide name',0,...
		'Drop Shadow',4,...
		'position',[356,63,412,89])

add_block('built-in/To Workspace',[sys,'/','time'])
set_param([sys,'/','time'],...
		'mat-name','t',...
		'position',[230,72,280,88])

add_block('built-in/Clock',[sys,'/','Clock'])
set_param([sys,'/','Clock'],...
		'position',[135,70,155,90])

add_block('built-in/S-Function',[sys,'/','S-Function'])
set_param([sys,'/','S-Function'],...
		'function name','vdv',...
		'position',[285,140,335,160])

add_block('built-in/To Workspace',[sys,'/','input'])
set_param([sys,'/','input'],...
		'orientation',2,...
		'mat-name','u',...
		'position',[85,282,135,298])

add_block('built-in/Demux',[sys,'/','Demux'])
set_param([sys,'/','Demux'],...
		'orientation',2,...
		'outputs','2',...
		'position',[370,205,410,240])
add_line(sys,[160,80;225,80])
add_line(sys,[340,150;465,150])
add_line(sys,[255,150;280,150])
add_line(sys,[440,150;440,225;415,225])
add_line(sys,[175,155;210,155])
add_line(sys,[365,230;300,230])
add_line(sys,[225,230;130,230;130,140;210,140])
add_line(sys,[175,230;175,290;140,290])

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
