MATLAB: Ode event stopping without a zero crossing

event functionMATLABode45

I'm having trouble with an ODE45 model I am building and the event function used to terminate the integration. The event function I am using detects a zero crossing (positive direction) to terminate the solver then another function modifies the ode and I'm feeding the end state from the first run into another ode45 call as the initial conditions (similar to ballode example except instead of modifying the initial conditions I am modifying ode).
The first termination occurs correctly but then I am getting a second termination during the subsequent run. The output reports the same event index (ie) as the first run. When I manually run the event function with the terminated state I am finding that the value of the flagged event is not zero.
How can the solver stop and report an event with an event value that is much less than zero?
Any thoughts?

Best Answer

Yes, changing the values of the variables that were used to create the anonymous function does not change the values of those variables that the anonymous function stored when it was created. As stated in the "Variables in the Expression" section on this documentation page, to change those values you must recreate the anonymous function.