How to Remove Extra Subplot When Custom Plotting with 'particleswarm'?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hello MATLAB Community,
I’m working on implementing a custom plot function for the particleswarm function in the Global Optimization Toolbox. However, I’m encountering an issue where an extra empty subplot is being generated along with the intended plots. I would like to know how to remove this extra subplot.
Here is my full code:
fun = @(x) 3*(1-x(1)).^2.*exp(-(x(1).^2)-(x(2)+1).^2)...
- 10*(x(1)/5 - x(1).^3 - x(2).^5).*exp(-x(1).^2-x(2).^2)...
- 1/3*exp(-(x(1)+1).^2 - x(2).^2);
xbounds = [-3, 3, -3, 3]; % [xmin xmax ymin ymax]
fconvert = @(x1, x2) fun([x1, x2]); % for fsurf, fun should be defined as fun = @(x1,x2,...,xn) but for PSO, fun = @(x)
myfig = fsurf(fconvert, xbounds,'ShowContours','on', 'FaceAlpha', 0.7);
xlabel('x1');
ylabel('x2');
zlabel('f(x1, x2)');
box on
plotFcnWithHandle = @(optimValues, state) myplot(optimValues, state, myfig);
rng default % For reproducibility
nvars = 2;
lb = [xbounds(1), xbounds(3)];
ub = [xbounds(2), xbounds(4)];
options = optimoptions('particleswarm', ...
'SwarmSize', 50,...
'Display','iter',...
'DisplayInterval', 1,...
'FunctionTolerance', 0.01,...
'MaxIterations', 500,...
'MaxStallIterations', 20,...
'PlotFcn', {'pswplotbestf',plotFcnWithHandle});
[x,fval] = particleswarm(fun,nvars,lb,ub,options);
function stop = myplot(optimValues, state, myfig)
stop = false;
ax = get(myfig,'Parent');
positions = optimValues.swarm;
hold(ax, 'on');
delete(findall(ax, 'Tag', 'particles'));
scatter3(ax, positions(:,1), positions(:,2), optimValues.swarmfvals, ...
'r', 'filled', 'MarkerEdgeColor','k', 'Tag', 'particles');
% Update the figure
drawnow;
pause(1); % Pause for 1 second
end
In the code above, an extra empty subplot is generated in addition to the intended plot in the figure titled as "particleswarm". I would like to know how to prevent or remove this extra subplot. Thank you for your help!
0 Commenti
Risposte (1)
Walter Roberson
il 23 Ago 2024
'PlotFcn', {'pswplotbestf',plotFcnWithHandle})
Plotting is generating one subplot for each plot function specified in the 'PlotFcn' option. You have specified two plot functions, so it generates two subplots.
To get around this, you will need to create a single new plot function that combines the effect of pswplotbestf and plotFcnWithHandle .
plotFcnWithHandle = @(optimValues, state) dual_plots(optimValues, state, myfig);
%...
'PlotFcn', plotFcnWithHandle )
%...
function dual_plots(optimValues, state, myfig)
pswplotbestf(optimValues, state);
myplot(optimValues, state, myfig)
end
2 Commenti
Walter Roberson
il 27 Ago 2024
Modificato: Walter Roberson
il 27 Ago 2024
functions stop = dual_plots(optimValues, state, myfig)
stop = pswplotbestf(optimValues, state);
if ~stop
stop = myplot(optimValues, state, myfig);
end
end
Vedere anche
Categorie
Scopri di più su Particle Swarm in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!