Evaluating Stalagmite Function Using Genetic Algorithm
AIM:
To write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
STALAGMITE:
A stalagmite is a type of rock formation that rises from the floor of a cave due to the accumulation of material deposited on the floor from ceiling drippings. Stalagmites are typically composed of calcium carbonate, but may consist of lava, mud, peat, pitch, sand, sinter and amberat (crystallized urine of pack rats).
Since our function gives us a shape of a stalagmite it is named as stalagmite function. Stalagmite function is a mathematical model that helps us to generate minimas and maximas. It is 3-Dimensional function, and it is designed to generate local maximas.
THEORY OF GENETIC ALGORITHM:
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 in order to produce offspring of the next generation.
In computer science and operations research, a genetic algorithm (GA) is a metaheuristic inspired by the process of natural selection that belongs to the larger class of evolutionary algorithms (EA). Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems by relying on biologically inspired operators such as mutation, crossover and selection. John Holland introduced genetic algorithms in 1960 based on the concept of Darwin’s theory of evolution, and his student David E. Goldberg further extended GA in 1989.

FIGURE.1
NOTION OF NATURAL SELECTION
The process of natural selection starts with the selection of 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 at the end, a generation with the fittest individuals will be found.
This notion can be applied for a search problem. We consider a set of solutions for a problem and select the set of best ones out of them.
Five phases are considered in a genetic algorithm.
- Initial population
- Fitness function
- Selection
- Crossover
- Mutation
1) INITIAL POPULATION:
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 (string of 1s and 0s). We say that we encode the genes in a chromosome.

FIGURE.2 : Population, Chromosomes and Genes
2) FITNESS FUNCTION:
The fitness function determines how 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.
3) SELECTION:
The idea of 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 chance to be selected for reproduction.
4) CROSSOVER:
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.

FIGURE.3 : Crossover point
Offspring are created by exchanging the genes of parents among themselves until the crossover point is reached.

FIGURE.4 : Exchanging genes among parents
The new offspring are added to the population.

FIGURE.5 : New offspring
5) MUTATION:
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.

FIGURE.6 : Mutation: Before and After
Mutation occurs to maintain diversity within the population and prevent premature convergence.
TERMINATION:
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.
Using this approach of Genetic Algorithm we are going optimize the stalagmite function and find the optimized value of this stalagmite function.
GOVERNING EQUATIONS:
To generate a stalagmite following functions are used,
`f_1(x)=[sin(5.1*pi*x+0.5)]^6`
`f_1(y)=[sin(5.1*pi*y+0.5)]^6`
`f_2(x)=exp^((-4*log2*(x-0.0667)^2)/0.64)`
`f_2(y)=exp^((-4*log2*(y-0.0667)^2)/0.64`
`f(x,y)=f_1(x)*f_1(y)*f_2(x)*f_2(y)`
Maximizing f(x,y) is the same as minimizing -f(x,y)
PROGRAM:
CODE FOR FINDING GLOBAL MAXIMA:
close all
clear all
clc
%Defining Our Search Space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%Creating 2-D Array
[xx ,yy] = meshgrid(x,y);
%Calculating Fitting Function
for i=1:length(xx);
for j=1:length(yy);
input(1) = xx(i,j);
input(2) = yy(i,j);
f(i,j)= stalagmite_func(input);
end
end
%Converting Fitting Function to change graph upwards
F=-f;
%Plotting Stalagmite Function
surface(xx,yy,F)
shading interp
hold on;
%Performing the Genetic Algorithm
[output , fval]=ga(@stalagmite_func,2,[],[],[],[],[0,0],[0.6,0.6])
%Plotting Global Maxima
x_max= output(:,1);
y_max= output(:,2);
f_max= -fval;
plot3(x_max,y_max,f_max,'marker','o','markersize',5,'markerfacecolor','r');
xlabel('X-value');
ylabel('Y-value');
title('GLOBAL MAXIMA');
legend('STALAGMITE','GLOBAL MAXIMA')
EXPLANATION:
- Here, first the commands ‘close all', 'clear all' , 'clc', are used to close any previous plots and to clear the workspace and command window respectively.
- Then the values of ‘x’ and ‘y’ are entered using ‘linspace()’ command. This is our search space, that means for which value of x and y that are have defined in ‘linspace()’ are we getting an optimized value for the stalagmite function.
- Now using ‘meshgrid’ command these 1-D array is converted into 2-D arrays. These 2-dimensional array are named as ‘xx’ and ‘yy’ in the code.
- Now to calculate the stalagmite function we have used ‘FOR LOOPS’, and in these loops we have defined ‘i’ and ‘j’ as each value of ‘x’ and ‘y’ of 2-dimensional array.
- Now for each value of ‘x’ and ‘y’ we are creating an array named as ‘input’ and thus this ‘input’ will be a row vector i.e. two column matrix.
- Now first column of input vector are defined as values of ‘xx’ matrix values and second column of input vector are defined as ‘yy’ matrix values.
- Now using these values the stalagmite function is calculated. Function for the stalagmite is entered separately as a function code using the equations as mentioned above.
FUNCTION CODE:
function[f] = stalagmite_func(input);
x=input(1);
y=input(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));
f = -[f1_x.*f1_y.*f2_x.*f2_y];
end
- Now this function takes the ‘input’ values as the input for the function and the returns the value of ‘f’.
- Now this above function is called in the ‘FOR LOOP’ for different values of ‘x’ and ‘y’ and different values of ‘f’ are calculated.
- These two ‘FOR LOOPS’ generates the data for the plot that we need to generate a stalagmite graph.
- Then ‘surface()’ command is used whichcreates a primitive, three-dimensional surface plot. If the command is entered as ‘surface(X,Y,Z)’ then the function plots the values in matrix Z as heights above a grid in the x-y plane defined by X and Y. The color of the surface varies according to the heights specified by Z. Here in this code (X,Y,Z) are (xx,yy,F) respectively.
- Then ‘shading interp’ command is used to remove the grids that are formed on the plot, which will give us a smooth and better looking plot.
- Now to optimize the above function we used a inbuilt Matlab command called the ‘ga()’. This command performs the genetic algorithm on the function which we have defined and gives us output as optimized results. It will automatically perform the genetic algorithm calculations and give us the optimized solution for the fitting function as output.
- Syntax for this ‘ga()’ command is as follows:
x = ga(fun , nvars) finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub) defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub. (Set A=[ ] and b=[ ] if no linear inequalities exist.) (Set Aeq=[ ] and beq=[ ] if no linear equalities exist.)
[x , fval] = ga(___), for any previous input arguments, also returns fval, the value of the fitness function at x.
- The above commands are the syntaxes for the ‘ga()’ command used in the above code. Now the first syntax is the basic genetic algorithm command where the ‘ga()’ takes the inputs as fitting equation that has to be evaluated and number of variables present in the fitting function. So each time this command is run it takes random values for the function variables initially and then uses those values for further calculations.
- The next syntax of ‘ga()’ allows us to provide the bounds to the ‘ga()’command that will limit it, to choose the values within the specified values for the function variables. We can also give any linear inequalities and equalities as inputs and ‘ga()’ will calculate according to these inequalities and equalities.
- The third syntax of ‘ga()’ gives the value of ‘x’ as well as the function’s minimum value. So we can get value of ‘x’ where the function is minimum and that minimum value of function is also calculated and given as output.
- Now the drawback of ‘ga()’ command is ,it always calculates the value of ‘x’ where the function is minimum, thus giving us the global minima. But we need to calculate global maxima in our code, so in order to achieve that we are minimizing the fitting function with a ‘- symbol’, because Maximizing f(x,y) is the same as minimizing -f(x,y).
- That is the reason we have put ‘- symbol’ in the equation of ‘f’ in the function code.
- Now since the value of ‘f’ in the function code will be negative, so our plot will we downwards, in order to make it upwards, after the ‘FOR LOOP’ the values of ‘f’ are again multiplied with ‘- symbol’ thus giving us positive values and making the plot upwards. These new values of ‘f’ are assigned to a variable ‘F’. This is only done to get the get the graph upwards so that we get a better look. It won’t affect the ‘ga()’ command and its calculations.
- Now after the ‘shading interp’ command, the ‘ga()’ is entered in the code, and the output of ‘ga()’ are mentioned as ‘outputs’ and ‘fval’ i.e. for which value of ‘x’ and ‘y’ which are ‘outputs’ in the code, the function value which is ‘fval’ in the code is minimum and it will give value of this funtion’s minimum value also.
- Then for the inputs of ‘ga()’, the fitting function which is named as ‘stalagmite_func’ which is called with a function handle, and numbers of variables in the fitting code are entered and the bounded values(limits of ‘x’ and ‘y’) are entered so that ‘ga()’ only gives value between these bounds are entered.
- Since we have no linear inequalities and equalities so it is just eneterd ‘[ ]’ in the ‘ga()’ command.

FIGURE.7
- Now once we run the code we will get a row vector for ‘outputs’ and value of ‘fval’ in command window. First value of ‘outputs’ vector is the value of ‘x’ and second value of ‘outputs’ vector is the value of ‘y’ for which value of fitting function is minimum.
- Now to plot the global maxima point in the graph we have defined ‘x’ value as ‘x_max’ which is first value of ‘outputs’ vector and ‘y’ value as ‘y_max’ which is second value of ‘outputs’ vector.
- As mentioned above, Maximizing f(x,y) is the same as minimizing -f(x,y), so since we have minimized the function ‘-f’ , so value of ‘fval’ is the global maxima of the function ‘+f’ . Since our graph is upward so value of ‘fval’ is multiplied by ‘- symbol’ and assigned to a variable f_max.
- Then ‘plot3()’ command is used as we need to plot point in a 3-d graph, global maxima is plotted using ‘x_max’ , ‘y_max’ and ‘f_max’ .
|
FIGURE.8 |
FIGURE.9 |
- In Figure.9 we can observe that there are many local maximas in the plot, and the point gives us the global maxima of the function.
To demonstrate how ‘ga()’ works we have done 3 type of studies and coded it. These studies show us how to use genetic algorithm to obtain best results.
STUDY 1 : STATISTICAL BEHAVIOUR
CODE:
%Study 1 - Statistical behaviour
close all
clear all
clc
%1-D Array
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
%Creating 2-D Array
[xx ,yy] = meshgrid(x,y);
%Calculating Fitting Function
for i=1:length(xx);
for j=1:length(yy);
input(1) = xx(i,j);
input(2) = yy(i,j);
f(i,j)= stalagmite_func(input);
end
end
F = -f;
%Performing the Genetic Algorithm
tic
for l=1:num_cases
[inputs, fopt(l)] = ga(@stalagmite_func, 2);
xopt(l)=inputs(1);
yopt(l)=inputs(2);
end
Fopt= - fopt;
study1_time =toc
%Plotting Global Maxima And function values
figure(1)
subplot(2,1,1)
hold on
surface(x,y,F)
shading interp
plot3(xopt,yopt,Fopt,'-o','markersize',5,'markerfacecolor','r')
xlabel('X values')
ylabel('Y values')
title('Unbounded Inputs')
legend('Stalagmite','Global Maxima')
subplot(2,1,2)
plot(Fopt)
title('VARIATION IN F ')
xlabel('Iterations')
ylabel('Function Maximum')
legend('Change in F values')
EXPLANATION:
- This study shows us the statistical behaviour of genetic algorithm in MATLAB.
- The code is similar to that of above code, the only change here that we are performing ‘ga()’ for some number of iterations and see how the values are changing for each iteration.
- In the above code ,we have defined ‘x’ and ‘y’ values and then we have defined number of times the ‘ga()’ has to be performed as a variable ‘num_cases’ and we have given it a value.
- Then using ‘x’ and ‘y’ we have created 2-d array using ‘meshgrid()’ command.
- Now using FOR LOOP values of ‘f’ are determined, it is same as the above code. Function code used here is also the same as the above code used in this FOR LOOP.
- Now since the values of ‘f’ are negative and we will get downward graph, so in order to get a upward graph ,we have multiplied ‘f’ with ‘-symbol’ and assigned it to a variable ‘F’.
- Now here another FOR LOOP is used to evaluate ‘ga()’ for 50 iterations. In this FOR LOOP we have defined ‘l’ as the number of iterations and then we have entered the ‘ga()’ function with a normal syntax that we have mentioned in previous code. Only the fitting function and number of variables are entered as inputs for the ‘ga()’ function.
- Then the first value of inputs is assigned to a variable ‘xopt’ and the second value of inputs is assigned to a variable ‘yopt’. Every time the FOR LOOP is executed it gives new values of ‘fopt’ , ‘xopt’ and ‘yopt’.
- Then the ‘fopt’ values are multiplied with ‘-symbol’ and assigned to variable ‘Fopt’. This is done in order to point the global maxima since our graph will be upwards. This is only done for plotting purpose.
- Now for this FOR LOOP there is a command used ‘tic and tok’, this commands helps to know the time taken to calculate this FOR LOOP, ‘tic’ is entered at the start for FOR LOOP and ‘toc’ is entered after the ending of FOR LOOP and for ‘toc’ a variable is assigned so the time is calculated under that variable.
- Then a ‘figure()’ command is entered to open a figure window, then a ‘subplot()’ command is used to open the first graph in the figure window. Subplot( m , n , p ) divides the current figure into an m -by- n grid and creates axes in the position specified by p. Accordingly the values are entered in the code for ‘subplot()’.
- Then plotting is done using ‘surface()’ command and then ‘shading interp’ is entered in the code to remove grids on the graph in the this first subplot.
- Then ‘plot3()’ is used to generate a point in a 3-d graph, this command is used to plot the global maxima point. This helps us how we are getting the global maxima each and every time ‘ga()’ function is run.
- Then ‘title()’ command is used to give a title to the first plot, ‘xlabel’ and ‘ylabel’ commands are entered to name x-axis and y-axis respectively.
- Next, second subplot command is entered to get second graph between the optimized ‘f’ values obtained from ‘ga()’ function and the number of iterations that ‘ga()’is run.
- Then ‘plot()’ command is used to plot the graph between ‘Fopt’ values and number of iterations.
- Thus in the same figure window we will get two graphs using ‘subplot()’ command.

FIGURE.10
- If we run the code, we will get above results, here we can observe that we have got different optimized values of function ‘f’ each the ‘ga()’ was run. We have got 50 different value of ‘f’ when 50 times ‘ga()’ was run. So in this way we can’t use ‘ga()’ for our calculation in reality.
- We can observe the second graph in the figure that how values of ‘f’ are changing for each iteration.
- Genetic algorithm always takes random population samples and then performs the mutation and cross-over process to get a new generation. Due to this random inputs taken by genetic algorithm we get different values when we run ‘ga()’ each time.Here genetic algorithm does not know what is our search space i.e. limits of ‘x’ and ‘y’.
- To get better optimised values of ‘f’ that are within the limits of ‘x’ and ‘y’ defined by us, we have used a modified ‘ga()’ syntax, which we will use in study 2.

FIGURE.11
- Now the ‘tic and toc’ command entered in the code has calculated the time taken to finish whole FOR LOOP of the ‘ga()’ function and given us the time in the command window. It has taken nearly 5 seconds for ‘ga’ to generate the solution.
STUDY 2: STATISTICAL BEHAVIOUR WITH BOUNDED INPUTS
CODE:
%Study 2 - Statistical behaviour with bounded conditions
close all
clear all
clc
%1-D Array
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
%Creating 2-D Array
[xx ,yy] = meshgrid(x,y);
%Calculating Fitting Function
for i=1:length(xx);
for j=1:length(yy);
input(1) = xx(i,j);
input(2) = yy(i,j);
f(i,j)= stalagmite_func(input);
end
end
F=-f;
%Performing the Genetic Algorithm
tic
for l=1:num_cases
[inputs, fopt(l)] = ga(@stalagmite_func, 2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(l)=inputs(1);
yopt(l)= inputs(2);
end
Fopt = -fopt;
study2_time =toc
%Plotting Global Maxima And function values
figure(1)
subplot(2,1,1)
hold on
surface(x,y,F)
shading interp
plot3(xopt, yopt, Fopt,'-o','markersize',5,'markerfacecolor','r')
title('Bounded Inputs')
xlabel('X-values')
ylabel('Y-values')
legend('Stalagmite','Global Maxima')
subplot(2,1,2)
hold on
plot(Fopt)
title('VARIATION IN F ')
xlabel('Iterations')
ylabel('Function Maximum')
legend('Change in F values')
EXPLANATION:
- The above code is same that of previous code, only the change is in the syntax of ‘ga()’ command.
- Now in this code the syntax used for ‘ga()’ command is
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub) defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub. (Set A=[ ] and b=[ ] if no linear inequalities exist.) (Set Aeq=[ ] and beq=[ ] if no linear equalities exist.)
- The above syntax helps us to enter upper and lower bound values as input to get a better solution than previous code. This means that when ‘ga()’ calculates the values of ‘f’, it will display on the that values of ‘f’ that lie between the bounded values of ‘x’ and ‘y’.
- Now it is not compulsory to take values of ‘x’ and ‘y’ values only as your bounded values, these values are taken according to the problem we are working on , sometimes for bounded values to be specified needs some practical experience for some situations. So bounded values are taken according to our problem.
- When we run the code we get the following figure,

FIGURE.12
- In the above figure we can observe that now the graphs are much clear than the previous one and now –ga() predicts that the global maxima is somewhere between three points that are in the first plot.
- When you look at the second plot, we can also observe that our maximum value is between 0.84 to 1.
- But here still we didn’t get a single optimized value i.e. results are yet clear here. To overcome this problem, further we are using modified ‘ga()’ command.

FIGURE.13
- In above figure we can see that time taken by ‘ga’ to calculate, is more, compared to previous study. That means as we have entered more inputs ‘ga’ is taking more time to generate the solution.
STUDY 3: STATISTICAL BEHAVIOUR WITH BOUNDED INPUTS AND INCREASED POPULATION
CODE:
%STUDY 3- STATISTICAL BEHAVIOUR WITH BOUNDED INPUTS AND INCREASED POPULATION
close all
clear all
clc
%1-D Array
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
%Creating 2-D Array
[xx ,yy] = meshgrid(x,y);
%Calculating Fitting Function
for i=1:length(xx);
for j=1:length(yy);
input(1) = xx(i,j);
input(2) = yy(i,j);
f(i,j)= stalagmite_func(input);
end
end
F=-f;
%Study 3 - Increasing iterations
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',600);
%Performing the Genetic Algorithm
tic
for l=1:num_cases
[inputs, fopt(l)] = ga(@stalagmite_func, 2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(l)=inputs(1);
yopt(l)= inputs(2);
end
Fopt = -fopt;
study3_time =toc
%Plotting Global Maxima And function values
figure(1)
subplot(2,1,1)
hold on
surface(x,y,F)
shading interp
plot3(xopt,yopt,Fopt,'o','markersize',5,'markerfacecolor','r')
title('Bounded Inputs And Increasing Population')
xlabel('X-values')
ylabel('Y-values')
legend('Stalagmite','Global Maxima')
subplot(2,1,2)
hold on
plot(Fopt)
title('F Maximum')
xlabel('Iterations')
ylabel('Function Maximum')
legend('Change in F values')
EXPLANATION:
- The code is same that of the previous two codes, the only change is in the ‘ga()’ command and one extra command is added to the code.
- In this code after the fitting function is calculated we have a command called ‘optimoptions()’, the syntax for this command is,
options = optimoptions(SolverName) returns a set of default options for the SolverName solver.
options = optimoptions(SolverName,Name,Value) returns options with the named parameters altered with the specified values.
- This particular code helps us to rewrite the default values of any inbuilt MATLAB function. For the first syntax when we run it we will get all the default values of the solver function which is mentioned in the brackets. Here in our code we have given ‘ga’ in the brackets so this command will return default values of ‘ga’ function in MATLAB.

FIGURE.14
- In the above figure we can see what the first syntax does. Here we can see that the default value for ‘PopuationSize’ is 50. That means, when ‘ga’ takes random samples as inputs,it takes those values from a Population of 50, so the number of options to choose for ‘ga’ is less.
- If we increase this value of ‘PopulationSize’ then ‘ga’ will have more samples to choose and perform mutation and crossover then it can give us more accurate optimized value.
- To change this value we used second syntax of ‘optimoptions()’ i.e. we have defined which solver the values have to changed, then the parameter in that solver which has to be changed and then by what value it has to be replaced.
- Thus by doing this we have changed the ‘PopulationSize’ to 300.
- Then , in the ‘ga()’ function we have used the following syntax to give this change as one of the input,
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options) minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist.)
- Here in the above syntax we can enter all the inputs as that of previous code, additionally we can also give nonlinear constraints and ‘options’ i.e. changes of defaults values as inputs in the ‘ga()’ function.
- Since here we don’t have any nonlinear constraints. We have entered [] in its place and we have entered the ‘options’ as input to tell the ‘ga’ that there is change in the default values.
- Then rest of the code is same as the previous code and when we run this code we get following results.
FIGURE.15
- From the above figure we can observe that by increasing the population size we have got a single optimized value in the first graph. If we increase the population size more further, the value will bw more accurate.
- In the second plot we can observe that maximum value of for many iterations is near to 0.99999985 and only in three iterations ‘ga()’ predicts that values of ‘f’ are less that of other iteration values. So the maximum value is somewhere near to this value.
- This problem can be further rectified by increasing the population size and our graph will be much more clear.

FIGURE.16
- Now we see in the command window that time taken by ‘ga()’ function is more than that for previous codes. So as we increase the inputs ‘ga()’ takes more time to calculate. This is one of the drawback of ‘ga()’ command.
CONCLUSIONS:
- The idea behind the above studies is to observe how the ‘ga’ command works with different inputs and how to obtain a same values for a function even if ‘ga’ is run several times.
- Thus global maxima is found for the stalagmite function.
- Genetic Algorithm process is one of most used method for obtained optimized value.
- Genetic Algorithm is mostly used to find the minimum value of a function, for example to find minimum drag force that will be created by the aerofoil.
- Similar approach as above code can be used to find maximum values using ‘ga’ command.

