Event help with ode15s system of equations

1 view (last 30 days)
Nicole
Nicole on 18 Jun 2013
Hi, I would appreciate any help you can offer. I am a novice in MATLAB and I tried searching the forums but couldn't find a similar situation.
I currently have a matrix of differential equations, V. I would like to stop the ode solver when the difference between the most recent y value and the second to last y value is approximately zero.
I want to be able to automatically run these ODEs that stop when the value isn't changing instead of wasting computational time. I have recently came across the diff command but I am unsure how to implement it in the event.
These are the pertinent sections of my code:
options=odeset('Events', @events, 'OutputSel',[43 50 79],'OutputFcn', @odeplot);
[t,y,te,ye,ie]=ode15s(V,[tstart tfinal],y0,options)
function [value,isterminal,direction]=events(t,y) dy=diff(y(79));
value=dy(length(dy));
isterminal=1; direction=1;
end ---
I am only interested in finding the difference in the 79th index of y. Right now the code is returning the error:
Attempted to access dy(0); index must be a positive integer or logical.
Error in odeStopEvent/events (line 110) value=dy(length(dy));
Error in odeevents (line 29) eventValue = feval(eventFcn,t0,y0,eventArgs{:});
Error in ode15s (line 184) [haveEventFcn,eventFcn,eventArgs,valt,teout,yeout,ieout] = ...
Error in odeStopEvent (line 93) [t,y,te,ye,ie]=ode15s(V,[tstart tfinal],y0,options)
----
When I run with this event function, it doesn't stop even though the difference appears to be close to zero. I've tried subtracting small amounts from the difference but this doesn't stop the ODE either.
function [value,isterminal,direction]=events(t,y)
value=diff(y(79));
isterminal=1;
direction=1;
end
Thanks for your help!

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!