MATLAB: How to stop ode45 after a fixed number of time steps

arrayarraysodeode45

So, I'm running ode45 for a very large set of N coupled first order ODEs. If I allow ode45 to keep running in some cases, eventually it just stops and gives an out of memory error. "Requested 'N by timesteps' (16.0GB) array exceeds maximum array size preference." I don't have a problem with this, what I do have a problem with is the fact that none of the work ode45 has done up to that point is saved in working memory. If I had a way to stop execution at a set number of timesteps, so that this issue would never arise, that would be great.

Best Answer

If ode45 is taking that many steps, it's probably quite slow. Your system may be stiff; try one of the stiffer solvers Cleve mentions in the later part of that blog post.
Or if it's trying to generate that large a matrix because your tspan vector has many elements, instead try specifying just the start and end times and call ode45 with one output. In that case it will be a struct array, and you can use deval to evaluate the solution for smaller subsets of the time span, process those solution values, then repeat the process until you've performed your calculations for the whole time span.
Related Question