Plot function within optimisation algorithm does not work with subplots -
1 回表示 (過去 30 日間)
古いコメントを表示
Hello, I am trying to visualise optimisation progress, where the mathematical model is fitted onto measured data. By using only one set of axes, the function works with no problems. Whenever I try to implement subplots to plot multiple values into one figure, I get the following error:
Error using optim.problemdef.OptimizationProblem/solve - Invalid or deleted object.
I am using lsqnonlin function, the options of which are defined by the following:
lsqoptions = optimoptions("lsqnonlin",...
Display = "iter-detailed",...
MaxIterations = 1.5e4,...
MaxFunctionEvaluations = 1e5,...
FunctionTolerance = 1e-18,...
StepTolerance = 1e-18...
);
lsqoptions.PlotFcn = @(x, optimValues, flag) plotprogress(x, optimValues, flag, ipe, plotting, ord, freq, stor_mod, loss_mod, dyn_mod, E);
Within the plotprogress function,I define variables to be plotted along with several other quantities. When trying to plot with subplots, the plot function looks like this:
function out = plotprogress(varargin)
% extracting variables from varargin....
% plotting
ax = subplot(3,1,3);
plotting.count = 1;
for i = 0:18:73
semilogx(ax,freq(i+1:i+18),dyn_mod(i+1:i+18),'.','LineWidth',2,'MarkerSize',25,'Color',plotting.dotcol(plotting.count,:)); hold on; grid on;
plotting.count = plotting.count+1;
end
semilogx(ax,freq,dyn_num,'LineWidth',2,Color=ipe.darkred)
hold off;
legend('20C','10C','0C','-10C','-20C','fit','Location','northwest')
xlabel('f_{master}')
ylabel('Dynamic modulus master')
out = false;
end
Every variable is defined, so this should not be a problem. Whenever I change the ax = subplot(3,1,3); into ax = gca; the plot function seems to be working with no problems whatsoever.
2 件のコメント
Jan
2022 年 11 月 16 日
Please post a copy of the complete message, which reveals, in which line the error occur. Sharing this important inforamtion with the readers ist a good idea.
採用された回答
Ishu
2023 年 9 月 5 日
Hi Ondrej,
I understand that you are getting error while using "solve()" function. This is because your function "plotprogress(...)" is not valid as in this function you are plotting at 3 different axes in one iteration and this is not supported by "plotFcn" . And as you said that when you replace "subplot(...)" with "gca" it does not give any error, this is because "gca" uses only one set of axes to plot.
So what use can do is:
- You can replace "PlotFcn" with "OutputFcn"
lsqoptions.OutputFcn = @(x, optimValues, flag) plotprogress(x, optimValues, flag, ipe, plotting, ord, freq, stor_mod, loss_mod, dyn_mod, E);
2. You can create different functions for different plots, and provide all these functions in "lsqoptions.PlotFcn". This will take more time to complete.
lsqoptions.PlotFcn = {@(x, optimValues, flag) plotprogress_storage(..), @(x, optimValues, flag) plotprogress_loss(..), @(x, optimValues, flag) plotprogress_dynamic(..)};
function out = plotprogress_storage(..)
subplot(3,1,1)
for
% your code
end
% your code
end
function out = plotprogress_loss(..)
subplot(3,1,2)
for
% your code
end
% your code
end
function out = plotprogress_dynamic(..)
subplot(3,1,3)
for
% your code
end
% your code
end
For more information on "lsqnonlin" you can refer to this documentation:
In the above documentation you can also check for different "options" available in "lsqnonlin".
For Output function and plot function you can refer this:
Hope it helps!
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Log Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!