MATLAB Answers

Making sure the maximum calculated value in my function is represented in ode15s output.

19 views (last 30 days)
steve mayoh
steve mayoh on 8 Jan 2021
Answered: Jan on 9 Jan 2021
I've used ode15s to solve a system of ODEs and then plotted the results. However when I do that, the peak calculated value in my function (which I would like to show on my graph) is not outputted by ode15s and hence my plotted peak does not actually show the true peak value.
Due to the stiffness of my ODEs, I cannot just increase the number of values I have, so I was wondering if there is a way to output the calculated maximum point from my function while using ode15s?
Any help would be much appreciated.


steve mayoh
steve mayoh on 8 Jan 2021
This is the relevant part of the function for the 3rd ODE, the results of which I want to plot against time. The solver used is ode15s and is called with set tolerances for each of the 3 ODEs.
[X,Y] = ode15s(@(t,c) process(t,c),tspan,init_cond,options);
if t < t1
dTdt(3) = 0;
elseif t >= t1 && t < t2
dTdt(3) = Q1/sum;
dTdt(3) = (Q1 - Q2)/sum;
My current plot is shown as below.
I know my function itself iterates 349 time, with one giving a max T of 540, however, from ode15s, the max T is only 537. Ideally, the peak of the graph should be 540.
Please let me know if there is anything else I can share.
Piotr Balik
Piotr Balik on 8 Jan 2021
If you have stepwise defined derivative, how about computing ode at three intervals? Start of the following one would be the last point of the former.

Sign in to comment.

Accepted Answer

Jan on 9 Jan 2021
ODE15S is designed to integrate smooth functions, as the other ODE integrators of MATLAB are. Running it with this not differentiable function replies a trajectory, which is not a "reliable result". The maximum value might be dominated by random rounding errors.
Prefer to run 3 integrations for the different time intervals using the final value of the former one as initial value for the next. After joining the trajectories you have a reliable output including the wanted maximum value.


Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!

Translated by