function [ret,x0,str,ts,xts]=clutch_2(t,x,u,flag);
%CLUTCH_2	is the M-file description of the SIMULINK system named CLUTCH_2.
%	The block-diagram can be displayed by typing: CLUTCH_2.
%
%	SYS=CLUTCH_2(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_2 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_2 with a FLAG of zero:
%	[SIZES]=CLUTCH_2([],[],[],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',[215,210,925,633])
     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;');


%     Subsystem  'Subsystem2'.

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


%     Finished composite block 'Subsystem2'.

set_param([sys,'/','Subsystem2'],...
		'BackGround',5,...
		'hide name',0,...
		'position',[605,315,706,384])

add_block('built-in/Note',[sys,'/','Tcl MAX, f(Tin)'])
set_param([sys,'/','Tcl MAX, f(Tin)'],...
		'position',[85,97,90,102])

add_block('built-in/Gain',[sys,'/','1//Ie2'])
set_param([sys,'/','1//Ie2'],...
		'orientation',2,...
		'hide name',0,...
		'Gain','1/(1+Ie/Iv)',...
		'position',[65,45,145,105])

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',[30,30,60,50])

add_block('built-in/Constant',[sys,'/','Tin'])
set_param([sys,'/','Tin'],...
		'hide name',0,...
		'Value','Tin',...
		'position',[30,6,60,24])

add_block('built-in/Abs',[sys,'/','Abs'])
set_param([sys,'/','Abs'],...
		'orientation',2,...
		'position',[435,375,465,395])

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',[345,341,410,369])

add_block('built-in/Look Up Table',[sys,'/',['Look-Up',13,'Table']])
set_param([sys,'/',['Look-Up',13,'Table']],...
		'orientation',2,...
		'Input_Values','[0; Tol; Tol+eps;1; 100]',...
		'Output_Values','[1.1*mu;1.1*mu;mu;mu;mu]',...
		'Mask Display','mu',...
		'position',[365,375,395,395])

add_block('built-in/Mux',[sys,'/','Mux5'])
set_param([sys,'/','Mux5'],...
		'orientation',2,...
		'hide name',0,...
		'inputs','2',...
		'position',[180,345,215,380])

add_block('built-in/Fcn',[sys,'/',['Friction Froce model',13,'(static friction included)']])
set_param([sys,'/',['Friction Froce model',13,'(static friction included)']],...
		'orientation',2,...
		'Expr','2/3*R*u(1)*u(2)',...
		'position',[80,352,120,378])


%     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',[615,165,645,195])


%     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',[620,65,650,105])

add_block('built-in/Mux',[sys,'/','Mux'])
set_param([sys,'/','Mux'],...
		'hide name',0,...
		'inputs','2',...
		'position',[565,65,595,100])

add_block('built-in/Sum',[sys,'/','Sum4'])
set_param([sys,'/','Sum4'],...
		'orientation',2,...
		'hide name',0,...
		'position',[165,56,185,89])

add_block('built-in/Mux',[sys,'/','Mux2'])
set_param([sys,'/','Mux2'],...
		'hide name',0,...
		'inputs','2',...
		'position',[240,160,270,195])


%     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',[65,128,100,157])

add_block('built-in/Note',[sys,'/','wv'])
set_param([sys,'/','wv'],...
		'position',[460,185,465,190])

add_block('built-in/Note',[sys,'/','we'])
set_param([sys,'/','we'],...
		'position',[460,150,465,155])

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',[310,275,350,295])

add_block('built-in/Gain',[sys,'/','1//Ie1'])
set_param([sys,'/','1//Ie1'],...
		'orientation',2,...
		'hide name',0,...
		'Gain','Ie*bv/Iv-be',...
		'position',[265,39,350,91])

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',[295,159,370,201])

add_block('built-in/Demux',[sys,'/','Demux'])
set_param([sys,'/','Demux'],...
		'hide name',0,...
		'outputs','2',...
		'position',[400,161,440,194])

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',[155,170,185,200])

add_block('built-in/Mux',[sys,'/','Mux1'])
set_param([sys,'/','Mux1'],...
		'orientation',2,...
		'hide name',0,...
		'inputs','2',...
		'position',[455,266,485,299])

add_block('built-in/Sum',[sys,'/','Sum2'])
set_param([sys,'/','Sum2'],...
		'hide name',0,...
		'inputs','+-',...
		'position',[570,162,585,193])
add_line(sys,[65,15;220,15;220,170;235,170])
add_line(sys,[450,285;355,285])
add_line(sys,[305,285;85,285;85,185;150,185])
add_line(sys,[600,85;615,85])
add_line(sys,[190,185;235,185])
add_line(sys,[105,145;130,145;130,175;150,175])
add_line(sys,[60,75;40,75;40,135;60,135])
add_line(sys,[590,180;610,180])
add_line(sys,[375,180;395,180])
add_line(sys,[275,180;290,180])
add_line(sys,[445,185;565,185])
add_line(sys,[545,185;545,90;560,90])
add_line(sys,[445,170;565,170])
add_line(sys,[525,170;525,75;560,75])
add_line(sys,[220,80;190,80])
add_line(sys,[75,365;40,365;40,195;150,195])
add_line(sys,[525,185;525,290;490,290])
add_line(sys,[505,170;505,275;490,275])
add_line(sys,[480,170;480,65;355,65])
add_line(sys,[260,65;190,65])
add_line(sys,[160,75;150,75])
add_line(sys,[340,355;220,355])
add_line(sys,[175,365;125,365])
add_line(sys,[40,195;40,150;60,150])
add_line(sys,[360,385;320,385;320,370;220,370])
add_line(sys,[590,180;590,385;470,385])
add_line(sys,[430,385;400,385])

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
