Plot function within optimisation algorithm does not work with subplots -
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Jan
am 16 Nov. 2022
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.
Akzeptierte Antwort
Ishu
am 5 Sep. 2023
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Log Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!