# Is there a way to avoid repeating time-consuming calculations from odefun in the events function?

조회 수: 5 (최근 30일)
Are Mjaavatten . 2019년 10월 7일
편집: Are Mjaavatten . 2019년 10월 8일
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)
end
function [dydt,eventvalue] = time_consuming_fun(y)
% a lot of calculations to find variable u, here simplified to:
u = y;
eventvalue = u;
dydt = -u*y;
end
function dydt = rhs(~,y)
dydt = time_consuming_fun(y);
end
function [value,isterminal,direction] = stopfun(~,y)
[~,eventvalue] = time_consuming_fun(y);
value = eventvalue-0.4;
isterminal = 1;
direction = 0;
end

댓글을 달려면 로그인하십시오.

### 채택된 답변

Stephen23 2019년 10월 7일
편집: Stephen23 님. 2019년 10월 7일
"Is there a way to do the time-consuming calculations only once at each time step?"
Not really, because numeric ODE solvers do not call the function at specific time points, as the ode45 documentation makes clear: "However, the solver does not step precisely to each point specified in tspan. Instead, the solver uses its own internal steps to compute the solution, then evaluates the solution at the requested points in tspan". So even if you "precalculated" the values at particular timepoints, they would not be used by the solver, because the solver picks its own arbitrary timepoints to use... which there is no trivial way to predict.
You might be able to use interpolation though, depending on the behavior of the function and if it really is slow enough to justify doing: evaluate the actual ode function at the timepoints before calling ode45, pass that data as extra parameter/s, then interpolate it within an anonymous/nested function.
I doubt that memoize would make much difference, as very few function evaluations are likely to repeat exactly the same input values.
##### 댓글 수: 1표시 없음숨기기 없음
Are Mjaavatten 2019년 10월 8일
편집: Are Mjaavatten 님. 2019년 10월 8일
I was relatively sure that there was no easy solution. I submitted the question in the hope that some clever person out there might prove me wrong. Your answer strengthens my suspicion that no such solution exists. Precalculation and interpolation is an interesting idea that may work in simple cases. However, it is unlikely to work in my real problem, where y is a five component vector.
I guess I just have to accept that my problem takes about twice the time it would "ideally" take.

댓글을 달려면 로그인하십시오.

### 추가 답변 (1개)

Daniel M 2019년 10월 7일
You might find this comment by Walter Roberson useful, where he talks about using memoize().

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

R2014b

### Community Treasure Hunt

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

Start Hunting!