odeEvent

Event definition for an ode object: stop or note when a quantity crosses zero.

Category: Other public functions: funfun

Syntax

E = odeEvent(EventFcn=fcn)

Creates an event from a function @(t, y) whose zero crossings are the events.

E = odeEvent(EventFcn=fcn, Direction=d, Response=r)

Also sets which crossings count and what the solver does.

Description

Each element of the event function is monitored while the solver steps. When it changes sign in an allowed direction, the solver locates the crossing time accurately and stores it. After solve, the result has the event times in EventTime and the index of the event function that fired in EventIndex.

Typical uses are finding when a falling object hits the ground or when a concentration passes a threshold.

Input arguments

EventFcn
Function handle returning one or more values; an event occurs when a value crosses zero.
Direction
'both' (default), 'ascending' or 'descending'.
Response
'proceed' (default) records the event and goes on, 'stop' ends the integration.

Output arguments

E
An odeEvent object, to be assigned to the EventDefinition property of an ode object.

Example

When does y reach 0.5

e = odeEvent(EventFcn=@(t, y) y - 0.5);
f = ode(ODEFcn=@(t, y) -y, InitialValue=1, EventDefinition=e);
sol = solve(f, 0, 3);
fprintf('%.4f\n', sol.EventTime)
fprintf('%.4f\n', log(2))
Output
0.6931
0.6931

See also

ode odeset ode45 odeMassMatrix odeJacobian