% File: MGProjectileMotionFMAeventDetection.txt
%------------------------------------------------------
%       Physical objects
NewtonianFrame N
Particle       Q
%---------------------------------------------------
%       Mathematical declarations
Constant   g = 9.8 m/s^2,  b = 0.05 N*s/m
Variable   x'',  y''
Q.SetMass( m = 145 grams )
%---------------------------------------------------
%       Translational kinematics
Q.Translate( No, x*Nx> + y*Ny> )
%---------------------------------------------------
%       Add relevant forces
Q.AddForce( -m * g * Ny> )
Q.AddForce( -b * Q.GetVelocity(N) )
%---------------------------------------------------
%       Form equations of motion with F = m*a
DynamicEqn[1] = Dot( Q.GetDynamics(), Nx> )
DynamicEqn[2] = Dot( Q.GetDynamics(), Ny> )

Solve( DynamicEqn,  x'',  y''  )
%---------------------------------------------------
%       Solve nonlinear ODEs (numerically integrate)
Input  tFinal = .6 sec,  tStep = 0.002 sec,  absError = 1.0E-7
Input  x = 0 m,  x' = 2 m/s
Input  y = .09 m,  y' = -2 m/s
Output  x m, y m

ODE() MGProjectileMotionFMAeventDetection.m
%---------------------------------------------------
Save MGProjectileMotionFMAeventDetection.all
Quit

%===========================================================================
function [functionsToEvaluateForEvent, eventTerminatesIntegration1Otherwise0ToContinue, eventDirection_AscendingIs1_CrossingIs0_DescendingIsNegative1] = EventDetection( t, VAR, uSimulink )
%===========================================================================
% Detects when designated functions are zero or cross zero with positive or negative slope.
% Step 1: Uncomment call to mdlDerivatives and mdlOutputs.
% Step 2: Change functionsToEvaluateForEvent,                      e.g., change  []  to  [t - 5.67]  to stop at t = 5.67.
% Step 3: Change eventTerminatesIntegration1Otherwise0ToContinue,  e.g., change  []  to  [1]  to stop integrating.
% Step 4: Change eventDirection_AscendingIs1_CrossingIs0_DescendingIsNegative1,  e.g., change  []  to  [1].
% Step 5: Possibly modify function EventDetectedByIntegrator (if eventTerminatesIntegration1Otherwise0ToContinue is 0).
%---------------------------------------------------------------------------
mdlDerivatives( t, VAR, uSimulink );        % UNCOMMENT FOR EVENT HANDLING
mdlOutputs(     t, VAR, uSimulink );        % UNCOMMENT FOR EVENT HANDLING
functionsToEvaluateForEvent = [y];
eventTerminatesIntegration1Otherwise0ToContinue = [0];
eventDirection_AscendingIs1_CrossingIs0_DescendingIsNegative1 = [-1];
eventDetectedByIntegratorTerminate1OrContinue0 = eventTerminatesIntegration1Otherwise0ToContinue;
end


%===========================================================================
function [isIntegrationFinished, VAR] = EventDetectedByIntegrator( t, VAR, nIndexOfEvents )
%===========================================================================
isIntegrationFinished = eventDetectedByIntegratorTerminate1OrContinue0( nIndexOfEvents );
if( ~isIntegrationFinished ),
   SetNamedQuantitiesFromMatrix( VAR );
   yp = -0.8*yp;
%  Put code here to modify how integration continues.
   VAR = SetMatrixFromNamedQuantities;
end
end
