All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
"Matlab program to optimise the stalagmite function and finding the global maxima of the function." 1. Basics Concepts:- 1.1 Optimization:- Optimization is an Area of study which is used to Maximize (or) Minimize the particular function, which depends upon multiple parameters to get the desired output. 1.2 Stalagmite…
Neeraj Dixit
updated on 12 Dec 2020
1.1 Optimization:-
Optimization is an Area of study which is used to Maximize (or) Minimize the particular function, which depends upon multiple parameters to get the desired output.
1.2 Stalagmite Mathematical Model:-
Stalagmite mathematical model used to develop the local Maxima (or) Minima. Here to determine the optimum solution of the Maxima of the function we are using the Genetic Algorithm Technique. The explanation of the Genetic Algorithm is as follows.
1.3 Genetic algorithm:-
Genetic Algorithm is an Optimization Technique use to solve Non-linear & Non-differential Optimization problem. The name come from using Evolution Biology Technique (Darwin's Theory of Evolution).
In programming, the technique of Evolution is an optimizing process which is developed by Prof. John Holland and his student of the University of Michigan around 1975.
It is Starts with an initial generation of candidate solution that is tested against the objective function. Subsequent Generation evolves from the first through Selection, Crossover and then Mutation. which continues until the cycle stop with the true value.
More Details related to the Selection, Crossover and then Mutation is explained as follows.
Selection: In this, we are selecting the best performing bit strings from one generation to the next generation. i.e based on the Fitness.
Let, take an example
here we have 2 parents who have good performance so selecting them, based on the fitness.
Crossover: Due to they performed well, therefore taking common between them to create a child as shown below.
Mutation: Taking parent and mutate certain variable and create a child based on mutation. i.e in mutation, we are adding other qualities to the child.
To solve the global optimization problem we are using the stalagmite mathematical model which is used to develop the local Maxima and Minima, but in our problem we have to get Global Maxima of the function hence we are using the Genetic algorithm here.
2.1 Step-1:- Initialization.
First, we are defining our search space and creating the one Dimensional arrays x and y with linspace command.
the x and y are input array it does not define the workspace for the genetic algorithm as follows
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
where:-
0 is an initial value
0.6 is a final value and at last
150 is the number of terms.
Now, creating the two Dimensional array using the meshgrid command as,
[xx,yy]= meshgrid(x,y);
For each value of x and y, we are going to create an input vector (row vector) i.e has two-column.
Note:- the command line [xx, yy]= meshgrid(x,y); is to be used in the separate function name as “get_stalagmite”, which explanation we can get in the “Error and elimination” section further.
Further, we are using the for loop to get the same action multiple times.
for i=1:length(xx)
for j=1:length(yy)
input(1)=xx(i,j);
input(2)=yy(i,j);
f(i,j)= stalagmite(input);
end
end
Where, the stalagmite is a separate function which is based on the formula as follows
Equation Used:
f(x,y)=f1,xf2,xf1,yf2,y
f1.x=[sin(5.1πx+0.5)]6
f1.y=[sin(5.1πy+0.5)]6
f2.x=exp[−4ln(2)(x−0.0667)20.64]
f2.y=exp[−4ln(2)(y−0.0667)20.64]
And now we are writing the final stalagmite Function based on the above formula is as,
function [fx_y] = stalagmite(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));
fx_y = -(f1_x.*f1_y.*f2_x.*f2_y);
end
where the only main point needs to be considered is, the function name and file name should be same.
2.2 Step-2:- Assigning the 2nd function.
After stalagmite function, to optimize the final program we are going to define one new function name as “get_stalagmite” which can be accessed for all the cases which we are going to work further.
The new function is containing the for loop command and mesh grid command only. here we are not using the one Dimensional arrays x and y with linspace command, Because the x and y are input array it does not define the workspace for the genetic algorithm, hence it is unnecessary in this function.
The “get_stalagmite” function is as follows which will use in our main program to help's to generate stalagmite function.
function [f] = get_stalagmite(x,y)
[xx,yy]= meshgrid(x,y);
for i=1:length(xx)
for j=1:length(yy)
input(1)=xx(i,j);
input(2)=yy(i,j);
f(i,j)= stalagmite(input);
end
end
2.3 Step-3:- Syntax Referred for Genetic Algorithm.
In main program we are going to use the one Dimensional arrays x and y with linspace command and as we know the x and y are input array it does not define the workspace for the genetic algorithm.
The syntax used for the genetic algorithm is as shown below.
Syntax Referred:
Where:-
finds a local minimum x
= ga(fun
,nvars
,A
,b
)x
to fun
, subject to the linear inequalities A*x
≤ b
. ga
evaluates the matrix product A*x
as if x
is transposed (A*x'
).
finds a local minimum x
= ga(fun
,nvars
,A
,b
,Aeq
,beq
)x
to fun
, subject to the linear equalities Aeq*x
= beq
and A*x
≤ b
. (Set A=[]
and b=[]
if no linear inequalities exist.) ga
evaluates the matrix product Aeq*x
as if x
is transposed (Aeq*x'
).
defines a set of lower and upper bounds on the design variables, x
= ga(fun
,nvars
,A
,b
,Aeq
,beq
,lb
,ub
)x
, so that a solution is found in the range lb
≤ x
≤ ub
. (Set Aeq=[]
and beq=[]
if no linear equalities exist.)
subjects the minimization to the constraints defined in x
= ga(fun
,nvars
,A
,b
,Aeq
,beq
,lb
,ub
,nonlcon
)nonlcon
. The function nonlcon
accepts x
and returns vectors C
and Ceq
, representing the nonlinear inequalities and equalities respectively. ga
minimizes the fun
such that C(x)
≤ 0
and Ceq(x) = 0
. (Set lb=[]
and ub=[]
if no bounds exist.)
minimizes with the default optimization parameters replaced by values in x
= ga(fun
,nvars
,A
,b
,Aeq
,beq
,lb
,ub
,nonlcon
,options
)options
. (Set nonlcon=[]
if no nonlinear constraints exist.) Create options
using optimoptions
.
or x
= ga(fun
,nvars
,A
,b
,[],[],lb
,ub
,nonlcon
,IntCon
)
requires that the variables listed in x
= ga(fun
,nvars
,A
,b
,[],[],lb
,ub
,nonlcon
,IntCon
,options
)IntCon
take integer values.
2.4 Step-4:- Writing Program With Different Optimization studies.
In the main program, we are going to do three studies (one by one) to optimize the solution.
Initially, we use the one Dimensional arrays x and y with linspace command and as we know the x and y are input array it does not define the workspace for the genetic algorithm. Here we are using the component used in the program as follows.
num_cases = 50 indicates that running the genetic algorithm 50 times.
f =get_stalagmite(x,y); is a function which help’s to generate stalagmite function.
F=-f is used to inversing the minimum function i.e to get Maximum function.
after that, we are going to do each study separately as follows.
Study-1:-Statistical Behavior.
In this, we are just calling the (ga) function and passing a number of variables as an input which is equal to 2 and storing the x and y value also the (fopt) optimum value which function is produced.
after that opening new figure window [figure(1)] and creating the subplot (which creating the multiple plots in the same figure window) for the two plot windows in the same figure.
the first plot is surfc(x,y,F) and using the plot3 command for x, y and the optimum value of (ga) predict's over the 50-studies. which help us to know how the algorithm is performing.
Now, the second subplot in which directly optimum value is get plotted as a function of iterations.
To measure how long it takes to measure the study we are using tic and study1_time=toc.
Program for study-1:
close all
clear all
clc
% Defining Array
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50
f =get_stalagmite(x,y);
F=-f
% 1st Study _Statistical Behavior
tic
for i = 1 : num_cases
[inputs, fopt(i)] = ga(@ stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time=toc
fopt=-fopt;
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
plot3( xopt, yopt , fopt, 'marker','o','markersize',6,'markerfacecolor','r')
title('Unbounded Inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function Maximum')
Result of study-1:
Hence, Further optimization is required.
Study-2:-Statistical Behavior - with Upper and Lower Bounds.
In study 2 we are going to use the upper and lower limit which is known as upper and lower bounds. the syntax use is based on our referred syntax section.
Where:-
[0;0] = upper and lower limit of the x.
[1;1] = upper and lower limit of the y.
Program for study-2:
close all
clear all
clc
% Defining Array
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50
f =get_stalagmite(x,y);
F=-f
%Study: 2 - Statistical Behavior - with Upper and Lower Bounds
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
fopt=-fopt;
study2_time=toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
title('Bounded inputs')
xlabel('x values')
ylabel('y values')
plot3(xopt,yopt,fopt,'marker','o','markersize',6,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function Maximum')
Result of study-2:
To optimize the results and getting more accuracy we are increasing the iteration in genetic algorithm. Because by default the (ga) takes 50 value. Here we are going to increase the population size because if initial population is too small then it not going to find out global maxima (or) minima, it only finds the local maxima (or) minima.
the (ga) value is 50 which is changeable by initially (i.e we can use the increased value by default) in the individual program, but in the combined program we need to use the command as follows,
options=optimoptions('ga')
options=optimoptions(options,'PopulationSize',170)
here we are increasing the size to 170 but this is going to slow down the (ga).
Program for study-3:
close all
clear all
clc
% Defining Array
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50
f =get_stalagmite(x,y);
F=-f
%Study: 3 - Increasing GA Iterations
options=optimoptions('ga')
options=optimoptions(options,'PopulationSize',170)
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.5;0.5],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
fopt=-fopt;
study3_time=toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
title('Increased Iterations')
xlabel('x values')
ylabel('y values')
plot3(xopt,yopt,fopt,'marker','o','markersize',6,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function maximum')
Result of study-3:
Note:- The following errors occur only when we are using a separate function known as get_stalagmite. While if we are not using the separate function then the following two errors are not coming into the picture.
1.The first Error in Defining the x and y.
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
In the main program, it is important to define the variable x and y, but it is not required to define in the get_stalagmite function otherwise we get the following error.
To avoid, we are using the x,y variables in the Main Program.
2. The second error in defining the Meshgrid command.
[xx,yy]= meshgrid(x,y).
The meshgrid command is compulsory to Define in the function get_stalagmite. Otherwise, we will get the following error.
To avoid the error, we are using the meshgrid command in the function get_stalagmite.
Note:- Some other points to be considered during the program, to avoid an error.
1. All the functions used in the main program should be in the same folder on the computer.
2. function name and file name should be same
3. For maximizing function, we use fopt=-fopt, F=-f.
Where:-
F=-f is common in all the studies but, use of fopt=-fopt after the end of each for loop is mandatory. otherwise, we will get the error as "Unrecognized function or variable 'fopt'."
Also, if we use the not use fopt=-fopt then the error in output we get is as follows,
Hence, to avoid this kind o error in each plot we are using the
fopt=-fopt.
To get Final combined result we have combined all the Three studies in the same program as follows,
Combined Studies Program:
close all
clear all
clc
% Defining Array
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50
f =get_stalagmite(x,y);
F=-f
% 1st Study _Statistical Behavior
tic
for i = 1 : num_cases
[inputs, fopt(i)] = ga(@ stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time=toc
fopt=-fopt;
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
plot3( xopt, yopt , fopt, 'marker','o','markersize',6,'markerfacecolor','r')
title('Unbounded Inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function Maximum')
%Study: 2 - Statistical Behavior - with Upper and Lower Bounds
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
fopt=-fopt;
study2_time=toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
title('Bounded inputs')
xlabel('x values')
ylabel('y values')
plot3(xopt,yopt,fopt,'marker','o','markersize',6,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function Maximum')
%Study: 3 - Increasing GA Iterations
options=optimoptions('ga')
options=optimoptions(options,'PopulationSize',170)
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.5;0.5],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
fopt=-fopt;
study3_time=toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
title('Increased Iterations')
xlabel('x values')
ylabel('y values')
plot3(xopt,yopt,fopt,'marker','o','markersize',6,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function maximum')
max_value=fopt
Now, we get the most accurate result which is between 0.9999997 to 0.9999998.
To get the maximum value of the function we are using the command in the program of study-3 as,
max_value = fopt
Finally, we get the final result as,
max_value = 1.0000
Conclusion:
Hence, we are able to optimise the stalagmite function and find the global maxima of the function.
The (ga) is only used for the minimum function therefore to Maximize the function we inversed the minimum function.
Referance:-
https://in.mathworks.com/help/gads/ga.html#d120e39233
The flow of Report.
1. Basics.
2. Procedure.
3. Error and Error Elimination.
4. Result and Conclusion.
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...
Week 1 Stress Concentration on a Plate with hole
1. Objective: To Perform a static analysis on two models of a plate with holes. (Axial Loading). CASE-1 CASE-2 Length=300mm Length=300mm Height=120mm Height=120mm Thickness=30mm Thickness=30mm Circular Hole at the center Diameter=60mm Circular Hole at the center Diameter=60mm -----------------NA-------------------…
12 Aug 2022 03:52 PM IST
Comparison in Meshing Method In ANSA (Manual, Batch and casting Mesh)
1. Objective: For the given component, checking for the geometrical errors, Taking the Mid surface and meshing the Mid surface with the given 2D element Quality Criteria and assigning thickness to it. Performing three cases using the following Quality Criteria. S.No Quality Criteria Value 1 Target/Average…
30 Jul 2022 04:23 PM IST
Meshing of Car Hood model
1. Objective: For the given Hood model, Taking the mid surface for all the components after checking the geometrical errors and meshing the mid surface with the following element Quality criteria. S.No Quality Criteria Value 1 Target/Average length 5 2 Minimum Length 2 3 Maximum Length …
16 Jul 2022 07:16 AM IST
Morphing of a component
1. Objective: To Perform a morphing for the given two Models. 2. Procedure: 2.1 Introduction: Change in Design and Shape of components without changing numerical inputs. E.g., changing a vehicle's existing dimension to the new modified version, just like making a sedan version of a vehicle by modifying an already…
16 Jul 2022 07:01 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.