How to Remove Extra Subplot When Custom Plotting with 'particleswarm'?
조회 수: 18 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Walter Roberson
2024년 8월 23일
'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
Walter Roberson
2024년 8월 27일
편집: Walter Roberson
2024년 8월 27일
functions stop = dual_plots(optimValues, state, myfig)
stop = pswplotbestf(optimValues, state);
if ~stop
stop = myplot(optimValues, state, myfig);
end
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Particle Swarm에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


