All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
1. AIM: To Optimize the given Stalagmite Function and find out Global Maxima of the function using ga (Genetic-Algorithm) in MATLAB. 2. OBJECTIVES OF THE PROJECT: Theory about ga (Genetic-Algorithm). Stopping criteria. The syntax for ga in MATLAB. 3. THEORY & GOVERNING EQUATIONS: Introduction to Genetic Algorithm.…
Amit Chilap
updated on 04 Mar 2021
1. AIM:
2. OBJECTIVES OF THE PROJECT:
3. THEORY & GOVERNING EQUATIONS:
A genetic algorithm is a search heuristic that is inspired by Charles Darwin’s theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction to produce offspring of the next generation. A Genetic Algorithm (GA) is a search-based optimization technique based on the principles of Genetics and Natural Selection. It is frequently used to find optimal or near-optimal solutions to difficult problems which otherwise would take a lifetime to solve. It is frequently used to solve optimization problems, in research, and machine learning.
The Notion of Natural Selection
The process of natural selection starts with the selection of the fittest individuals from a population. They produce offspring which inherit the characteristics of the parents and will be added to the next generation. If parents have better fitness, their offspring will be better than parents and have a better chance at surviving. This process keeps on iterating and in the end, a generation with the fittest individuals will be found.
This notion can be applied to a search problem. We consider a set of solutions for a problem and select the set of best ones out of them.
The process begins with a set of individuals which is called a Population. Each individual is a solution to the problem you want to solve.
An individual is characterized by a set of parameters (variables) known as Genes. Genes are joined into a string to form a Chromosome (solution).
In a genetic algorithm, the set of genes of an individual is represented using a string, in terms of an alphabet. Usually, binary values are used (a string of 1s and 0s). We say that we encode the genes in a chromosome.
Population, Chromosomes, and Genes
The fitness function determines how to fit an individual is (the ability of an individual to compete with other individuals). It gives a fitness score to each individual. The probability that an individual will be selected for reproduction is based on its fitness score.
The idea of the selection phase is to select the fittest individuals and let them pass their genes to the next generation.
Two pairs of individuals (parents) are selected based on their fitness scores. Individuals with high fitness have more chances to be selected for reproduction.
Crossover is the most significant phase in a genetic algorithm. For each pair of parents to be mated, a crossover point is chosen at random from within the genes.
For example, consider the crossover point to be 3 as shown below.
Crossover point
Offspring are created by exchanging the genes of parents among themselves until the crossover point is reached.
Exchanging genes among parents
The new offspring are added to the population.
New offspring
In certain new offspring formed, some of their genes can be subjected to a mutation with a low random probability. This implies that some of the bits in the bit string can be flipped.
Mutation: Before and After
The mutation occurs to maintain diversity within the population and prevent premature convergence.
The algorithm terminates if the population has converged (does not produce offspring which are significantly different from the previous generation). Then it is said that the genetic algorithm has provided a set of solutions to our problem.
The termination condition of a Genetic Algorithm is important in determining when a GA run will end. It has been observed that initially, the GA progresses very fast with better solutions coming in every few iterations, but this tends to saturate in the later stages where the improvements are very small. We usually want a termination condition such that our solution is close to the optimal, at the end of the run.
Usually, we keep one of the following termination conditions −
For example, in a genetic algorithm, we keep a counter which keeps track of the generations for which there has been no improvement in the population. Initially, we set this counter to zero. Each time we don’t generate off-springs who are better than the individuals in the population, we increment the counter.
However, if the fitness of any of the off-springs is better, then we reset the counter to zero. The algorithm terminates when the counter reaches a predetermined value.
Like other parameters of a GA, the termination condition is also highly problem specific and the GA designer should try out various options to see what suits his particular problem the best.
Before beginning a discussion on Genetic Algorithms, it is essential to be familiar with some basic terminology that will be used throughout this tutorial.
For example, consider the 0/1 Knapsack Problem. The Phenotype space consists of solutions that just contain the item numbers of the items to be picked.
However, in the genotype space, it can be represented as a binary string of length n (where n is the number of items). A 0 at position x represents that xth item is picked while a 1 represents the reverse. This is a case where genotype and phenotype spaces are different.
The basic structure of a GA is as follows −
We start with an initial population (which may be generated at random or seeded by other heuristics), select parents from this population for mating. Apply crossover and mutation operators on the parents to generate new off-springs. And finally, these off-springs replace the existing individuals in the population, and the process repeats. In this way, genetic algorithms try to mimic human evolution to some extent.
f = ga(func, nvars, A, b, [ ], [ ], lb, ub, nonlcon, Intcon, options)
[x, fval] = ga(func, nvars, A, b, Aeq, Beq, lb, ub, nonlcon, Intcon, options)
x = optimum solution
func = fitness function.
nvars = number of variables,
A, b = Linear inequality constraints
Aeq, Beq = Linear equality constraints
lb = Lower bound(limit)
ub = Upper bound(limit)
nonlcon = Use for nonlinear constraints
Intcon = to get solution in integer
options = To set population size minimize with non-default options
BODY OF THE CONTENT:
f(x,y)=f1,xf2,xf1,yf2,yf(x,y)=f1,xf2,xf1,yf2,y
f1,x=[sin(5.1πx+0.5)]6f1,x=[sin(5.1πx+0.5)]6
f1,y=[sin(5.1πy+0.5)]6f1,y=[sin(5.1πy+0.5)]6
f2,x=exp[-4ln(2)(x-0.0667)20.64]f2,x=exp[−4ln(2)(x−0.0667)20.64]
f2,x=exp[-4ln(2)(x-0.0667)20.64]f2,x=exp[−4ln(2)(x−0.0667)20.64]
function[fxy]=stalagmite(a)
x=a(1,1);
y=a(1,2);
f1x=(sin(5.1*pi*x+0.5))^6;
f1y=(sin(5.1*pi*y+0.5))^6;
f2x=exp(-4*log(2)*((x-0.0667)^2)/0.64);
f2y=exp(-4*log(2)*((y-0.0667)^2)/0.64);
fxy=(-1)*f1x*f2x*f1y*f2y;
end
1. Plotting the Stalagmite Surface
clear all
close all
clc
%Defining our search place.
x=linspace(0,0.6,150); %Defining our search place in X-Direction.
y=linspace(0,0.6,150); %Defining our search place in Y-Direction.
%Creating a two-dimensional mesh grid.
[xx yy]=meshgrid(x,y);
%Evaluating the stalagmite function
for i=1:length(xx) %Using 'for' loop to collect data for every X-Value.
for j=1:length(yy) %Using 'for' loop to collect data for every Y-Value.
inputvector(1)=xx(i,j); %Collecting X-Value for Stalagmite Function.
inputvector(2)=yy(i,j); %Collecting Y-Value for Stalagmite Function.
f(i,j)=stalagmite(inputvector); %Collecting Outputs of Stalagmite Function.
end %Ending 'for' function.
end %Ending 'for' function.
%Plotting Stalagmite Function
figure(1) %Figure for Plotting Stalagmite function in 3-D.
surfc(xx,yy,-f) %Generating a Surface for Plotting Stalagmite function in 3-D.
xlabel('x-axis') %Labelling the X-Axis.
ylabel('y-axis') %Labelling the Y-Axis.
zlabel('z-axis') %Labelling the Z-Axis.
shading interp %Removing grid-lines from surface.
2. Case(1)-Statistical Behaviour Without Limits/Boundation
%Statistical Behaviour with Unbounded Region.
tic %Start Stopwatch Timer.
for i=1:50 %Starting For-Loop to do 50 Iterations.
[inputs1, fopt_ubr(i)]=ga(@stalagmite,2); %Using inbuild ga(Genetic-Algorithm) to Find Optimum value of function stalagmite.
x_opt_ubr(i)=inputs1(1,1); %Collecting X-Value of Optimum value of function stalagmite for every iteration.
y_opt_ubr(i)=inputs1(1,2); %Collecting Y-Value of Optimum value of function stalagmite for every iteration.
end %Ending 'for' function.
study_time_1=toc; %Stop Stopwatch Timer and collect time data.
figure(2) %Figure for Plotting Stalagmite function in 3-D with Unbounded Region.
txt1=sprintf('Study Time %1.4f sec',study_time_1); %Text to display time taken to evaluate Optimization.
sgtitle(txt1) %Display time taken to evaluate Optimization.
%Creating 2*1 plots in Single Figure.
subplot(2,1,1) %Defining 1st Plot from 2*1 Plots in single figure.
surfc(xx,yy,-f) %Generating a surface for Plotting Stalagmite function in 3-D with Unbounded Region.
xlabel('x-axis') %Labelling the X-Axis.
ylabel('y-axis') %Labelling the Y-Axis.
zlabel('z-axis') %Labelling the Z-Axis.
grid on %Display Axis Grid Lines.
shading interp %Removing grid-lines from the surface.
hold on %Command to add multiple Plots in a single Plot.
plot3(x_opt_ubr,y_opt_ubr,-fopt_ubr,'-o','markerfacecolor','r'); %Plotting the Optimized points on the Surface/3-D Space.
subplot(2,1,2) %Defining 2nd Plot from 2*1 Plots in single figure.
plot(-fopt_ubr); %Plotting Optimum Value vs no. of Iterations.
xlabel('Iterations') %Labelling the X-Axis.
ylabel('Function Maximum Value') %Labelling the Y-Axis.
3. Case(2)-Statistical Behaviour Limits/Boundation
%Statistical Behaviour with Bounded Region.
tic %Start Stopwatch Timer.
for i=1:50 %Starting For-Loop to do 50 Iterations.
[inputs1, fopt_br(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6]); %Using inbuild ga(Genetic-Algorithm) to Find Optimum value of function stalagmite.
x_opt_br(i)=inputs1(1,1); %Collecting X-Value of Optimum value of function stalagmite for every iteration.
y_opt_br(i)=inputs1(1,2); %Collecting Y-Value of Optimum value of function stalagmite for every iteration.
end %Ending 'for' function.
study_time_2=toc; %Stop Stopwatch Timer and collect time data.
figure(3) %Figure for Plotting Stalagmite function in 3-D with Bounded Region.
txt2=sprintf('Study Time %1.4f sec',study_time_2); %Text to display time taken to evaluate Optimization.
sgtitle(txt2) %Display time taken to evaluate Optimization.
%Creating 2*1 plots in Single Figure.
subplot(2,1,1) %Defining 1st Plot from 2*1 Plots in single figure.
surfc(xx,yy,-f) %Generating a surface for Plotting Stalagmite function in 3-D with Bounded Region.
xlabel('x-axis') %Labelling the X-Axis.
ylabel('y-axis') %Labelling the Y-Axis.
zlabel('z-axis') %Labelling the Z-Axis.
grid on %Display Axis Grid Lines.
shading interp %Removing grid-lines from the surface.
hold on %Command to add multiple Plots in a single Plot.
plot3(x_opt_br,y_opt_br,-fopt_br,'-o','markerfacecolor','r'); %Plotting the Optimized points on the Surface/3-D Space.
subplot(2,1,2) %Defining 2nd Plot from 2*1 Plots in single figure.
plot(-fopt_br); %Plotting Optimum Value vs no. of Iterations.
xlabel('Iterations') %Labelling the X-Axis.
ylabel('Function Maximum Value') %Labelling the Y-Axis.
4. Case(3)-Statistical Behaviour Limits/Boundation with Increase in Population Size.
%Statistical Behaviour with Bounded Region with Increase in Size of Population.
options=optimoptions('ga','populationsize',300); %Increasing the Size of Population by using optimoptions inbuild function.
tic %Start Stopwatch Timer.
for i=1:50 %Starting For-Loop to do 50 Iterations.
[inputs1, fopt_p(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options); %Using inbuild ga(Genetic-Algorithm) to Find Optimum value of function stalagmite.
x_opt_p(i)=inputs1(1,1); %Collecting X-Value of Optimum value of function stalagmite for every iteration.
y_opt_p(i)=inputs1(1,2); %Collecting Y-Value of Optimum value of function stalagmite for every iteration.
end %Ending 'for' function.
study_time_3=toc; %Stop Stopwatch Timer and collect time data.
figure(4) %Figure for Plotting Stalagmite function in 3-D with Bounded Region with Increase in Size of Population.
txt3=sprintf('Study Time %1.4f sec',study_time_3); %Text to display time taken to evaluate Optimization.
sgtitle(txt3) %Display time taken to evaluate Optimization.
%Creating 2*1 plots in Single Figure.
subplot(2,1,1) %Defining 1st Plot from 2*1 Plots in single figure.
surfc(xx,yy,-f) %Generating a surface for Plotting Stalagmite function in 3-D with Bounded Region with Increase in Size of Population.
xlabel('x-axis') %Labelling the X-Axis.
ylabel('y-axis') %Labelling the Y-Axis.
zlabel('z-axis') %Labelling the Z-Axis.
grid on %Display Axis Grid Lines.
shading interp %Removing grid-lines from the surface.
hold on %Command to add multiple Plots in a single Plot.
plot3(x_opt_p,y_opt_p,-fopt_p,'-o','markerfacecolor','r'); %Plotting the Optimized points on the Surface/3-D Space.
subplot(2,1,2) %Defining 2nd Plot from 2*1 Plots in single figure.
plot(-fopt_p); %Plotting Optimum Value vs no. of Iterations.
xlabel('Iterations') %Labelling the X-Axis.
ylabel('Function Maximum Value') %Labelling the Y-Axis.
Optimum_Solution=sprintf('Maximum value is %1.4f at %1.4f,%1.4f',-fopt_p(1),x_opt_p(1),y_opt_p(1)); %Text to display Optimized Solution/Value.
title(Optimum_Solution) %Display Optimized Solution/Value.
5. Stopping Criteria & Best Fit.
%Stopping Criteria
options=optimoptions('ga','PlotFcn',{@gaplotstopping,@gaplotbestf},'populationsize',300); %Increasing the Size of Population by using optimoptions inbuild function.
tic %Start Stopwatch Timer.
[inputs1, fopt_s]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options); %Using inbuild ga(Genetic-Algorithm) to Find Optimum value of function stalagmite.
x_opt_s=inputs1(1,1); %Collecting X-Value of Optimum value of function stalagmite for every iteration.
y_opt_s=inputs1(1,2); %Collecting Y-Value of Optimum value of function stalagmite for every iteration.
study_time_4=toc; %Stop Stopwatch Timer and collect time data.
figure(5) %Figure for Plotting Stopping Criteria and Best Fit.
txt4=sprintf('Study Time %1.4f sec',study_time_4); %Text to display time taken to evaluate.
sgtitle(txt4) %Display time taken to evaluate.
% Try to understand the concept of the genetic algorithm by referring to various sources before you attend this challenge. Look out for information regarding the stalagmite function and ways to optimize it.
% Write a code in MATLAB to optimize the stalagmite function and find the global maxima of the function.
% Explain the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
% Make sure that your code gives the same output, even if it is made to run several times.
% Plot graphs for all 3 studies and F maximum vs no. of iterations. Title and axes labels are a must, legends could be shown if necessary.
clear all
close all
clc
%Defining our search place.
x=linspace(0,0.6,150); %Defining our search place in X-Direction.
y=linspace(0,0.6,150); %Defining our search place in Y-Direction.
%Creating a two-dimensional mesh grid.
[xx yy]=meshgrid(x,y);
%Evaluating the stalagmite function
for i=1:length(xx) %Using 'for' loop to collect data for every X-Value.
for j=1:length(yy) %Using 'for' loop to collect data for every Y-Value.
inputvector(1)=xx(i,j); %Collecting X-Value for Stalagmite Function.
inputvector(2)=yy(i,j); %Collecting Y-Value for Stalagmite Function.
f(i,j)=stalagmite(inputvector); %Collecting Outputs of Stalagmite Function.
end %Ending 'for' function.
end %Ending 'for' function.
%Plotting Stalagmite Function
figure(1) %Figure for Plotting Stalagmite function in 3-D.
surfc(xx,yy,-f) %Generating a Surface for Plotting Stalagmite function in 3-D.
xlabel('x-axis') %Labelling the X-Axis.
ylabel('y-axis') %Labelling the Y-Axis.
zlabel('z-axis') %Labelling the Z-Axis.
shading interp %Removing grid-lines from surface.
%Statistical Behaviour with Unbounded Region.
tic %Start Stopwatch Timer.
for i=1:50 %Starting For-Loop to do 50 Iterations.
[inputs1, fopt_ubr(i)]=ga(@stalagmite,2); %Using inbuild ga(Genetic-Algorithm) to Find Optimum value of function stalagmite.
x_opt_ubr(i)=inputs1(1,1); %Collecting X-Value of Optimum value of function stalagmite for every iteration.
y_opt_ubr(i)=inputs1(1,2); %Collecting Y-Value of Optimum value of function stalagmite for every iteration.
end %Ending 'for' function.
study_time_1=toc; %Stop Stopwatch Timer and collect time data.
figure(2) %Figure for Plotting Stalagmite function in 3-D with Unbounded Region.
txt1=sprintf('Study Time %1.4f sec',study_time_1); %Text to display time taken to evaluate Optimization.
sgtitle(txt1) %Display time taken to evaluate Optimization.
%Creating 2*1 plots in Single Figure.
subplot(2,1,1) %Defining 1st Plot from 2*1 Plots in single figure.
surfc(xx,yy,-f) %Generating a surface for Plotting Stalagmite function in 3-D with Unbounded Region.
xlabel('x-axis') %Labelling the X-Axis.
ylabel('y-axis') %Labelling the Y-Axis.
zlabel('z-axis') %Labelling the Z-Axis.
grid on %Display Axis Grid Lines.
shading interp %Removing grid-lines from the surface.
hold on %Command to add multiple Plots in a single Plot.
plot3(x_opt_ubr,y_opt_ubr,-fopt_ubr,'-o','markerfacecolor','r'); %Plotting the Optimized points on the Surface/3-D Space.
subplot(2,1,2) %Defining 2nd Plot from 2*1 Plots in single figure.
plot(-fopt_ubr); %Plotting Optimum Value vs no. of Iterations.
xlabel('Iterations') %Labelling the X-Axis.
ylabel('Function Maximum Value') %Labelling the Y-Axis.
%Statistical Behaviour with Bounded Region.
tic %Start Stopwatch Timer.
for i=1:50 %Starting For-Loop to do 50 Iterations.
[inputs1, fopt_br(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6]); %Using inbuild ga(Genetic-Algorithm) to Find Optimum value of function stalagmite.
x_opt_br(i)=inputs1(1,1); %Collecting X-Value of Optimum value of function stalagmite for every iteration.
y_opt_br(i)=inputs1(1,2); %Collecting Y-Value of Optimum value of function stalagmite for every iteration.
end %Ending 'for' function.
study_time_2=toc; %Stop Stopwatch Timer and collect time data.
figure(3) %Figure for Plotting Stalagmite function in 3-D with Bounded Region.
txt2=sprintf('Study Time %1.4f sec',study_time_2); %Text to display time taken to evaluate Optimization.
sgtitle(txt2) %Display time taken to evaluate Optimization.
%Creating 2*1 plots in Single Figure.
subplot(2,1,1) %Defining 1st Plot from 2*1 Plots in single figure.
surfc(xx,yy,-f) %Generating a surface for Plotting Stalagmite function in 3-D with Bounded Region.
xlabel('x-axis') %Labelling the X-Axis.
ylabel('y-axis') %Labelling the Y-Axis.
zlabel('z-axis') %Labelling the Z-Axis.
grid on %Display Axis Grid Lines.
shading interp %Removing grid-lines from the surface.
hold on %Command to add multiple Plots in a single Plot.
plot3(x_opt_br,y_opt_br,-fopt_br,'-o','markerfacecolor','r'); %Plotting the Optimized points on the Surface/3-D Space.
subplot(2,1,2) %Defining 2nd Plot from 2*1 Plots in single figure.
plot(-fopt_br); %Plotting Optimum Value vs no. of Iterations.
xlabel('Iterations') %Labelling the X-Axis.
ylabel('Function Maximum Value') %Labelling the Y-Axis.
%Statistical Behaviour with Bounded Region with Increase in Size of Population.
options=optimoptions('ga','populationsize',300); %Increasing the Size of Population by using optimoptions inbuild function.
tic %Start Stopwatch Timer.
for i=1:50 %Starting For-Loop to do 50 Iterations.
[inputs1, fopt_p(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options); %Using inbuild ga(Genetic-Algorithm) to Find Optimum value of function stalagmite.
x_opt_p(i)=inputs1(1,1); %Collecting X-Value of Optimum value of function stalagmite for every iteration.
y_opt_p(i)=inputs1(1,2); %Collecting Y-Value of Optimum value of function stalagmite for every iteration.
end %Ending 'for' function.
study_time_3=toc; %Stop Stopwatch Timer and collect time data.
figure(4) %Figure for Plotting Stalagmite function in 3-D with Bounded Region with Increase in Size of Population.
txt3=sprintf('Study Time %1.4f sec',study_time_3); %Text to display time taken to evaluate Optimization.
sgtitle(txt3) %Display time taken to evaluate Optimization.
%Creating 2*1 plots in Single Figure.
subplot(2,1,1) %Defining 1st Plot from 2*1 Plots in single figure.
surfc(xx,yy,-f) %Generating a surface for Plotting Stalagmite function in 3-D with Bounded Region with Increase in Size of Population.
xlabel('x-axis') %Labelling the X-Axis.
ylabel('y-axis') %Labelling the Y-Axis.
zlabel('z-axis') %Labelling the Z-Axis.
grid on %Display Axis Grid Lines.
shading interp %Removing grid-lines from the surface.
hold on %Command to add multiple Plots in a single Plot.
plot3(x_opt_p,y_opt_p,-fopt_p,'-o','markerfacecolor','r'); %Plotting the Optimized points on the Surface/3-D Space.
subplot(2,1,2) %Defining 2nd Plot from 2*1 Plots in single figure.
plot(-fopt_p); %Plotting Optimum Value vs no. of Iterations.
xlabel('Iterations') %Labelling the X-Axis.
ylabel('Function Maximum Value') %Labelling the Y-Axis.
Optimum_Solution=sprintf('Maximum value is %1.4f at %1.4f,%1.4f',-fopt_p(1),x_opt_p(1),y_opt_p(1)); %Text to display Optimized Solution/Value.
title(Optimum_Solution) %Display Optimized Solution/Value.
%Stopping Criteria
options=optimoptions('ga','PlotFcn',{@gaplotstopping,@gaplotbestf},'populationsize',300); %Increasing the Size of Population by using optimoptions inbuild function.
tic %Start Stopwatch Timer.
[inputs1, fopt_s]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options); %Using inbuild ga(Genetic-Algorithm) to Find Optimum value of function stalagmite.
x_opt_s=inputs1(1,1); %Collecting X-Value of Optimum value of function stalagmite for every iteration.
y_opt_s=inputs1(1,2); %Collecting Y-Value of Optimum value of function stalagmite for every iteration.
study_time_4=toc; %Stop Stopwatch Timer and collect time data.
figure(5) %Figure for Plotting Stopping Criteria and Best Fit.
txt4=sprintf('Study Time %1.4f sec',study_time_4); %Text to display time taken to evaluate.
sgtitle(txt4) %Display time taken to evaluate.
clc %Clearing the Command Window
study_time_1 %Display time taken to evaluate Optimization for Plotting Stalagmite function in 3-D with Unbounded Region.
study_time_2 %Display time taken to evaluate Optimization for Plotting Stalagmite function in 3-D with Bounded Region.
study_time_3 %Display time taken to evaluate Optimization for Plotting Stalagmite function in 3-D with Bounded Region with Increase in Size of Population.
Optimum_Solution %Display Optimized Solution/Value.
% Try to understand the concept of the genetic algorithm by referring to various sources before you attend this challenge. Look out for information regarding the stalagmite function and ways to optimize it.
% Write a code in MATLAB to optimize the stalagmite function and find the global maxima of the function.
% Explain the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
% Make sure that your code gives the same output, even if it is made to run several times.
% Plot graphs for all 3 studies and F maximum vs no. of iterations. Title and axes labels are a must, legends could be shown if necessary.
clear all
close all
clc
%Defining our search place.
x=linspace(0,0.6,150); %Defining our search place in X-Direction.
y=linspace(0,0.6,150); %Defining our search place in Y-Direction.
%Creating a two-dimensional mesh grid.
[xx yy]=meshgrid(x,y);
%Evaluating the stalagmite function
for i=1:length(xx) %Using 'for' loop to collect data for every X-Value.
for j=1:length(yy) %Using 'for' loop to collect data for every Y-Value.
inputvector(1)=xx(i,j); %Collecting X-Value for Stalagmite Function.
inputvector(2)=yy(i,j); %Collecting Y-Value for Stalagmite Function.
f(i,j)=stalagmite(inputvector); %Collecting Outputs of Stalagmite Function.
end %Ending 'for' function.
end %Ending 'for' function.
fval_O=0; %Initial Maximum Value
inputs_O=[0,0]; %Initial Outputs
tic %Start Stopwatch Timer.
for i=1:1000; %Starting For-Loop to do 1000 Iterations.
[inputs, fval(i)]=ga(@stalagmite,2); %Using inbuild ga(Genetic-Algorithm) to Find Optimum value of function stalagmite.
z=-fval; %Collecting the Optimized Maximum Value
if z(i)>fval_O; %Providing condition to check compare Maximum Values.
fval_O=z(i); %Updating Maximum Value.
inputs_O=inputs; %Updating Inputs for Maximum Value.
else fval_O=fval_O; %Updating Maximum Value.
inputs_O=inputs_O; %Updating Inputs for Maximum Value.
end %Ending 'if' function.
end %Ending 'for' function.
studytime=toc %Stop Stopwatch Timer and collect time data.
%Plotting Stalagmite Function
figure(1) %Figure for Plotting Stalagmite function in 3-D.
surfc(xx,yy,-f) %Generating a Surface for Plotting Stalagmite function in 3-D.
xlabel('x-axis') %Labelling the X-Axis.
ylabel('y-axis') %Labelling the Y-Axis.
zlabel('z-axis') %Labelling the Z-Axis.
grid on %Display Axis Grid Lines.
shading interp %Removing grid-lines from surface.
hold on %Command to add multiple Plots in a single Plot.
plot3(inputs_O(1),inputs_O(2),fval_O,'-o','markerfacecolor','r'); %Plotting the Optimized points on the Surface/3-D Space.
txt=sprintf('Study Time %1.4f sec',studytime); %Text to display time taken to evaluate Optimization.
title(txt); %Display time taken to evaluate Optimization.
%Displaying the Solution.
clc %Clearing the Command Window
studytime %Display time taken to evaluate Optimization for Plotting Stalagmite function.
inputs_O %Display Inputs for Maximum Value.
fval_O %Display Maximum Value.
5. CONCLUSION:
6. REFERENCES:
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-3 Challenge: ADVISOR Tool
Introduction to HEV using MATLAB & Simulink Week-3 Challenge: ADVISOR Tool AIM: To simulate the given data and conditions for an EV using Advisor Tool in MATLAB. About ADVISOR ADVISOR, NREL’s Advanced Vehicle Simulator, is a set of model, data, and script text files for use with MATLAB and Simulink. It…
04 Jul 2022 11:04 AM IST
Project -BAJA All Terrain Vehicle (ATV) model
Simulink for Mechanical & Electrical Engineers - Challenges Final Project Aim To study, analyze and make a detailed report on BAJA All Terrain Vehicle (ATV) model using Simulink & compare between its different modes. Objective Prepare a technical report explaining the model properties & comments on the results.…
03 Jun 2021 03:25 AM IST
Week - 4
Simulink for Mechanical & Electrical Engineers Challenges = Week 4 Aim To Make a Simulink model using State-Flow for given questions. Questions & Solution Q1. Implement control logic of a “washing machine” using Stateflow as per given sequence: If the power supply is available, the system gets activated. If the Water supply…
21 May 2021 06:29 PM IST
Week -2
Simulink for Mechanical & Electrical Engineers Challenges = Week 2 Aim To Make a Simulink model of Doorbell using solenoid block. To Use a thermistor to sense the temperature of a heater & turn on or turn off the fan according to temperature. Questions & Solution Q1. Make a Simulink model of Doorbell using…
14 May 2021 12:30 AM IST
Related Courses
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.