MATLAB: MATLAB event function for ODE, specifying time span, and obtaining solution

eventode45

Hello All,
Warm regards for all and hope you all have a great 2021!
So, in my research project, I am using MATLAB ode45 to integrate some ODEs. I am also using event function to terminate the integration.
The problem that I am having is that I want to have a consistent number of time points and corresponding state soluitions regardless of the time span I provide to ode45.
In general, to be safe, i.e. to make sure that an event happens, I am using a very conservative time span as an input. However, while the event happens, I am getting a very few number of solutions out.
I heard that there are some interpolation functions that allows one to obtain solutions from ode45 at a specified time points but I am not sure how to pass the information from the ode solver to the interpolating function other than a few number of ode solutions. There may be just one single point in certain case…
So basically, are there ways to ensure that (1) the event will happen within a given time span other than being conservative with time span, and (2) can I obtain many time points and corresponding solutions even if the returen ode solutions only contains a few points that are in the range that I want the solution for (i.e. from initial time to an event time)?
Thank you so much for your help!

Best Answer

If you specify a vecor of times for your tspan instead of start and end time, MATLAB will still solve the ode the same, but will return results for the time points you specified. From the tspan input argument definition on the ode45 documentation page.
  • If tspan has two elements, [t0 tf], then the solver returns the solution evaluated at each internal integration step within the interval.
  • If tspan has more than two elements [t0,t1,t2,...,tf], then the solver returns the solution evaluated at the given points. 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. The solutions produced at the specified points are of the same order of accuracy as the solutions computed at each internal step.Specifying several intermediate points has little effect on the efficiency of computation, but for large systems it can affect memory management.