can we plot convergence of a ode solver same as fsolve?
조회 수: 3 (최근 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
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
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.
댓글 수: 0
추가 답변 (1개)
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
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 Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!