The right-hand side in my ODE is quite time-consuming. In the course of calculating dy/dt I calculate a variable u that I also use in my events function. Is there a way to do the time-consuming calculations only once at each time step?
Example, where I have replaced the time-consuming calculation with a simple one:
function event_example opt = odeset('events',@stopfun); y0 = 1; [t,y] = ode45(@rhs,[0,3],y0,opt); plot(t,y)endfunction [dydt,eventvalue] = time_consuming_fun(y)% a lot of calculations to find variable u, here simplified to:
u = y; eventvalue = u; dydt = -u*y;endfunction dydt = rhs(~,y) dydt = time_consuming_fun(y);endfunction [value,isterminal,direction] = stopfun(~,y) [~,eventvalue] = time_consuming_fun(y); value = eventvalue-0.4; isterminal = 1; direction = 0;end
Best Answer