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

31 次查看(过去 30 天)
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!

采纳的回答

Cris LaPierre
Cris LaPierre 2021-1-4
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.
  7 个评论
Steven Lord
Steven Lord 2021-1-4
You can call the ODE solver with one output which will be a solution structure. You can pass that structure into deval to evaluate that solution at a list of times that you also pass into deval. The red circles below are the points ode45 computed, the black + symbols are the points computed by deval.
sol = ode45(@(t, y) y, [0 2], 1);
tt = 0:0.125:2;
plot(sol.x, sol.y, 'ro', tt, deval(sol, tt), 'k+-')
Taehun Kim
Taehun Kim 2021-1-4
Thanks Steven! I did see that ode solvers could return a struct as an output and also deval is available but it did not occur to me that I could use the structure to be passed to deval somehow. It seems like when I am using an event function, I get additional fields in the struct containing the event information. I guess with the combination of your proposal and the careful notes from Cris on the number of data points, I may be able to implement something that may solve the problem! As you pointed out, there is a careful balance between the time span that we may provide and the number of time points that we may obtain. I will have to choose some rule in the program so that we are specifying some sort of a reasonable time span for a given IVP... Thank you so much guys! The experience I had from you all are phenomenal!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by