function [ret,x0,str,ts,xts]=clutch(t,x,u,flag);
%CLUTCH	is the M-file description of the SIMULINK system named CLUTCH.
%	The block-diagram can be displayed by typing: CLUTCH.
%
%	SYS=CLUTCH(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 CLUTCH 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 CLUTCH with a FLAG of zero:
%	[SIZES]=CLUTCH([],[],[],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',[221,93,950,508])
     open_system(sys)
end;
set_param(sys,'algorithm',     'RK-45')
set_param(sys,'Start time',    '0')
set_param(sys,'Stop time',     '5')
set_param(sys,'Min step size', '0.0001')
set_param(sys,'Max step size', '.1')
set_param(sys,'Relative error','1e-6')
set_param(sys,'Return vars',   't')
set_param(sys,'Load callback','R=.1;Ie=1;Iv=1;be=1;bv=1;we=1;wv=0;Tin=1;mu=.55;Tol=1e-3;');

add_block('built-in/From Workspace',[sys,'/','Lookup Table'])
set_param([sys,'/','Lookup Table'],...
		'matl_expr','[[0; 1; 2; 3; 4 ;5;10],[1; 4; 4 ;0 ;0; 1;1]]',...
		'Mask Display','Tin',...
		'position',[35,55,65,75])

add_block('built-in/Gain',[sys,'/','1//Ie1'])
set_param([sys,'/','1//Ie1'],...
		'orientation',2,...
		'hide name',0,...
		'Gain','Ie*bv/Iv-be',...
		'position',[295,62,395,118])

add_block('built-in/Constant',[sys,'/','Tin'])
set_param([sys,'/','Tin'],...
		'hide name',0,...
		'Value','Tin',...
		'position',[35,31,65,49])

add_block('built-in/Note',[sys,'/','CLUTCH LOCKUP DEMONSTRATION '])
set_param([sys,'/','CLUTCH LOCKUP DEMONSTRATION '],...
		'Font Name','helvetica',...
		'Font Weight','bold',...
		'Font Angle','oblique',...
		'Font Size',18,...
		'position',[365,5,370,10])

add_block('built-in/Sum',[sys,'/','Sum2'])
set_param([sys,'/','Sum2'],...
		'hide name',0,...
		'inputs','+-',...
		'position',[615,192,630,223])

add_block('built-in/Mux',[sys,'/','Mux1'])
set_param([sys,'/','Mux1'],...
		'orientation',2,...
		'hide name',0,...
		'inputs','2',...
		'position',[495,281,525,314])

add_block('built-in/Fcn',[sys,'/',['Friction Force model',13,'(only kinetic friction modeled)']])
set_param([sys,'/',['Friction Force model',13,'(only kinetic friction modeled)']],...
		'orientation',2,...
		'Expr','2/3*R*mu*u',...
		'position',[80,357,120,383])

add_block('built-in/Switch',[sys,'/',['If locked, use minmum ',13,'of f(Tin) and Tcl; otherwize, ',13,'use Tcl from friction model.']])
set_param([sys,'/',['If locked, use minmum ',13,'of f(Tin) and Tcl; otherwize, ',13,'use Tcl from friction model.']],...
		'Threshold','1',...
		'position',[170,200,200,230])

add_block('built-in/Demux',[sys,'/','Demux'])
set_param([sys,'/','Demux'],...
		'hide name',0,...
		'outputs','2',...
		'position',[425,191,465,224])

add_block('built-in/State-Space',[sys,'/','Engine//Vehicle'])
set_param([sys,'/','Engine//Vehicle'],...
		'A','[-be/Ie 0; 0 -bv/Iv]',...
		'B','[1/Ie -1/Ie; 0 1/Iv]',...
		'C','[1 0; 0 1]',...
		'D','zeros(2,2)',...
		'X0','[we wv]',...
		'position',[320,189,395,231])

add_block('built-in/Note',[sys,'/','Tcl MAX, f(Tin)'])
set_param([sys,'/','Tcl MAX, f(Tin)'],...
		'position',[100,122,105,127])

add_block('built-in/From Workspace',[sys,'/',['Clutch Normal',13,'Force']])
set_param([sys,'/',['Clutch Normal',13,'Force']],...
		'orientation',2,...
		'hide name',0,...
		'matl_expr','[[0;0.5; 4; 4.5; 5 ;10],[0; 125; 125; 0; 0;0]]',...
		'Mask Display','Normal\nForce',...
		'position',[230,352,305,388])

add_block('built-in/Fcn',[sys,'/',['Engine speed = Vehicle speed',13,'(Ignore we < wv case)']])
set_param([sys,'/',['Engine speed = Vehicle speed',13,'(Ignore we < wv case)']],...
		'orientation',2,...
		'Expr','u(1)<=u(2)',...
		'position',[335,290,375,310])

add_block('built-in/Note',[sys,'/','we'])
set_param([sys,'/','we'],...
		'position',[515,180,520,185])

add_block('built-in/Note',[sys,'/','wv'])
set_param([sys,'/','wv'],...
		'position',[515,215,520,220])


%     Subsystem  'MIN'.

new_system([sys,'/','MIN'])
set_param([sys,'/','MIN'],'Location',[95,160,366,377])

add_block('built-in/Inport',[sys,'/','MIN/in_2'])
set_param([sys,'/','MIN/in_2'],...
		'Port','2',...
		'position',[10,75,30,95])

add_block('built-in/Outport',[sys,'/','MIN/out_1'])
set_param([sys,'/','MIN/out_1'],...
		'position',[195,70,215,90])

add_block('built-in/Fcn',[sys,'/','MIN/Min'])
set_param([sys,'/','MIN/Min'],...
		'Expr','(u(1)<u(2))*u(1) + (u(2)<=u(1))*u(2)',...
		'position',[120,65,160,95])

add_block('built-in/Mux',[sys,'/','MIN/Mux3'])
set_param([sys,'/','MIN/Mux3'],...
		'hide name',0,...
		'inputs','2',...
		'position',[75,61,105,94])

add_block('built-in/Inport',[sys,'/','MIN/in_1'])
set_param([sys,'/','MIN/in_1'],...
		'position',[15,45,35,65])
add_line([sys,'/','MIN'],[110,80;115,80])
add_line([sys,'/','MIN'],[40,55;70,70])
add_line([sys,'/','MIN'],[165,80;190,80])
add_line([sys,'/','MIN'],[35,85;70,85])
set_param([sys,'/','MIN'],...
		'Mask Display','min',...
		'Mask Dialogue','Minimum ')


%     Finished composite block 'MIN'.

set_param([sys,'/','MIN'],...
		'hide name',0,...
		'position',[80,158,115,187])

add_block('built-in/Mux',[sys,'/','Mux2'])
set_param([sys,'/','Mux2'],...
		'hide name',0,...
		'inputs','2',...
		'position',[255,190,285,225])

add_block('built-in/Sum',[sys,'/','Sum4'])
set_param([sys,'/','Sum4'],...
		'orientation',2,...
		'hide name',0,...
		'position',[170,81,190,114])

add_block('built-in/Gain',[sys,'/','1//Ie2'])
set_param([sys,'/','1//Ie2'],...
		'orientation',2,...
		'hide name',0,...
		'Gain','1/(1+Ie/Iv)',...
		'position',[70,74,150,126])

add_block('built-in/Mux',[sys,'/','Mux'])
set_param([sys,'/','Mux'],...
		'hide name',0,...
		'inputs','2',...
		'position',[610,95,640,130])


%     Subsystem  ['we = yellow',13,'wv = green'].

new_system([sys,'/',['we = yellow',13,'wv = green']])
set_param([sys,'/',['we = yellow',13,'wv = green']],'Location',[0,59,274,252])

add_block('built-in/S-Function',[sys,'/',['we = yellow',13,'wv = green/S-function',13,'M-file which plots',13,'lines',13,'']])
set_param([sys,'/',['we = yellow',13,'wv = green/S-function',13,'M-file which plots',13,'lines',13,'']],...
		'function name','sfunyst',...
		'parameters','ax, color, npts, dt',...
		'position',[130,55,180,75])

add_block('built-in/Inport',[sys,'/',['we = yellow',13,'wv = green/x']])
set_param([sys,'/',['we = yellow',13,'wv = green/x']],...
		'position',[65,55,85,75])
add_line([sys,'/',['we = yellow',13,'wv = green']],[90,65;125,65])
set_param([sys,'/',['we = yellow',13,'wv = green']],...
		'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,'/',['we = yellow',13,'wv = green']],...
		'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,'/',['we = yellow',13,'wv = green']],...
		'Mask Translate','npts = @4; color = @5; ax = [0, @1, @2, @3]; dt=-1;')
set_param([sys,'/',['we = yellow',13,'wv = green']],...
		'Mask Help','Uses MATLAB''s graph window.\nUse only one block per system.\nLine type must be in quotes.\nSee the M-file sfunyst.m.')
set_param([sys,'/',['we = yellow',13,'wv = green']],...
		'Mask Entries','6\/-2\/2\/2000\/''y-/g-/c-/w-/m*/ro/b+''\/')


%     Finished composite block ['we = yellow',13,'wv = green'].

set_param([sys,'/',['we = yellow',13,'wv = green']],...
		'position',[665,95,695,135])


%     Subsystem  'Hit Crossing'.

new_system([sys,'/','Hit Crossing'])
set_param([sys,'/','Hit Crossing'],'Location',[90,259,548,551])

add_block('built-in/Constant',[sys,'/','Hit Crossing/Constant'])
set_param([sys,'/','Hit Crossing/Constant'],...
		'Value','0',...
		'position',[235,125,285,145])

add_block('built-in/Constant',[sys,'/','Hit Crossing/Constant1'])
set_param([sys,'/','Hit Crossing/Constant1'],...
		'Value','cross',...
		'position',[35,80,85,100])

add_block('built-in/Inport',[sys,'/','Hit Crossing/in_1'])
set_param([sys,'/','Hit Crossing/in_1'],...
		'position',[50,35,70,55])

add_block('built-in/Gain',[sys,'/','Hit Crossing/Gain'])
set_param([sys,'/','Hit Crossing/Gain'],...
		'Gain','1/tol',...
		'position',[265,45,285,65])

add_block('built-in/Relational Operator',[sys,'/',['Hit Crossing/Relational',13,'Operator']])
set_param([sys,'/',['Hit Crossing/Relational',13,'Operator']],...
		'Operator','<',...
		'position',[190,37,220,68])

add_block('built-in/Reset Integrator',[sys,'/',['Hit Crossing/Resetable',13,'Integrator',13,'to stop integral ',13,'wind-up']])
set_param([sys,'/',['Hit Crossing/Resetable',13,'Integrator',13,'to stop integral ',13,'wind-up']],...
		'position',[350,37,380,153])

add_block('built-in/Note',[sys,'/',['Hit Crossing/Places discontinuity into integrator',13,'which slows the simulation down. ']])
set_param([sys,'/',['Hit Crossing/Places discontinuity into integrator',13,'which slows the simulation down. ']],...
		'position',[145,180,150,185])
add_line([sys,'/','Hit Crossing'],[225,55;260,55])
add_line([sys,'/','Hit Crossing'],[90,90;145,90;145,60;185,60])
add_line([sys,'/','Hit Crossing'],[290,55;345,55])
add_line([sys,'/','Hit Crossing'],[75,45;185,45])
add_line([sys,'/','Hit Crossing'],[290,135;345,135])
add_line([sys,'/','Hit Crossing'],[385,95;415,95;415,230;305,230;305,95;345,95])
set_param([sys,'/','Hit Crossing'],...
		'Mask Display','plot(-0.2,-0.2,3.45,1.2,dvec,abs(cos(dvec)),[.6 2.5],[0 0])',...
		'Mask Type','Crossing')
set_param([sys,'/','Hit Crossing'],...
		'Mask Dialogue','Causes simulation to take smaller steps at the crossing value (for instance, near a discontinuity). |Crossing Value:|Tol: (typically square simulation tol)')
set_param([sys,'/','Hit Crossing'],...
		'Mask Translate','cross=@1; tol=@2;dvec=-.3:.03:(pi+.3);')
set_param([sys,'/','Hit Crossing'],...
		'Mask Help','This block causes the simulator to take smaller steps near a crossing point. Systems with discontinuities can be more accurately modeled with this block. Use in conjunction with derivative blocks to stop at rising or falling crossing points.')
set_param([sys,'/','Hit Crossing'],...
		'Mask Entries','0\/1e-8\/')


%     Finished composite block 'Hit Crossing'.

set_param([sys,'/','Hit Crossing'],...
		'position',[665,195,695,225])


%     Subsystem  'Subsystem1'.

new_system([sys,'/','Subsystem1'])
set_param([sys,'/','Subsystem1'],'Location',[486,613,757,820])
set_param([sys,'/','Subsystem1'],...
		'Mask Display','See clutch system\nwith static friction\nmodelled',...
		'Mask Type','Button',...
		'Mask Dialogue','eval(''clutch_2'');')


%     Finished composite block 'Subsystem1'.

set_param([sys,'/','Subsystem1'],...
		'BackGround',5,...
		'hide name',0,...
		'position',[585,335,716,405])


%     Subsystem  'Subsystem2'.

new_system([sys,'/','Subsystem2'])
set_param([sys,'/','Subsystem2'],'Location',[486,613,757,820])
set_param([sys,'/','Subsystem2'],...
		'Mask Display','Double click to\nrun Simulation\nfor 5 seconds',...
		'Mask Type','Button',...
		'Mask Dialogue','eval(''y=rk45(''''clutch'''',5,[],[1e-6,1e-4,.1]);'')')


%     Finished composite block 'Subsystem2'.

set_param([sys,'/','Subsystem2'],...
		'BackGround',5,...
		'hide name',0,...
		'position',[585,255,716,325])
add_line(sys,[490,300;380,300])
add_line(sys,[330,300;100,300;100,215;165,215])
add_line(sys,[225,370;125,370])
add_line(sys,[645,115;660,115])
add_line(sys,[205,215;250,215])
add_line(sys,[120,175;145,175;145,205;165,205])
add_line(sys,[75,370;55,370;55,180;75,180])
add_line(sys,[65,100;55,100;55,165;75,165])
add_line(sys,[635,210;660,210])
add_line(sys,[400,210;420,210])
add_line(sys,[290,210;315,210])
add_line(sys,[470,215;610,215])
add_line(sys,[585,215;585,120;605,120])
add_line(sys,[470,200;610,200])
add_line(sys,[565,200;565,105;605,105])
add_line(sys,[70,40;220,40;220,105;195,105])
add_line(sys,[55,225;165,225])
add_line(sys,[565,215;565,305;530,305])
add_line(sys,[545,200;545,290;530,290])
add_line(sys,[505,200;505,90;400,90])
add_line(sys,[290,90;195,90])
add_line(sys,[165,100;155,100])
add_line(sys,[205,105;205,200;250,200])

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
