function [ret,x0,str,ts,xts]=mpctesb(t,x,u,flag);
%MPCTESB	is the M-file description of the SIMULINK system named MPCTESB.
%	The block-diagram can be displayed by typing: MPCTESB.
%
%	SYS=MPCTESB(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 MPCTESB 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 MPCTESB with a FLAG of zero:
%	[SIZES]=MPCTESB([],[],[],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',[266,129,766,429])
     open_system(sys)
end;
set_param(sys,'algorithm',     'Gear')
set_param(sys,'Start time',    '0.0')
set_param(sys,'Stop time',     '0.4')
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/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',[215,195,280,225])

add_block('built-in/S-Function',[sys,'/','S-Function'])
set_param([sys,'/','S-Function'],...
		'function name','vdv',...
		'position',[220,120,270,140])

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

add_block('built-in/To Workspace',[sys,'/','time'])
set_param([sys,'/','time'],...
		'mat-name','t',...
		'position',[165,52,215,68])

add_block('built-in/To Workspace',[sys,'/','input'])
set_param([sys,'/','input'],...
		'orientation',2,...
		'mat-name','u',...
		'position',[75,232,125,248])


%     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',[291,43,347,69])


%     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',[310,10,337,35])

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/Mux',[sys,'/','Mux'])
set_param([sys,'/','Mux'],...
		'inputs','2',...
		'position',[150,111,185,144])

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

add_block('built-in/Demux',[sys,'/','Demux'])
set_param([sys,'/','Demux'],...
		'orientation',2,...
		'outputs','2',...
		'position',[305,185,345,220])

add_block('built-in/Step Fcn',[sys,'/','disturb'])
set_param([sys,'/','disturb'],...
		'Time','0',...
		'Before','5.1',...
		'After','5.1',...
		'position',[85,125,105,145])

add_block('built-in/Sum',[sys,'/','Sum'])
set_param([sys,'/','Sum'],...
		'orientation',2,...
		'position',[160,205,180,225])

add_block('built-in/Constant',[sys,'/','bias'])
set_param([sys,'/','bias'],...
		'orientation',2,...
		'Value','3.84',...
		'position',[205,247,235,263])
add_line(sys,[95,60;160,60])
add_line(sys,[275,130;400,130])
add_line(sys,[190,130;215,130])
add_line(sys,[375,130;375,205;350,205])
add_line(sys,[300,210;285,210])
add_line(sys,[110,135;145,135])
add_line(sys,[210,210;185,210])
add_line(sys,[200,255;195,255;185,220])
add_line(sys,[155,215;45,215;45,120;145,120])
add_line(sys,[140,215;140,240;130,240])

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
