can we plot convergence of a ode solver same as fsolve?

8 views (last 30 days)
Using 'fsolve', we can use the below code to plot the convergence.
x0 = [0,0];
opts = optimoptions(@fsolve,'Display','none','PlotFcn',@optimplotfirstorderopt);
x = fsolve(@root2d1,x0,opts);
function F = root2d1(x)
F(1) = exp(-exp(-(x(1)+x(2)))) - x(2)*(1+x(1)^2);
F(2) = x(1)*cos(x(2)) + x(2)*sin(x(1)) - 0.5;
end
Is there any way to plot the convergence of ode solver?
  1 Comment
Torsten
Torsten on 25 Aug 2022
What would you like to plot in the case of an ode solver ? The actual time reached in the integration process ?

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 25 Aug 2022
I think what you may be looking for is the OutputFcn. See the entry in the Name-Value Arguments section on the odeset function documentation page for more information.

More Answers (1)

Walter Roberson
Walter Roberson on 25 Aug 2022
What would that look like?
At each step, the ode solvers evaluate the function at several points "near" the current point, and use that to predict a new point. A cross-check is done to see if the prediction is reasonable. If the cross-check is within tolerance then the predicted new point is "accepted" and the step size is made larger. If the cross-check is not within tolerence then the predicted new point is rejected and the step size is reduced and new nearby points are tried based on the smaller step size.
The ode solvers do not have target output values such as 0 that they aim towards. They do not have a "goal"; all they have is the tolerance for whether to accept the prediction or not. They are not trying to find roots of the equations: they are evolving a dynamic system in time.
If you had theoretical solutions for the equations (from other source, perhaps dsolve()) then it would be fair to compare the distance between the dynamically-evolved values and the theoretical values... but that would not be "convergence", as it would be expected to get larger and smaller. The step size is deliberately continually increased until the tolerence of the prediction is exceeded, and then managed right on the boundary.
  2 Comments
Walter Roberson
Walter Roberson on 25 Aug 2022
Yes, iif you use odeset() to set the Refine option to 1 or less, and you pass in your tspan as a vector of two elements, then there will be one row of output in t for each successful time step. So you could
NumPlotPoints = 500;
tq = linspace(t(1), t(end), NumPlotPoints);
Steps = interp1(t, 1:length(t), tq);
plot(tq, Steps)
It is not uncommon for most of the work to be concentrated into a small time, so do not be surprised if the step count is fairly low and suddenly starts increasing, or is large at the beginning but then after a while hardly changes.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!