All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
OBJECTIVE To write a code in MATLAB to optimize the stalagmite function and find the global maxima of the function. What is ga in Matlab? A genetic algorithm is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics the natural process…
Basanagouda K Mudigoudra
updated on 02 Dec 2020
OBJECTIVE
What is ga in Matlab?
A genetic algorithm is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics the natural process of evolution theory of Charles Darvin. As in the biological process here also the fittest one is going to survive to get the best solution for optimization problems.
How does ga works in Matlab?
as shown above, first it will initialize the population randomness and then it will evaluate for the fittest function if it gets an optimum solution then it will terminate and stops the iterations, if the solution found is not optimum then it will use the selection, crossover and the mutation methods to get an optimum solution and then it will calculate for the fittest function and then it will terminate after getting an optimum solution.
SYNTAX OF GA
x=ga(fun,nvars,A,b,Aeq,beq,lb,ub)
where,
fun is the Objective function, specified as a function handle or function name.
nvars is the Number of variables, specified as a positive integer. The solver passes row vectors of length nvars
to fun.
A & b are the linear inequality constraints.
Aeq & beq are the linear equality constraints.
lb is the lower boundary condition.
ub is the upper boundary condition.
STALAGMITE FUNCTION
The stalagmite function used here is,
f(x,y)=f1,xf2,xf1,yf2,y
f1,x=[sin(5.1πx+0.5)]6
f2,x=exp[−4ln(2)((x−0.0667)20.64)]
f1,y=[sin(5.1πy+0.5)]6
f2,y=exp[−4ln(2)((y−0.0667)20.64)]
PROCEDURE
CODE
clear all
close all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50
f=get_stalagmite(x,y)
tic
%study 1 -statistical behaviour
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite_func,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study1_time = toc
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function minimum')
tic
% study 2 statistical behaviour with upper and loewr boundries
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study2_time=toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('function minimum')
% study 3 increasing GA iterations
options=optimoptions('ga');
options=optimoptions(options,'populationsize',300);
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study3_time=toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function minimum')
function [S]=stalagmite_func(input_vector)
x=input_vector(1);
y=input_vector(2);
f1_x=(sin(5.1*pi*x+0.5))^6;
f1_y=(sin(5.1*pi*y+0.5))^6;
f2_x=exp((-4*log(2))*((x-0.0667)^2/0.64));
f2_y=exp((-4*log(2))*((y-0.0667)^2/0.64));
[S]=-(f1_x.*f1_y.*f2_x.*f2_y);
end
function [s] = get_stalagmite(x,y)
[xx yy] = meshgrid(x,y);
x1 = (sin(5.1*pi*xx+0.5)).^6;
y1 = exp((-4*log(2)*(((xx-0.0667).^2)./0.64)));
x2 = (sin(5.1*pi*yy+0.5)).^6;
y2 = exp((-4*log(2)*(((yy-0.0667).^2)./0.64)));
s =-( x1.*x2.*y1.*y2);
end
OUTPUT
For study 1 -statistical behaviors with unbounded conditions
here we can observe that the condition is unbound so we can able to see the randomness of the values here in the below case.
For study2- statistical behaviors with lower bounds and upper bounds.
Here we are giving the bound conditions So the effect on improvement by using bounds is getting feasible solutions to a problem on the one hand. On the other hand, it is reducing the search space, which helps the solver finding good solutions faster.
For study3-increasing the ga iterations that is the population size.
Here we are giving the bound conditions along with that we are increasing the population size to 300 so that it will increase the number of iterations and we can obtain a more accurate or feasible solution and we can able see that in this case of study as we obtain the solution in the below.
ERRORS
The code was running and the values showing was incorrect because actually, it was due to not using the negative sign before the function value which we were plotting that is optf and f and this was corrected.
CONCLUSION
The code in MATLAB to optimize the stalagmite function and the global maxima of the function have done and the graph for the maximum function values vs no of iterations also been plotted.
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Project - 2 - Meshing on the suspension Assembly
AIM: To perform Meshing on the suspension assembly using ANSA. Objective: To perform the pre-requisites such as, checking for the given CAD goemetry errors & perform the topology clean up. To Generate a 2D mesh with the tria elements for the tyre & rim components.(Because, the given quality criteria is same…
09 Nov 2021 02:23 AM IST
Project 1 - 2D meshing on the instrumental Panel
AIM: To perform geometrical cleanup and extracting the midsurface and to generate 2D mesh for the given component. OBJECTIVE: For the given component, check for the geometrical errors and mesh with the given element Quality criteria. After meshing the component the thickness has to be assigned. Write a detailed report…
04 Nov 2021 03:21 PM IST
Week - 8 - Morphing
AIM: To understand the basics of morphing. OBJECTIVES: 1) Practice all the Morphing techniques that were shown in the video for Model-1. 2) Perform morphing for the Model-2 and Increase the Height of the Dog houses and Ribs. MORPHING: Morphing is the process of modifying the design of the model…
31 Oct 2021 09:27 AM IST
Week 6 - Creating connection for Rear door
AIM:To learn how to make connections between parts of the FE model in ANSA. OBJECTIVE: To Create and Apply different PIDs for different components. By using the same technique and the elements as shown in the video, Perform suitable connections for the given Rear door FE model. PROCEDURE: Import the FE model into…
19 Oct 2021 11:24 AM IST
Related Courses
0 Hours of Content
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.