How to Remove Extra Subplot When Custom Plotting with ‘particleswarm’?
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!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! 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! particle swarm optimization, global optimization toolbox MATLAB Answers — New Questions