function A = stagalmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1=(sin(5.1*pi*x+0.5)).^6;
f2=exp((-4*log (2)*((x-0.0667).^2))/0.64);
f3=(sin(5.1*pi*y+0.5)).^6;
f4=exp((-4*log(2))*((y-0.0667).^2)/0.64);
A =-(f1*f2*f3*f4);
end
close all
clear all
clc
%inputfor the x and y axis
x=linspace (0,0.6,150);
y=linspace (0,0.6,150);
num_spaces=50;
%since 3d plot we have to foem 2d array
[xx, yy]=meshgrid(x,y);
for i=1:length(x)
for j=1:length(y)
input_vector(1)=xx(i,j);
input_vector(2)=yy(i,j);
f(i,j)=stagalmite(input_vector);
end
end
%studeis are conducte using different syntax of genetic algorith
%study 1
%syntax X= ga(fun,nvars)
tic
for i=1:num_spaces
[results, fopt(i)]=ga (@stagalmite,2)
xopt(i)=results (1)
yopt(i)=results (2)
end
figure(1)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt, yopt,-fopt, 'marker', 'o')
subplot (2,1,2)
plot(-fopt)
xlabel('values')
ylabel('maximum value')
%study 2
% using the next syntax for more accurate values:
tic
for i=1:num_spaces
[results, fopt(i)]=ga (@stagalmite, 2, [], [], [], [], [0;0], [0.6;0.6])
xopt(i)=results (1)
yopt(i)=results (2)
end
study_time2=toc
figure(2)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt, yopt, -fopt, 'marker', 'o')
subplot(2,1,2)
plot(-fopt)
xlabel('values')
ylabel('maximum values')
%study3 with ga initial population and bounded values:
tic
options=optimoptions('ga')
options=optimoptions (options,'populationsize',250)
for i=1:num_spaces
[results, fopt(i)]=ga (@stagalmite, 2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)
xopt(i)=results (1)
yopt(i)=results (2)
end
study_time3=toc
figure(3)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt, yopt,-fopt, 'marker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('values')
ylabel('maximum values')
AIM: TO write a code in the matlab to optimize the stalagmite function and find the global maxima of the function using different case studies for more accuracy.
Theory:
GA:
This is generally an optimization technique and using iteration to find the desired values.
There are different syntax for the GA and we are using 3 different cases for our program
OBJECTIVE:
The global axima of the function are obtained using different syntax
The different syntax that can beee used are:
Syntax for genetic algorithm
we can give the inputs by any one of the following ways
X = ga(fun, nvars)
X = ga(fun, nvars,A,b)
x = ga(fun, nvars,A,b,Aeq, beq)
X = ga(fun, nvars,A,b, Aeq, beq, lb, ub)
x = ga(fun, nvars,A,b,Aeq, beq, lb, ub, nonlcon)
X = ga(fun, nvars,A,b, Aeq, beq, lb, ub, nonlcon, options)
X= ga(fun,nvars,A,b, [], [], lb, ub, nonlcon, IntCon)
X = ga(fun, nvars,A,b, [], [], lb, ub, non Icon, IntCon, options)
X = ga(problem)
[x, fval] = ga(______)
[x, fval, exitflag, output] ga(______) =
[x, fval, exitflag, output, population, scores] = ga(_____)
Body of the program with explanation:
close all
clear all
Clc
%inputfor the x and y axis
x=linspace (0,0.6,150); initial values btw 0 and 0.6
y=linspace (0,0.6,150); 150 values are taken
num_spaces=50; no. of iterations are defined
%since 3d plot we have to foem 2d array
[xx, yy]=meshgrid(x,y); 2d array of x and y is created using meshgrid
for i=1:length(x) initial conditions are provided.
for j=1:length(y)
input_vector(1)=xx(i,j);
input_vector(2)=yy(i,j);
f(i,j)=stagalmite(input_vector); a different function file is created.
end
end
%studeis are conducte using different syntax of genetic algorith
%study 1
%syntax X= ga(fun,nvars)
tic
for i=1:num_spaces the number of iterations can be taken as num_space.
[results, fopt(i)]=ga (@stagalmite,2) this syntax is used to calculate the maxima of the function.
xopt(i)=results (1)
yopt(i)=results (2)
end
figure(1)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
The results are store to xopt and yopt
-f is used to get the 3d plot upside down.
plot3(xopt, yopt,-fopt, 'marker', 'o') plotings of the grap for the first study
subplot (2,1,2) as the 3d image is obtained we use subplot
plot(-fopt)
xlabel('values') the labels for each axis are given;
ylabel('maximum value')
%study 2
% using the next syntax for more accurate values:
The same format is used but the syntax used is bounded values for more accurate positions and values
tic
for i=1:num_spaces
[results, fopt(i)]=ga (@stagalmite, 2, [], [], [], [], [0;0], [0.6;0.6])
xopt(i)=results (1)
yopt(i)=results (2)
end
study_time2=toc
figure(2)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt, yopt, -fopt, 'marker', 'o')
this is used to plot of xopt,yopt and -fopt to plot th optimum maximas
subplot(2,1,2)
plot(-fopt)
xlabel('values')
ylabel('maximum values')
%study3 with ga initial population and bounded values:
The third case is obtained using initial population and bounded values:
By increasing the initial population we can we can increase the accuracy of the result.
Tic this command is used to record the time.
options=optimoptions('ga')
options=optimoptions (options,'populationsize',250)
for i=1:num_spaces
[results, fopt(i)]=ga (@stagalmite, 2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)
xopt(i)=results (1)
yopt(i)=results (2)
end
study_time3=toc
figure(3)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt, yopt,-fopt, 'marker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('values')
ylabel('maximum values')
Function of the program:
function A = stagalmite(input_vector) function definitions
x=input_vector(1); the equations to give out the function maxima
y=input_vector(2);
f1=(sin(5.1*pi*x+0.5)).^6;
f2=exp((-4*log (2)*((x-0.0667).^2))/0.64);
f3=(sin(5.1*pi*y+0.5)).^6;
f4=exp((-4*log(2))*((y-0.0667).^2)/0.64);
A =-(f1*f2*f3*f4);
end
CONCLUSION:
The grap is obtained for the three different studies.