explanation is given inside code as comments for your convenience
all errors thrown are typos
clear all
close all
clc
%creating 1d array% %creates 1 dimensional array%
x=linspace(0,0.6,150); %creates range of values for x%
y=linspace(0,0.6,150); %creates range of values for y%
num_cases=150; %no of iterations for ga to run%
%creating 2D array%
[xx, yy] = meshgrid(x,y); %creates 2 dimensional array%
for i=1:length(xx) %specifying for loop%
for j= 1:length(yy)
input(1)=xx(i,j); %assingning output variable for xx%
input(2)=yy(i,j); %assingning output variable for yy%
f(i,j)=stalagmite(input); %calling function and assigning output variable%
end
end
%study1 without boundary conditions%
for i = 1:num_cases %specifying for loop%
[inputs, fopt(i)] = ga(@stalagmite,2); %calling ga and specifying outputs%
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
fopt = -fopt; %- is given to obtain global maxima%
f = -f;
figure(1) %plotting of generated values from ga%
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
%study 2 with boundaries%
for i= 1:num_cases
%general syntax%
% [inputs,fopt(i)]=ga(@stalagmite,2,a,b,aeq,beq,[ub],[lb])%
[inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6]); %upper and lower limits are specified%
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
fopt = -fopt;
f = -f;
figure(2) %plotting of generated values from ga%
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
%study3 with boundary conditions and population size%
%general syntax%
%[inputs,fopt(i)]=ga(@stalagmite,2,a,b,aeq,beq,[ub],[lb],[nonlcon],[intcon],options);%
options=optimoptions('ga');
options = optimoptions (options, 'PopulationSize', 500); %population size is added%
for i= 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6], [],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
fopt = -fopt;
f = -f;
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)