stop Ode45 when y is less than a value

%So thats my ode call and I want it to stop (and save datas % obviously before t=13s) when the value u(1)<=minu
%Where minu is calcuated before.
u0=[vf1m(jj) 0]';
tspan=[0 13]';
Opt = odeset('Events', @myEvent);
[t,u]= ode45(@(t,u) ok(t,u,p2,m,maxu,maxT), tspan, u0,Opt);
%I tried something like that, nothing worked.
function [value, isterminal, direction] = myEvent(t, u, minu)
value = u(1)<=minu
isterminal = 1; % Stop the integration
direction = 0;
%Eroor Message
Not enough input arguments.
Error in myEvent (line 2)
value = u(1)<=minu
Error in odeevents (line 28)
eventValue = feval(eventFcn,t0,y0,eventArgs{:});
Error in ode45 (line 148)
odeevents(FcnHandlesUsed,odeFcn,t0,y0,options,varargin);
Error in skr (line 217)
[t,u]= ode45(@(t,u) ok(t,u,p2,m,maxu,maxT), tspan, u0,Opt);

1 comentario

Lazaros Christoforidis
Lazaros Christoforidis el 28 de Mzo. de 2020
Note: jj have values so sometimes finishes @13s, other times crashes before

Iniciar sesión para comentar.

 Respuesta aceptada

Torsten
Torsten el 28 de Mzo. de 2020

0 votos

As defined, ode45 expects myEvent to have 2 input parameters, but you use 3.
Use
Opt = odeset('Events',@(t,u)myEvent(t,u,minu));
And I suggest you set
value = u(1) - minu;

5 comentarios

Lazaros Christoforidis
Lazaros Christoforidis el 28 de Mzo. de 2020
Thank you for your answer,
I wanted yo try that but I think its kinda impossible to u(1)==minu.
Torsten
Torsten el 28 de Mzo. de 2020
It's usual root finding. So why should it be impossible ?
Lazaros Christoforidis
Lazaros Christoforidis el 28 de Mzo. de 2020
Depends the problem,
I have a loop for many gear ratios and I want to find for each ratio If it works or not
My problem is when the u(1): motorcycle engine speed is less or equal than the minimum (minu) the motorcycle needs to stop.
Steven Lord
Steven Lord el 28 de Mzo. de 2020
You want your event function to be continuous (as the one Torsten suggested is) rather than discontinuous (like yours that uses <= is.)
The ODE solver will detect when the event function passes through zero.
Lazaros Christoforidis
Lazaros Christoforidis el 28 de Mzo. de 2020
Ohh okay, noww it makes sense
Ευχαριστω, υγεια

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by