1

I would want to solve a differential equation in MATLAB

odeopts = odeset('MaxStep',dt);
[t,X] = ode113(@MyDiff,tSpan,X0,odeopts);

Here dt, MyDiff, tSpan and X0 are defined earlier in the code. The problem is, that the discretisation step dt is very small, while the total simulation time tSpan(end) is very large. Furthermore, the solution is expected to become periodic from some time T with period P. Here, the period P is known a priori, while the time T is not.

What I would want to do is to automatically stop the ode113-solver when the solution X has become periodic, in order to save computational time. I would appreciate any thoughts on how I should do this.

My thoughts until now:

  1. The first subproblem is how to stop the Matlab solver when periodicity is discovered. MATLAB has included the option to stop the ode113-solver by an event-function:

    odeopts = odeset('MaxStep',dt,'Events',MyEventFcn);

However, MyEventFcn has to be a function of t and X at the current time step. It seems impossible to determine periodicity by this information. The only way around seems to me to use a global parameter that includes the X values at all previous time up to two times the expected period P. However, this seems quite inelegant and inefficient to me, and I was hoping there is a better way.

  1. The second problem is how to determine that the solution X is periodic with period P. At the moment I'm thinking that the best way is to use a threshold to the autocorrelation function, xcorr(X), but I'm not certain yet about the details, so any suggestions here would also be useful.

Edit: I implemented a solution, pretty much following my earlier thoughts, using a global variable to keep track of the X-values and using xcorr to detect periodicity. After thinking a while, this actually seems the most straightforward way to do it.

yarnamc
  • 143
  • 5

0 Answers0