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

9 次查看(过去 30 天)
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 个评论
Torsten
Torsten 2022-8-25
What would you like to plot in the case of an ode solver ? The actual time reached in the integration process ?

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2022-8-25
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.

更多回答(1 个)

Walter Roberson
Walter Roberson 2022-8-25
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 个评论
Walter Roberson
Walter Roberson 2022-8-25
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by