ode45 taking long time to solve
35 views (last 30 days)
Show older comments
Hi, I'm trying to solve a system of ode's with 11 equations using the ode45 solver with time interval 0 to 200 and timestep of 0.1. I am also simulating drug treatments by giving doses as heaviside functions:
J=0;
a=29;
while a<200
J=J+85.62*(heaviside(t-a)-heaviside(t-(a+1)));
a=a+3.5;
end
Without the heaviside functions, the run takes approximately 10 minutes, but with the heaviside functions, it takes many hours to do a single run. Why is it taking so long? Is there a way to reduce the run time?
Thanks!
0 Comments
Answers (2)
Walter Roberson
on 17 Feb 2017
Every use of heaviside corresponds to a discontinuity in your function. The ode*() routines respond to discontinuities by trying to find their exact boundary numerically in order to try to meet integration tolerances. Then when it decides that Yes, there is a discontinuity, it will quit the integration.
If your formula calls for heaviside or piecewise in time, then you need call ode45() once for each individual time range, passing in the objective function specific to that time range and using the output of one as the boundary conditions of the next. If your formula calls for heaviside or piecewise in x (or y) then you need to use event functions to detect transitions between segments and terminate the integration, and then restart inside the next segment.
0 Comments
Peter O
on 16 Feb 2017
Is ode45 a requirement? I'd guess that your equations are becoming stiff when you add in the heavisides. As a result, ode45 is taking really small timesteps to satisfy your tolderance requirements.
Two options:
1. Decrease the tolerance on ode45. Obviously accuracy will suffer if you decrease it too far.
opts = odeset('RelTol',1e-3);
[t,Y] = ode45(@(t,y) myfunc(t,y,....), tspan, ic, opts);
2. Try a stiff solver like ode15s or ode23tb. I'd recommend trying this first.
0 Comments
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!