You are on page 1of 9

strength = (5.88835 + 0.375*x(1) - 6.20833*x(2) + 0.6888*x(3)+ 0.2*x(4) + 0.

424
* x(1) *x(2) - 0.09167*x(1)*x(3) + 0.133*x(2)*x(3))
Nugget_Diameter = (- 4.30682 + 1.4* x(1) -2.589* x(2) + 0.685* x(3)+0.114* x(4)0.0139 x(3)*x(3) - 0.077 *x(1) * x(3) + 0.176*x(2)*x(3))

x(1)= 7-11
x(2)=0.2-0.4
x(3)= 5-17
x(4)=5-7

[7;0.2;5;5]
[11;0.4;17;7]
Function
function y = strength(x)
y = (-(5.88835 + 0.375*x(1) - 6.20833*x(2) + 0.6888*x(3)+ 0.2*x(4) + 0.424 *
x(1) *x(2) - 0.09167*x(1)*x(3) + 0.133*x(2)*x(3)));

Bounds
[7;0.2;5;5]
[11;0.4;17;7]

Simulated Annealing

Start point [7;0.2;5;5]


ObjectiveFunction = @strength;
X0 = [7;0.2;5;5];

lb=[7;0.2;5;5];

ub=[11;0.4;17;7];
[x,fval,exitFlag,output] = simulannealbnd(ObjectiveFunction,X0,lb,ub);
fprintf('The number of iterations was : %d\n', output.iterations);
fprintf('The number of function evaluations was : %d\n', output.funccount);
fprintf('The best function value found was : %g\n', fval);
[x,fval,exitFlag] = simulannealbnd(ObjectiveFunction,X0,lb,ub)

Optimization terminated: change in best function value less than options.TolFun.


The number of iterations was : 2885
The number of function evaluations was : 2962
The best function value found was : -10.5112

Answer

1.0
2.0
3.0
4.0

7.000863171061406
0.20282731582566732
16.895950596159267
6.994413212913133

Objective function value: -10.336292294008466


Optimization terminated: change in best function value less than options.TolFun.

1.0
2.0
3.0
4.0

7.077282117937123
0.21630146244485385
16.257024099091417
6.8469101288832155

1.0
2.0
3.0
4.0

7.000366745740669
0.20001687430478454
16.83019532778666
6.999763159192615

----------------------------Optimization running.

Objective function value: -10.505376231029937


Optimization terminated: change in best function value less than options.TolFun.

GA
[x fval] = ga(@fitnessfun, nvars, options)

Optimization running.
Objective function value: -10.518108792056639
Optimization terminated: average change in the fitness value less than
options.TolFun.

7.0001127566664945

0.20012373439100895

16.999997871300458 6.999999966039531

fun = @strength;
nvars = 4;

lb=[7;0.2;5;5];

ub=[11;0.4;17;7];
[x,fval,exitflag] = particleswarm(fun,nvars,lb,ub)

Output
Optimization ended: relative change in the objective value
over the last OPTIONS.StallIterLimit iterations is less than OPTIONS.TolFun.

x=

7.0000

0.2000 17.0000

7.0000

fval =

-10.5184

exitflag =

options = optimoptions('particleswarm','SwarmSize',100);

[x,fval,exitflag] = particleswarm(fun,nvars,lb,ub,options)

Copy to make new .m file


function stop = pswplotranges(optimValues,state)
stop = false; % This function does not stop the solver
switch state
case 'init'
nplot = size(optimValues.swarm,2); % Number of dimensions
for i = 1:nplot % Set up axes for plot
subplot(nplot,1,i);
tag = sprintf('psoplotrange_var_%g',i); % Set a tag for the subplot
semilogy(optimValues.iteration,0,'-k','Tag',tag); % Log-scaled plot
ylabel(num2str(i))
end
xlabel('Iteration','interp','none'); % Iteration number at the bottom
subplot(nplot,1,1) % Title at the top
title('Log range of particles by component')
setappdata(gcf,'t0',tic); % Set up a timer to plot only when needed
case 'iter'
nplot = size(optimValues.swarm,2); % Number of dimensions
for i = 1:nplot
subplot(nplot,1,i);
% Calculate the range of the particles at dimension i
irange = max(optimValues.swarm(:,i)) - min(optimValues.swarm(:,i));
tag = sprintf('psoplotrange_var_%g',i);
plotHandle = findobj(get(gca,'Children'),'Tag',tag); % Get the subplot
xdata = plotHandle.XData; % Get the X data from the plot
newX = [xdata optimValues.iteration]; % Add the new iteration
plotHandle.XData = newX; % Put the X data into the plot
ydata = plotHandle.YData; % Get the Y data from the plot
newY = [ydata irange]; % Add the new value
plotHandle.YData = newY; % Put the Y data into the plot
end
if toc(getappdata(gcf,'t0')) > 1/30 % If 1/30 s has passed
drawnow % Show the plot
setappdata(gcf,'t0',tic); % Reset the timer
end
case 'done'
% No cleanup necessary
end

fun = @strength;

nvars = 4;

lb=[7;0.2;5;5];
ub=[11;0.4;17;7];
options = optimoptions(@particleswarm,'OutputFcns',@pswplotranges);
rng default % For reproducibility
[x,fval,eflag] = particleswarm(fun,nvars,lb,ub,options)

x=

7.0000

fval =

-10.5184

eflag =

0.2000 17.0000

7.0000

You might also like