how to stop ode45 when one of states reach certain value

M
Mohamed Aburakhis · Oct 23, 2023 · 2K views
Question
[x_dot] =derivative(t, x)   x is states [x(1).....x(4)] I need to stop the integration when x(3) reaches 0.1 when the integration stop record (t)
Expert Answer
Profile picture of John Michell
John Michell PhD Expert
Answered Sep 17, 2026

Opt    = odeset('Events', @myEvent);
[T, Y] = ode45(@YourFun, T, Y0, Opt);

function [value, isterminal, direction] = myEvent(T, Y)
value      = (Y(3) == 0.1);
isterminal = 1;   % Stop the integration
direction  = 0;

100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!