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

% Subsystem 'Subsystem2'.

new_system([sys,'/','Subsystem2'])
set_param([sys,'/','Subsystem2'],'Location',[355,393414,500,393509])

add_block('built-in/Note',[sys,'/','Subsystem2/start'])
set_param([sys,'/','Subsystem2/start'],...
'position',[55,45,60,50])
set_param([sys,'/','Subsystem2'],...
'Mask Display','variables\n initialization',...
'Mask Dialogue','eval(''clear all;i3_18;'')')

% Finished composite block 'Subsystem2'.

set_param([sys,'/','Subsystem2'],...
'hide name',0,...
'Font Name','Arial',...
'Drop Shadow',4,...
'position',[5,2,120,61])

add_block('built-in/Sum',[sys,'/','Sum'])
set_param([sys,'/','Sum'],...
'hide name',0,...
'Font Name','Arial',...
'inputs','+-',...
'position',[90,139,110,166])

add_block('built-in/To Workspace',[sys,'/','To Workspace7'])
set_param([sys,'/','To Workspace7'],...
'hide name',0,...
'Font Name','Arial',...
'mat-name','dq',...
'buffer','[2501,1,Ts]',...
'position',[435,70,485,90])

add_block('built-in/To Workspace',[sys,'/','To Workspace6'])
set_param([sys,'/','To Workspace6'],...
'hide name',0,...
'Font Name','Arial',...
'mat-name','e',...
'buffer','[2501,1,Ts]',...
'position',[195,70,245,90])

add_block('built-in/From Workspace',[sys,'/',['From',13,'Workspace1']])
set_param([sys,'/',['From',13,'Workspace1']],...
'hide name',0,...
'Font Name','Arial',...
'matl_expr','[T,x_d]',...
'position',[10,133,60,157])

% Subsystem ['direct',13,'kinematics'].

new_system([sys,'/',['direct',13,'kinematics']])
set_param([sys,'/',['direct',13,'kinematics']],'Location',[212,105,577,369])

add_block('built-in/Note',[sys,'/',['direct',13,'kinematics/y']])
set_param([sys,'/',['direct',13,'kinematics/y']],...
'position',[185,85,190,90])

add_block('built-in/Note',[sys,'/',['direct',13,'kinematics/x']])
set_param([sys,'/',['direct',13,'kinematics/x']],...
'position',[185,35,190,40])

add_block('built-in/Demux',[sys,'/',['direct',13,'kinematics/Demux']])
set_param([sys,'/',['direct',13,'kinematics/Demux']],...
'hide name',0,...
'position',[70,28,110,127])

add_block('built-in/Inport',[sys,'/',['direct',13,'kinematics/cs']])
set_param([sys,'/',['direct',13,'kinematics/cs']],...
'position',[10,70,30,90])

add_block('built-in/Sum',[sys,'/',['direct',13,'kinematics/Sum']])
set_param([sys,'/',['direct',13,'kinematics/Sum']],...
'hide name',0,...
'position',[130,29,145,76])

add_block('built-in/Note',[sys,'/',['direct',13,'kinematics/z']])
set_param([sys,'/',['direct',13,'kinematics/z']],...
'position',[185,140,190,146])

add_block('built-in/Note',[sys,'/',['direct',13,'kinematics/phi']])
set_param([sys,'/',['direct',13,'kinematics/phi']],...
'position',[220,155,235,163])

add_block('built-in/Demux',[sys,'/',['direct',13,'kinematics/Demux1']])
set_param([sys,'/',['direct',13,'kinematics/Demux1']],...
'hide name',0,...
'position',[70,171,115,234])

add_block('built-in/Sum',[sys,'/',['direct',13,'kinematics/Sum3']])
set_param([sys,'/',['direct',13,'kinematics/Sum3']],...
'hide name',0,...
'inputs','+++',...
'position',[180,169,195,221])

add_block('built-in/Mux',[sys,'/',['direct',13,'kinematics/Mux']])
set_param([sys,'/',['direct',13,'kinematics/Mux']],...
'hide name',0,...
'position',[240,80,275,145])

add_block('built-in/Outport',[sys,'/',['direct',13,'kinematics/(x,y,z,phi)']])
set_param([sys,'/',['direct',13,'kinematics/(x,y,z,phi)']],...
'position',[305,105,325,125])

add_block('built-in/Sum',[sys,'/',['direct',13,'kinematics/Sum2']])
set_param([sys,'/',['direct',13,'kinematics/Sum2']],...
'hide name',0,...
'position',[155,78,170,127])

add_block('built-in/Inport',[sys,'/',['direct',13,'kinematics/q']])
set_param([sys,'/',['direct',13,'kinematics/q']],...
'Port','2',...
'position',[10,195,30,215])
add_line([sys,'/',['direct',13,'kinematics']],[175,105;235,105])
add_line([sys,'/',['direct',13,'kinematics']],[35,205;65,205])
add_line([sys,'/',['direct',13,'kinematics']],[115,65;125,65])
add_line([sys,'/',['direct',13,'kinematics']],[280,115;300,115])
add_line([sys,'/',['direct',13,'kinematics']],[150,55;205,55;205,90;235,90])
add_line([sys,'/',['direct',13,'kinematics']],[115,40;125,40])
add_line([sys,'/',['direct',13,'kinematics']],[35,80;65,80])
add_line([sys,'/',['direct',13,'kinematics']],[115,90;150,90])
add_line([sys,'/',['direct',13,'kinematics']],[115,115;150,115])
add_line([sys,'/',['direct',13,'kinematics']],[200,195;215,195;215,135;235,135])
add_line([sys,'/',['direct',13,'kinematics']],[120,195;175,195])
add_line([sys,'/',['direct',13,'kinematics']],[120,180;175,180])
add_line([sys,'/',['direct',13,'kinematics']],[120,225;150,225;150,210;175,210])
add_line([sys,'/',['direct',13,'kinematics']],[120,210;135,210;135,160;205,160;205,120;235,120])
set_param([sys,'/',['direct',13,'kinematics']],...
'Mask Display','k(q)')

% Finished composite block ['direct',13,'kinematics'].

set_param([sys,'/',['direct',13,'kinematics']],...
'orientation',2,...
'move name',0,...
'Font Name','Arial',...
'position',[150,229,190,276])

add_block('built-in/Clock',[sys,'/','Clock'])
set_param([sys,'/','Clock'],...
'hide name',0,...
'Font Name','Arial',...
'position',[390,5,410,25])

add_block('built-in/To Workspace',[sys,'/','To Workspace4'])
set_param([sys,'/','To Workspace4'],...
'hide name',0,...
'Font Name','Arial',...
'mat-name','time',...
'buffer','[2501,1,Ts]',...
'position',[435,5,485,25])

% Subsystem 'Subsystem8'.

new_system([sys,'/','Subsystem8'])
set_param([sys,'/','Subsystem8'],'Location',[355,393414,500,393509])

add_block('built-in/Note',[sys,'/','Subsystem8/start'])
set_param([sys,'/','Subsystem8/start'],...
'position',[55,45,60,50])
set_param([sys,'/','Subsystem8'],...
'Mask Display','plot',...
'Mask Dialogue','eval(''p3_18b;'')')

% Finished composite block 'Subsystem8'.

set_param([sys,'/','Subsystem8'],...
'orientation',2,...
'hide name',0,...
'Font Name','Arial',...
'Drop Shadow',4,...
'position',[415,285,485,316])

add_block('built-in/State-Space',[sys,'/',['Matrix',13,'Gain']])
set_param([sys,'/',['Matrix',13,'Gain']],...
'hide name',0,...
'Font Name','Arial',...
'A','[]',...
'B','[]',...
'C','[]',...
'D','K',...
'Mask Display','K',...
'Mask Type','Matrix Gain')
set_param([sys,'/',['Matrix',13,'Gain']],...
'Mask Dialogue','Matrix Gain.|Gain matrix:',...
'Mask Translate','K = @1;',...
'Mask Help','Multiplies input vector by entered matrix to produce output vector (y=Au).',...
'Mask Entries','K\/')
set_param([sys,'/',['Matrix',13,'Gain']],...
'position',[155,137,190,173])

% Subsystem ['Jacobian',13,'transpose'].

new_system([sys,'/',['Jacobian',13,'transpose']])
set_param([sys,'/',['Jacobian',13,'transpose']],'Location',[147,310,449,410])

add_block('built-in/Outport',[sys,'/',['Jacobian',13,'transpose/dq']])
set_param([sys,'/',['Jacobian',13,'transpose/dq']],...
'position',[255,40,275,60])

add_block('built-in/MATLAB Fcn',[sys,'/',['Jacobian',13,'transpose/MATLAB Fcn']])
set_param([sys,'/',['Jacobian',13,'transpose/MATLAB Fcn']],...
'hide name',0,...
'MATLAB Fcn','trJs_a',...
'Output Width','4',...
'Mask Display','trJ_a',...
'position',[175,35,225,65])

add_block('built-in/Mux',[sys,'/',['Jacobian',13,'transpose/Mux2']])
set_param([sys,'/',['Jacobian',13,'transpose/Mux2']],...
'hide name',0,...
'inputs','[4,4]',...
'position',[120,18,150,82])

add_block('built-in/Inport',[sys,'/',['Jacobian',13,'transpose/cs']])
set_param([sys,'/',['Jacobian',13,'transpose/cs']],...
'Port','2',...
'position',[60,55,80,75])

add_block('built-in/Inport',[sys,'/',['Jacobian',13,'transpose/v']])
set_param([sys,'/',['Jacobian',13,'transpose/v']],...
'position',[25,25,45,45])
add_line([sys,'/',['Jacobian',13,'transpose']],[155,50;170,50])
add_line([sys,'/',['Jacobian',13,'transpose']],[50,35;115,35])
add_line([sys,'/',['Jacobian',13,'transpose']],[230,50;250,50])
add_line([sys,'/',['Jacobian',13,'transpose']],[85,65;115,65])
set_param([sys,'/',['Jacobian',13,'transpose']],...
'Mask Display','tr(J_a(q))Ke')

% Finished composite block ['Jacobian',13,'transpose'].

set_param([sys,'/',['Jacobian',13,'transpose']],...
'move name',0,...
'Font Name','Arial',...
'position',[245,143,305,187])

% Subsystem ['sinusoidal',13,'functions'].

new_system([sys,'/',['sinusoidal',13,'functions']])
set_param([sys,'/',['sinusoidal',13,'functions']],'Location',[79,56,491,216])

add_block('built-in/Demux',[sys,'/',['sinusoidal',13,'functions/Demux']])
set_param([sys,'/',['sinusoidal',13,'functions/Demux']],...
'hide name',0,...
'outputs','[1,1,2]',...
'position',[55,104,95,136])

add_block('built-in/Sum',[sys,'/',['sinusoidal',13,'functions/Sum2']])
set_param([sys,'/',['sinusoidal',13,'functions/Sum2']],...
'hide name',0,...
'position',[120,104,135,126])

add_block('built-in/Note',[sys,'/',['sinusoidal',13,'functions/q1 + q2']])
set_param([sys,'/',['sinusoidal',13,'functions/q1 + q2']],...
'position',[165,95,170,101])

add_block('built-in/Inport',[sys,'/',['sinusoidal',13,'functions/q']])
set_param([sys,'/',['sinusoidal',13,'functions/q']],...
'position',[10,110,30,130])

add_block('built-in/Fcn',[sys,'/',['sinusoidal',13,'functions/Fcn4']])
set_param([sys,'/',['sinusoidal',13,'functions/Fcn4']],...
'hide name',0,...
'Expr','a(2)*sin(u)',...
'Mask Display','',...
'position',[230,104,300,126])

add_block('built-in/Fcn',[sys,'/',['sinusoidal',13,'functions/Fcn']])
set_param([sys,'/',['sinusoidal',13,'functions/Fcn']],...
'hide name',0,...
'Expr','a(1)*cos(u)',...
'Mask Display','',...
'position',[230,29,300,51])

add_block('built-in/Fcn',[sys,'/',['sinusoidal',13,'functions/Fcn3']])
set_param([sys,'/',['sinusoidal',13,'functions/Fcn3']],...
'hide name',0,...
'Expr','a(1)*sin(u)',...
'Mask Display','',...
'position',[230,79,300,101])

add_block('built-in/Fcn',[sys,'/',['sinusoidal',13,'functions/Fcn1']])
set_param([sys,'/',['sinusoidal',13,'functions/Fcn1']],...
'hide name',0,...
'Expr','a(2)*cos(u)',...
'position',[230,54,300,76])

add_block('built-in/Outport',[sys,'/',['sinusoidal',13,'functions/cs']])
set_param([sys,'/',['sinusoidal',13,'functions/cs']],...
'position',[380,70,400,90])

add_block('built-in/Mux',[sys,'/',['sinusoidal',13,'functions/Mux']])
set_param([sys,'/',['sinusoidal',13,'functions/Mux']],...
'hide name',0,...
'position',[330,23,355,132])
add_line([sys,'/',['sinusoidal',13,'functions']],[35,120;50,120])
add_line([sys,'/',['sinusoidal',13,'functions']],[100,120;115,120])
add_line([sys,'/',['sinusoidal',13,'functions']],[100,110;115,110])
add_line([sys,'/',['sinusoidal',13,'functions']],[360,80;375,80])
add_line([sys,'/',['sinusoidal',13,'functions']],[305,65;325,65])
add_line([sys,'/',['sinusoidal',13,'functions']],[305,40;325,40])
add_line([sys,'/',['sinusoidal',13,'functions']],[305,90;325,90])
add_line([sys,'/',['sinusoidal',13,'functions']],[305,115;325,115])
add_line([sys,'/',['sinusoidal',13,'functions']],[100,110;100,40;225,40])
add_line([sys,'/',['sinusoidal',13,'functions']],[140,115;225,115])
add_line([sys,'/',['sinusoidal',13,'functions']],[100,90;225,90])
add_line([sys,'/',['sinusoidal',13,'functions']],[200,115;200,65;225,65])
set_param([sys,'/',['sinusoidal',13,'functions']],...
'Mask Display','cs(q)')

% Finished composite block ['sinusoidal',13,'functions'].

set_param([sys,'/',['sinusoidal',13,'functions']],...
'orientation',2,...
'move name',0,...
'Font Name','Arial',...
'position',[255,220,295,260])

% Subsystem ['Discrete-Time',13,'Integrator'].

new_system([sys,'/',['Discrete-Time',13,'Integrator']])
set_param([sys,'/',['Discrete-Time',13,'Integrator']],'Location',[392,641,765,859])

add_block('built-in/Inport',[sys,'/',['Discrete-Time',13,'Integrator/in_1']])
set_param([sys,'/',['Discrete-Time',13,'Integrator/in_1']],...
'position',[25,70,45,90])

add_block('built-in/Sum',[sys,'/',['Discrete-Time',13,'Integrator/Sum']])
set_param([sys,'/',['Discrete-Time',13,'Integrator/Sum']],...
'position',[150,63,180,127])

add_block('built-in/Unit Delay',[sys,'/',['Discrete-Time',13,'Integrator/Unit Delay']])
set_param([sys,'/',['Discrete-Time',13,'Integrator/Unit Delay']],...
'Sample time','Ts',...
'x0','X0',...
'position',[210,85,260,105])

add_block('built-in/Gain',[sys,'/',['Discrete-Time',13,'Integrator/Gain']])
set_param([sys,'/',['Discrete-Time',13,'Integrator/Gain']],...
'Gain','Ts',...
'position',[80,61,110,99])

add_block('built-in/Outport',[sys,'/',['Discrete-Time',13,'Integrator/out_1']])
set_param([sys,'/',['Discrete-Time',13,'Integrator/out_1']],...
'position',[320,85,340,105])
add_line([sys,'/',['Discrete-Time',13,'Integrator']],[115,80;145,80])
add_line([sys,'/',['Discrete-Time',13,'Integrator']],[50,80;75,80])
add_line([sys,'/',['Discrete-Time',13,'Integrator']],[185,95;205,95])
add_line([sys,'/',['Discrete-Time',13,'Integrator']],[265,95;315,95])
add_line([sys,'/',['Discrete-Time',13,'Integrator']],[285,95;285,165;120,165;120,110;145,110])
set_param([sys,'/',['Discrete-Time',13,'Integrator']],...
'Mask Display','dpoly(Ts*[1 0],[1 -1],''z'')',...
'Mask Type','Discrete-Time Integrator',...
'Mask Dialogue','Discrete-Time Integrator:|Initial Condition:|Sample Time:')
set_param([sys,'/',['Discrete-Time',13,'Integrator']],...
'Mask Translate','X0=@1;Ts=@2;',...
'Mask Help','Implements a zeroth order discrete\nintegration using a gain, a sum, and\na unit delay. Inputs may be scalar\nor vector.\n')
set_param([sys,'/',['Discrete-Time',13,'Integrator']],...
'Mask Entries','q_i\/Tc\/')

% Finished composite block ['Discrete-Time',13,'Integrator'].

set_param([sys,'/',['Discrete-Time',13,'Integrator']],...
'hide name',0,...
'Font Name','Arial',...
'position',[340,146,380,184])

add_block('built-in/To Workspace',[sys,'/','To Workspace5'])
set_param([sys,'/','To Workspace5'],...
'hide name',0,...
'Font Name','Arial',...
'mat-name','q',...
'buffer','[2501,1,Ts]',...
'position',[435,155,485,175])
add_line(sys,[115,155;150,155])
add_line(sys,[65,145;85,145])
add_line(sys,[415,15;430,15])
add_line(sys,[385,165;430,165])
add_line(sys,[250,240;195,240])
add_line(sys,[310,165;335,165])
add_line(sys,[405,165;405,265;195,265])
add_line(sys,[405,240;300,240])
add_line(sys,[195,155;240,155])
add_line(sys,[220,240;220,175;240,175])
add_line(sys,[320,165;320,80;430,80])
add_line(sys,[125,155;125,80;190,80])
add_line(sys,[145,255;70,255;70,160;85,160])

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