function [ret,x0,str,ts,xts]=tengine(t,x,u,flag);
%TENGINE	is the M-file description of the SIMULINK system named TENGINE.
%	The block-diagram can be displayed by typing: TENGINE.
%
%	SYS=TENGINE(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 TENGINE 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 TENGINE with a FLAG of zero:
%	[SIZES]=TENGINE([],[],[],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',[107,135,816,684])
     open_system(sys)
end;
set_param(sys,'algorithm',     'RK-45')
set_param(sys,'Start time',    '0.0')
set_param(sys,'Stop time',     '5')
set_param(sys,'Min step size', '1e-4')
set_param(sys,'Max step size', '.01')
set_param(sys,'Relative error','1e-6')
set_param(sys,'Return vars',   't')
set_param(sys,'AssignWideVectorLines','on');

add_block('built-in/Note',[sys,'/',['Note: Exhaust gas recirculation',13,'(EGR) is not included.']])
set_param([sys,'/',['Note: Exhaust gas recirculation',13,'(EGR) is not included.']],...
		'position',[531,160,536,165])

add_block('built-in/Variable Transport Delay',[sys,'/',['Induction to',13,'Power Stroke',13,'Delay']])
set_param([sys,'/',['Induction to',13,'Power Stroke',13,'Delay']],...
		'orientation',2,...
		'Maximum delay:','.1',...
		'Initial Input','.108',...
		'position',[90,227,130,258])


%     Subsystem  ['Integrate Mass Flow',13,'and Sample'].

new_system([sys,'/',['Integrate Mass Flow',13,'and Sample']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample']],'Location',[124,458,1054,677])

add_block('built-in/Scope',[sys,'/',['Integrate Mass Flow',13,'and Sample/Air Charge',13,'per Cylinder']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Air Charge',13,'per Cylinder']],...
		'Vgain','0.150000',...
		'Hgain','0.260000',...
		'Vmax','0.300000',...
		'Hmax','5.200000',...
		'Window',[730,559,1063,811])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Air Charge',13,'per Cylinder']],...
		'position',[845,17,865,43])

add_block('built-in/Outport',[sys,'/',['Integrate Mass Flow',13,'and Sample/Air Charge']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Air Charge']],...
		'position',[885,90,905,110])

add_block('built-in/Memory',[sys,'/',['Integrate Mass Flow',13,'and Sample/delay3']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/delay3']],...
		'orientation',2,...
		'x0','.108',...
		'position',[755,160,795,190])

add_block('built-in/Reset Integrator',[sys,'/',['Integrate Mass Flow',13,'and Sample/Integrate over',13,'intake stroke.']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Integrate over',13,'intake stroke.']],...
		'Initial','.108',...
		'position',[665,65,695,95])

add_block('built-in/Constant',[sys,'/',['Integrate Mass Flow',13,'and Sample/Constant2']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Constant2']],...
		'Value','0',...
		'position',[590,100,610,120])

add_block('built-in/Inport',[sys,'/',['Integrate Mass Flow',13,'and Sample/mdot (g//s)']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/mdot (g//s)']],...
		'position',[590,25,610,45])

add_block('built-in/Relational Operator',[sys,'/',['Integrate Mass Flow',13,'and Sample/Find',13,'Positive',13,'Edges']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Find',13,'Positive',13,'Edges']],...
		'Operator','>',...
		'position',[375,60,400,95])

add_block('built-in/Sum',[sys,'/',['Integrate Mass Flow',13,'and Sample/Sum1']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Sum1']],...
		'inputs','-+',...
		'position',[315,30,335,105])

add_block('built-in/Memory',[sys,'/',['Integrate Mass Flow',13,'and Sample/delay']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/delay']],...
		'position',[240,35,280,65])


%     Subsystem  ['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events'].

new_system([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events']],'Location',[533,111,1083,325])


%     Subsystem  ['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly'].

new_system([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly']],'Location',[23,358,481,650])

add_block('built-in/Constant',[sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly/Constant1']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly/Constant1']],...
		'Value','cross',...
		'position',[35,80,85,100])

add_block('built-in/Inport',[sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly/in_1']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly/in_1']],...
		'position',[50,35,70,55])

add_block('built-in/Gain',[sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly/Gain']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly/Gain']],...
		'Gain','1/tol',...
		'position',[265,45,285,65])

add_block('built-in/Relational Operator',[sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly/Relational',13,'Operator']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly/Relational',13,'Operator']],...
		'Operator','<',...
		'position',[190,37,220,68])

add_block('built-in/Reset Integrator',[sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly/Resetable',13,'Integrator',13,'to stop integral ',13,'wind-up']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly/Resetable',13,'Integrator',13,'to stop integral ',13,'wind-up']],...
		'position',[350,37,380,153])

add_block('built-in/Note',[sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly/Places discontinuity into integrator',13,'which slows the simulation down. ']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly/Places discontinuity into integrator',13,'which slows the simulation down. ']],...
		'position',[120,170,125,175])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly']],[225,55;260,55])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly']],[90,90;145,90;145,60;185,60])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly']],[290,55;345,55])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly']],[75,45;185,45])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly']],[385,95;415,95;415,230;250,230;250,95;345,95])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly']],...
		'Mask Display','plot(-0.2,-0.2,3.45,1.2,dvec,abs(cos(dvec)),[.6 2.5],[0 0])')
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly']],...
		'Mask Type','Crossing')
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly']],...
		'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,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly']],...
		'Mask Translate','cross=@1; tol=@2;dvec=-.3:.03:(pi+.3);')
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly']],...
		'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,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly']],...
		'Mask Entries','[pi 2*pi 3*pi 4*pi]\/1e-6\/')


%     Finished composite block ['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly'].

set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Hit Crossing',13,'to hit transitions',13,'exactly']],...
		'position',[115,80,155,120])


%     Subsystem  ['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation'].

new_system([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation']],'Location',[404,138,1106,318])

add_block('built-in/Outport',[sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation/out_1']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation/out_1']],...
		'position',[600,65,620,85])

add_block('built-in/Logical Operator',[sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation/Logical',13,'Operator6']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation/Logical',13,'Operator6']],...
		'Operator','or',...
		'Number of Input Ports','1',...
		'position',[525,51,555,99])

add_block('built-in/Logical Operator',[sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation/Logical',13,'Operator5']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation/Logical',13,'Operator5']],...
		'Operator','AND',...
		'position',[450,63,480,87])

add_block('built-in/Relational Operator',[sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation/Relational',13,'Operator9']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation/Relational',13,'Operator9']],...
		'Operator','<',...
		'position',[330,93,360,117])

add_block('built-in/Relational Operator',[sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation/Relational',13,'Operator8']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation/Relational',13,'Operator8']],...
		'position',[330,33,360,57])

add_block('built-in/Inport',[sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation/crank_angle']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation/crank_angle']],...
		'position',[45,90,65,110])

add_block('built-in/Constant',[sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation/Constant5']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation/Constant5']],...
		'Value','[0  pi  2*pi   3*pi]',...
		'position',[150,51,280,79])

add_block('built-in/Constant',[sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation/Constant8']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation/Constant8']],...
		'Value','[.5*pi 1.5*pi 2.5*pi  3.5*pi]',...
		'position',[110,115,280,145])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation']],[285,130;290,130;290,110;325,110])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation']],[365,45;405,45;405,70;445,70])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation']],[365,105;405,105;405,80;445,80])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation']],[485,75;520,75])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation']],[560,75;595,75])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation']],[70,100;325,100])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation']],[70,100;125,100;125,40;325,40])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation']],[285,65;290,65;290,50;325,50])


%     Finished composite block ['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation'].

set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/event generation']],...
		'position',[215,24,245,116])

add_block('built-in/Inport',[sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Omega']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/Omega']],...
		'position',[30,60,50,80])

add_block('built-in/Outport',[sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/cylinder event']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events/cylinder event']],...
		'position',[305,60,325,80])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events']],[250,70;300,70])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events']],[55,70;210,70])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events']],[55,70;90,70;90,100;110,100])


%     Finished composite block ['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events'].

set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Generate',13,'Cylinder',13,'Events']],...
		'position',[170,63,205,107])

add_block('built-in/Inport',[sys,'/',['Integrate Mass Flow',13,'and Sample/N (rad//s) ']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/N (rad//s) ']],...
		'Port','2',...
		'position',[30,75,50,95])


%     Subsystem  ['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi'].

new_system([sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi']],'Location',[610,117,1056,405])

add_block('built-in/Note',[sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi/Note: does not reset exactly at 4pi']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi/Note: does not reset exactly at 4pi']],...
		'position',[200,180,205,185])

add_block('built-in/Outport',[sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi/Angle']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi/Angle']],...
		'position',[295,40,315,60])

add_block('built-in/Relational Operator',[sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi/Relational',13,'Operator1']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi/Relational',13,'Operator1']],...
		'orientation',2,...
		'hide name',0,...
		'Operator','>',...
		'position',[115,120,150,140])

add_block('built-in/Constant',[sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi/Constant1']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi/Constant1']],...
		'orientation',2,...
		'hide name',0,...
		'Value','4*pi',...
		'position',[245,95,275,115])

add_block('built-in/Sum',[sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi/Sum1']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi/Sum1']],...
		'orientation',2,...
		'hide name',0,...
		'inputs','+-',...
		'position',[190,90,210,110])

add_block('built-in/Reset Integrator',[sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi/Reset',13,'Integrator']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi/Reset',13,'Integrator']],...
		'Initial','15',...
		'position',[125,35,155,65])

add_block('built-in/Inport',[sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi/Velocity']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi/Velocity']],...
		'position',[45,30,65,50])

add_block('built-in/Note',[sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi/Note: unconnected inputs to blocks are set to zero.']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi/Note: unconnected inputs to blocks are set to zero.']],...
		'position',[200,215,205,220])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi']],[110,130;95,130;95,50;120,50])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi']],[185,100;175,100;175,125;155,125])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi']],[185,100;105,100;105,60;120,60])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi']],[240,105;215,105])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi']],[160,50;290,50])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi']],[225,50;215,95])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi']],[70,40;120,40])


%     Finished composite block ['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi'].

set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Crank Angle',13,'0-4pi']],...
		'position',[95,60,125,110])

add_block('built-in/Constant',[sys,'/',['Integrate Mass Flow',13,'and Sample/Constant']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Constant']],...
		'Value','.5',...
		'position',[330,135,355,155])

add_block('built-in/Mux',[sys,'/',['Integrate Mass Flow',13,'and Sample/Mux']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Mux']],...
		'inputs','2',...
		'position',[785,11,815,44])

add_block('built-in/Switch',[sys,'/',['Integrate Mass Flow',13,'and Sample/sampler2']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/sampler2']],...
		'Threshold','0.5',...
		'position',[760,67,790,133])

add_block('built-in/Note',[sys,'/',['Integrate Mass Flow',13,'and Sample/Impulses are generated',13,'at the beginning of each',13,'intake stroke']])
set_param([sys,'/',['Integrate Mass Flow',13,'and Sample/Impulses are generated',13,'at the beginning of each',13,'intake stroke']],...
		'position',[501,64,506,69])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[750,175;745,175;755,120])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[795,100;820,100;820,175;800,175])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[795,100;880,100])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[55,85;90,85])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[615,35;635,35;635,70;660,70])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[130,85;165,85])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[210,85;215,85;215,50;235,50])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[285,50;310,50])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[210,85;310,85])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[340,70;370,70])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[360,145;370,85])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[615,110;635,110;635,90;660,90])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[700,80;755,80])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[820,30;840,30])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[405,80;430,80;430,20;780,20])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[700,80;735,80;735,35;780,35])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[430,80;660,80])
add_line([sys,'/',['Integrate Mass Flow',13,'and Sample']],[430,80;430,175;735,175;735,100;755,100])


%     Finished composite block ['Integrate Mass Flow',13,'and Sample'].

set_param([sys,'/',['Integrate Mass Flow',13,'and Sample']],...
		'orientation',2,...
		'position',[250,204,280,266])

add_block('built-in/Fcn',[sys,'/','Delay = pi//N'])
set_param([sys,'/','Delay = pi//N'],...
		'orientation',2,...
		'Expr','pi/u[1]',...
		'position',[240,300,290,320])

add_block('built-in/Fcn',[sys,'/','Torque'])
set_param([sys,'/','Torque'],...
		'Expr','-181.3+379.36*u[1]+21.91*u[1]/u[2]-(.85*u[1]*u[1])/(u[2]*u[2])+.26*u[3]-.0028*u[3]*u[3]+.027*u[4]-.000107*u[4]*u[4]+.00048*u[4]*u[3]+2.55*u[3]*u[1]-.05*u[3]*u[3]*u[1]',...
		'position',[220,355,270,375])

add_block('built-in/Mux',[sys,'/',['Cylinder Air Mass',13,'Fuel Mass',13,'Spark Advance',13,'Engine Speed']])
set_param([sys,'/',['Cylinder Air Mass',13,'Fuel Mass',13,'Spark Advance',13,'Engine Speed']],...
		'position',[155,295,185,430])

add_block('built-in/Mux',[sys,'/','Pm, N'])
set_param([sys,'/','Pm, N'],...
		'orientation',2,...
		'inputs','2',...
		'position',[535,200,565,235])

add_block('built-in/Fcn',[sys,'/','Pumping'])
set_param([sys,'/','Pumping'],...
		'orientation',2,...
		'Expr','-.366+.08979*u[1]*u[2]-.0337*u[2]*u[1]*u[1]+.0001*u[1]*u[2]*u[2]',...
		'position',[420,206,480,234])

add_block('built-in/Gain',[sys,'/','Stoichiometric Fuel'])
set_param([sys,'/','Stoichiometric Fuel'],...
		'Gain','1/14.64',...
		'position',[50,329,115,361])

add_block('built-in/Constant',[sys,'/',['Spark Advance',13,'(degrees BTDC)']])
set_param([sys,'/',['Spark Advance',13,'(degrees BTDC)']],...
		'Value','15.0',...
		'position',[45,391,75,409])

add_block('built-in/To Workspace',[sys,'/','To Workspace'])
set_param([sys,'/','To Workspace'],...
		'mat-name','yout',...
		'buffer','3000',...
		'position',[640,302,690,318])

add_block('built-in/Note',[sys,'/',['mdot',13,'Cylinder (g//s)']])
set_param([sys,'/',['mdot',13,'Cylinder (g//s)']],...
		'position',[333,162,365,186])

add_block('built-in/Note',[sys,'/','Engine Timing Model'])
set_param([sys,'/','Engine Timing Model'],...
		'Font Name','helvetica',...
		'Font Weight','bold',...
		'Font Angle','oblique',...
		'Font Size',18,...
		'position',[365,1,370,6])

add_block('built-in/Note',[sys,'/',['Crossley, P.R., and Cook, J.A., ',13,'IEE International Conference ',13,'''Control 91'', Conference Publication 332,',13,' Vol. 2 pp. 921-925,25-28 March, 1991,',13,'Edinburgh, U.K.']])
set_param([sys,'/',['Crossley, P.R., and Cook, J.A., ',13,'IEE International Conference ',13,'''Control 91'', Conference Publication 332,',13,' Vol. 2 pp. 921-925,25-28 March, 1991,',13,'Edinburgh, U.K.']],...
		'position',[120,92,125,97])


%     Subsystem  'Throttle'.

new_system([sys,'/','Throttle'])
set_param([sys,'/','Throttle'],'Location',[327,127,923,365])

add_block('built-in/Inport',[sys,'/',['Throttle/Throttle Angle,',13,'theta (deg)']])
set_param([sys,'/',['Throttle/Throttle Angle,',13,'theta (deg)']],...
		'position',[70,20,90,40])

add_block('built-in/Product',[sys,'/','Throttle/Product'])
set_param([sys,'/','Throttle/Product'],...
		'position',[390,110,415,130])

add_block('built-in/Switch',[sys,'/','Throttle/Switch'])
set_param([sys,'/','Throttle/Switch'],...
		'position',[315,109,340,141])

add_block('built-in/Fcn',[sys,'/','Throttle/Flow Regime'])
set_param([sys,'/','Throttle/Flow Regime'],...
		'Expr','u[1]/u[2]-0.5',...
		'position',[230,115,270,135])

add_block('built-in/Fcn',[sys,'/','Throttle/g(Pm)'])
set_param([sys,'/','Throttle/g(Pm)'],...
		'Expr','2/u[2]*sqrt(u[1]*u[2]-u[1]*u[1])',...
		'position',[230,60,270,80])

add_block('built-in/Mux',[sys,'/','Throttle/Mux'])
set_param([sys,'/','Throttle/Mux'],...
		'inputs','2',...
		'position',[170,106,200,139])

add_block('built-in/Fcn',[sys,'/','Throttle/f(theta)'])
set_param([sys,'/','Throttle/f(theta)'],...
		'Expr','2.821-0.05231*u[1]+0.10299*u[1]*u[1]-0.00063*u[1]*u[1]*u[1]',...
		'position',[230,20,270,40])

add_block('built-in/Inport',[sys,'/',['Throttle/Atmospheric Pressure,',13,'Pa, (bar)']])
set_param([sys,'/',['Throttle/Atmospheric Pressure,',13,'Pa, (bar)']],...
		'Port','3',...
		'position',[70,160,90,180])

add_block('built-in/Inport',[sys,'/',['Throttle/Manifold Pressure,',13,'Pm (bar)']])
set_param([sys,'/',['Throttle/Manifold Pressure,',13,'Pm (bar)']],...
		'Port','2',...
		'position',[70,105,90,125])

add_block('built-in/Constant',[sys,'/','Throttle/Sonic Flow'])
set_param([sys,'/','Throttle/Sonic Flow'],...
		'Value','1.0',...
		'position',[245,160,265,180])

add_block('built-in/Outport',[sys,'/',['Throttle/Ouput Throttle',13,'Flow, mdot (g//s)']])
set_param([sys,'/',['Throttle/Ouput Throttle',13,'Flow, mdot (g//s)']],...
		'position',[475,110,495,130])
add_line([sys,'/','Throttle'],[275,30;355,30;355,115;385,115])
add_line([sys,'/','Throttle'],[275,70;285,70;285,115;310,115])
add_line([sys,'/','Throttle'],[270,170;295,170;295,135;310,135])
add_line([sys,'/','Throttle'],[205,125;210,125;210,70;225,70])
add_line([sys,'/','Throttle'],[205,125;225,125])
add_line([sys,'/','Throttle'],[275,125;310,125])
add_line([sys,'/','Throttle'],[345,125;385,125])
add_line([sys,'/','Throttle'],[420,120;470,120])
add_line([sys,'/','Throttle'],[95,30;225,30])
add_line([sys,'/','Throttle'],[95,115;165,115])
add_line([sys,'/','Throttle'],[95,170;145,170;145,130;165,130])


%     Finished composite block 'Throttle'.

set_param([sys,'/','Throttle'],...
		'position',[345,43,375,157])

add_block('built-in/Constant',[sys,'/',['Atmospheric',13,'Pressure, Pa',13,'(bar)']])
set_param([sys,'/',['Atmospheric',13,'Pressure, Pa',13,'(bar)']],...
		'Value','1.0',...
		'position',[275,130,295,150])

add_block('built-in/Step Fcn',[sys,'/',['Throttle Step',13,'Input']])
set_param([sys,'/',['Throttle Step',13,'Input']],...
		'Time','0',...
		'After','0',...
		'position',[275,50,295,70])

add_block('built-in/Note',[sys,'/',['Thanks to Ken Butts of',13,'Ford Motor Company for',13,'permission to provide this model',13,'based on:',13,'',13,'']])
set_param([sys,'/',['Thanks to Ken Butts of',13,'Ford Motor Company for',13,'permission to provide this model',13,'based on:',13,'',13,'']],...
		'position',[120,31,125,36])

add_block('built-in/Note',[sys,'/',['Modified by P. Barnard (11//94)',13,'and T. Liefeld (9//94), ',13,'The MathWorks, Inc.']])
set_param([sys,'/',['Modified by P. Barnard (11//94)',13,'and T. Liefeld (9//94), ',13,'The MathWorks, Inc.']],...
		'position',[120,161,125,166])


%     Subsystem  'Intake Manifold'.

new_system([sys,'/','Intake Manifold'])
set_param([sys,'/','Intake Manifold'],'Location',[331,508,898,660])

add_block('built-in/Inport',[sys,'/','Intake Manifold/mass flow in'])
set_param([sys,'/','Intake Manifold/mass flow in'],...
		'position',[30,15,50,35])

add_block('built-in/Inport',[sys,'/','Intake Manifold/mass flow out'])
set_param([sys,'/','Intake Manifold/mass flow out'],...
		'Port','2',...
		'position',[30,80,50,100])

add_block('built-in/Sum',[sys,'/','Intake Manifold/Sum'])
set_param([sys,'/','Intake Manifold/Sum'],...
		'inputs','+-',...
		'position',[230,38,250,67])

add_block('built-in/Integrator',[sys,'/','Intake Manifold/Integrate'])
set_param([sys,'/','Intake Manifold/Integrate'],...
		'Initial','.46',...
		'position',[390,45,410,65])

add_block('built-in/Gain',[sys,'/','Intake Manifold/RT//Vm'])
set_param([sys,'/','Intake Manifold/RT//Vm'],...
		'Gain','.41328',...
		'position',[280,36,360,74])

add_block('built-in/Gain',[sys,'/',['Intake Manifold/Ratio of',13,'specific heats, ',13,'gamma']])
set_param([sys,'/',['Intake Manifold/Ratio of',13,'specific heats, ',13,'gamma']],...
		'Gain','1.4',...
		'position',[115,15,155,35])

add_block('built-in/Outport',[sys,'/',['Intake Manifold/Manifold Pressure,',13,'Pm (bar)']])
set_param([sys,'/',['Intake Manifold/Manifold Pressure,',13,'Pm (bar)']],...
		'position',[485,45,505,65])
add_line([sys,'/','Intake Manifold'],[365,55;385,55])
add_line([sys,'/','Intake Manifold'],[415,55;480,55])
add_line([sys,'/','Intake Manifold'],[55,90;215,90;225,60])
add_line([sys,'/','Intake Manifold'],[55,25;110,25])
add_line([sys,'/','Intake Manifold'],[160,25;205,25;205,45;225,45])
add_line([sys,'/','Intake Manifold'],[255,55;275,55])


%     Finished composite block 'Intake Manifold'.

set_param([sys,'/','Intake Manifold'],...
		'position',[500,87,530,138])

add_block('built-in/Note',[sys,'/',['Manifold Pressure,',13,'Pm (bar)']])
set_param([sys,'/',['Manifold Pressure,',13,'Pm (bar)']],...
		'position',[605,76,610,82])

add_block('built-in/Note',[sys,'/',['mdot Throttle',13,'(g//s)']])
set_param([sys,'/',['mdot Throttle',13,'(g//s)']],...
		'position',[420,61,425,66])

add_block('built-in/Integrator',[sys,'/',['Engine',13,'Speed, N',13,'(rad//s)']])
set_param([sys,'/',['Engine',13,'Speed, N',13,'(rad//s)']],...
		'Initial','86',...
		'position',[580,365,600,395])

add_block('built-in/Sum',[sys,'/','Sum1'])
set_param([sys,'/','Sum1'],...
		'inputs','+-',...
		'position',[435,354,455,401])

add_block('built-in/Step Fcn',[sys,'/',['Load Torque',13,'Step1']])
set_param([sys,'/',['Load Torque',13,'Step1']],...
		'Time','4',...
		'After','8',...
		'position',[310,430,330,450])

add_block('built-in/Sum',[sys,'/','Sum2'])
set_param([sys,'/','Sum2'],...
		'position',[380,380,400,400])

add_block('built-in/Step Fcn',[sys,'/',['Load Torque',13,'Step']])
set_param([sys,'/',['Load Torque',13,'Step']],...
		'Time','2',...
		'Before','17',...
		'After','13',...
		'position',[310,375,330,395])

add_block('built-in/Gain',[sys,'/',['Inverse Inertia,',13,'1//J']])
set_param([sys,'/',['Inverse Inertia,',13,'1//J']],...
		'Gain','1/.14',...
		'position',[495,365,540,395])

add_block('built-in/Scope',[sys,'/','Scope'])
set_param([sys,'/','Scope'],...
		'Vgain','110.000000',...
		'Hgain','5.000000',...
		'Vmax','220.000000',...
		'Hmax','10.000000',...
		'Window',[736,565,1067,879],...
		'position',[655,365,685,395])

add_block('built-in/To Workspace',[sys,'/','To Workspace1'])
set_param([sys,'/','To Workspace1'],...
		'mat-name','lout',...
		'buffer','3000',...
		'position',[425,427,475,443])


%     Subsystem  'Subsystem'.

new_system([sys,'/','Subsystem'])
set_param([sys,'/','Subsystem'],'Location',[420,446,596,555])
set_param([sys,'/','Subsystem'],...
		'Mask Display','Double-click\nto run simulation')
set_param([sys,'/','Subsystem'],...
		'Mask Dialogue','eval(''[t,x,y]=rk45(''''tengine'''',5.0,[],[1e-6 1e-4 0.01]);subplot(211);plot(t,yout);ylabel(''''Engine Speed RPM'''');subplot(212),plot(t,lout);ylabel(''''Load Torque NM'''');xlabel(''''Time (sec)'''');'')')


%     Finished composite block 'Subsystem'.

set_param([sys,'/','Subsystem'],...
		'BackGround',5,...
		'hide name',0,...
		'position',[323,496,430,542])
add_line(sys,[300,60;340,60])
add_line(sys,[300,140;340,140])
add_line(sys,[530,220;485,220])
add_line(sys,[535,115;675,115;675,210;570,210])
add_line(sys,[190,365;215,365])
add_line(sys,[415,220;285,220])
add_line(sys,[120,345;150,345])
add_line(sys,[245,235;135,235])
add_line(sys,[235,310;190,310;190,250;135,250])
add_line(sys,[85,245;25,245;25,345;45,345])
add_line(sys,[535,115;675,115;675,36;240,36;240,100;340,100])
add_line(sys,[545,380;575,380])
add_line(sys,[275,365;430,365])
add_line(sys,[605,380;620,380;620,486;115,486;115,415;150,415])
add_line(sys,[405,390;430,390])
add_line(sys,[460,380;490,380])
add_line(sys,[335,440;365,440;375,395])
add_line(sys,[335,385;375,385])
add_line(sys,[605,380;620,380;620,225;570,225])
add_line(sys,[620,311;620,310;360,310;360,250;285,250])
add_line(sys,[620,311;635,310])
add_line(sys,[380,100;495,100])
add_line(sys,[405,220;405,125;495,125])
add_line(sys,[405,390;410,390;420,435])
add_line(sys,[360,310;295,310])
add_line(sys,[25,310;150,310])
add_line(sys,[620,380;650,380])
add_line(sys,[80,400;108,400;108,380;150,380])

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
