MATLAB: Can I set two different tolerances when I am solving an EDO using ode45

abstolode45reltoltolerancetspan

Hello,
the solution of the EDO I'm solving has a sinusoidal form and I would like to find "exactly" where it reaches a maximum. For this reason, what I do is reduce the tolerance in order to increase the number of points and the likelihood of that one of them would be the maximum.
The problem is I want the maximum after some periods of time (when the solution is stationary) and using the tolerance mentioned before increases a lot the computational time.
So, is there any possibility of solving the EDO using a medium tolerance and when it is near the maximum, then decrease the tolerance to get higher accuracy?
First attempt has been solving the EDO with the medium tolerance, find approximately where it reaches the maximum and solve again using a tspan near the maximum. But the problem has been that the initial condition used is obtained from the first solution, which has an error due to the medium tolerance.
Thanks in advance

Best Answer

This is not an accurate strategy: If you integrate the initial part with a low tolerance, the final position will have a low accuracy in consequence. When you now proceed with a high accuracy, the trajectory will remain away from the true solution. If you want to determine the maximum with an high accuracy, this is the wrong approach.
If you want to determine a certain point of the trajectory with a high accuracy, you have to integrate the complete interval with a low tolerance. But of course do not use a tiny tolerance because this would decrease the accuracy due to the accumulation of rounding errors.
A usual method to determine the location of a maximum is setting an event, which triggers for a zero in the slope. Or you can approximate intervals of the solution by a polynomial and determine its maximum (see polyfit).