You are on page 1of 20


T. Sai Krishna Vikas

Study of Matlab
Dr. Somnath Pan

Study of

DC motor armature
Introduction to Genetic
Study of Genetic Algorithm using


Simulation of DC motor could be simply done in MATLAB. The electrical
and mechanical circuit parameters are fed to the software. Then the transfer
function is written. Once the transfer function is properly written, by using the
syntax, LTIview we could get the graph of the performance of motor. This, we
could simulate the motor on software rather than testing the motor directly with
different parameters for finding desired parameters.

For controlling the speed of the motor by varying the applied voltage. The
speed vs time graph of feeding the software with a transfer function obtained by
taking the speed as output and applied voltage as input. And the syntax LTIview
is used.
If the change in speed with other variable, the appropriate transfer function
has to be taken with the desired variable as input.
In this example, Im taking two transfer functions, one with speed as output
and other with position as output, while both of them have applied voltage as input.
>> R=2;

>> L=0.5;
>> Fo=0.1;
>> Kb=0.1;
>> Kt=0.2;
>> J=0.02;
>> h=tf(Kt,[L*J (R*J)+(Fo*L) (R*Fo)+(Kt*Kb)
-----------------------0.01 s^2 + 0.09 s + 0.22

Continuous-time transfer function.

>> h1=tf(Kt,[L*J (R*J)+(Fo*L) (R*Fo)+(Kt*Kb) 0])

h1 =
---------------------------0.01 s^3 + 0.09 s^2 + 0.22 s
Continuous-time transfer function.
>> ltiview('step',h,h1)
The blue line in the plot represents the function h(speed as output), while the green line represents the function
h1(position as output).

Even bode plot could be plotted. By using the syntax: BODE

>> bode(h,h1)

As early as 1962, John Holland's work on adaptive systems laid the
foundation for later developments.
By the 1975, the publication of the book
Early to mid-1980s, genetic algorithms were being applied to a broad range of
In 1992 John Koza has used genetic algorithm to evolve programs to perform
certain tasks. He called his method "genetic programming"(GP).
What is a Genetic Algorithm??
A genetic algorithm (or GA) is a search technique used in computing to find true
or approximate solutions to optimization and search problems.

(GA)s are categorized as global search heuristics.

(GA)s are a particular class of evolutionary algorithms that use techniques
inspired by evolutionary biology such as inheritance, mutation, selection, and
crossover (also called recombination).
The evolution usually starts from a population of randomly generated individuals
and happens in generations.
In each generation, the fitness of every individual in the population is evaluated,
multiple individuals are selected from the current population (based on their
fitness), and modified to form a new population.
The new population is used in the next iteration of the algorithm.
The algorithm terminates when either a maximum number of generations has
been produced, or a satisfactory fitness level has been reached for the population.

Individual - Any possible solution
Population - Group of all individuals

Fitness Target function that we are optimizing (each individual has a


Trait - Possible aspect (features) of an individual.

Genome collection of all chromosomes (traits) for an individual.


Start with a large population of randomly generated attempted
solutions to a problem
Repeatedly do the following:
Evaluate each of the attempted solutions
(probabilistically) keep a subset of the best solutions
Use these solutions to generate a new population
Quit when you have a satisfactory solution (or you run out of time)

Suppose we want to maximize the number of ones in a string of l
binary digits
Is it a trivial problem?
It may seem so because we know the answer in advance
However, we can think of it as maximizing the number of correct answers,
each encoded by 1, to l yes/no difficult questions.
An individual is encoded (naturally) as a string of l binary digits
The fitness f of a candidate solution to the MAXONE problem is the number of
ones in its genetic code
We start with a population of n random strings. Suppose that l = 10 and n = 6.
We toss a fair coin 60 times and get the following initial population:
s1 = 1111010101 f (s1) = 7
s2 = 0111000101 f (s2) = 5
s3 = 1110110101 f (s3) = 7
s4 = 0100010011 f (s4) = 4
s5 = 1110111101 f (s5) = 8
s6 = 0100110000 f (s6) = 3

We randomly (using a biased coin) select a subset of the individuals based on their
Suppose that, after performing selection, we get the following population:
s1` = 1111010101 (s1)
s2` = 1110110101 (s3)
s3` = 1110111101 (s5)

s4` = 0111000101 (s2)

s5` = 0100010011 (s4)
s6` = 1110111101 (s5)
Next we mate strings for crossover. For each couple we first decide (using some
pre-defined probability, for instance 0.6) whether to actually perform the
crossover or not
If we decide to actually perform crossover, we randomly extract the crossover
points, for instance 2 and 5
Crossover result:
Before crossover:
s1` = 1111010101 s2` = 1110110101
After crossover:
s1``= 1110110101 s2`` = 1111010101

The final step is to apply random mutations: for each bit that we are to copy to the
new population we allow a small probability of error (for instance 0.1)

Initial strings

After mutating

s1`` = 1110110101

s1``` = 1110100101

s2`` = 1111010101

s2``` = 1111110100

s3`` = 1110111101

s3``` = 1110101111

s4`` = 0111000101

s4``` = 0111000101

s5`` = 0100011101

s5``` = 0100011101

s6`` = 1110110011

s6``` = 1110110001

And now, iterate

In one generation, the total population fitness changed from 34 to 37, thus
improved by ~9%

At this point, we go through the same process all over again, until a stopping
criterion is met

There are many different strategies to select the individuals to be copied over into
the next
Methods of Selection

Roulette-wheel selection.
Elitist selection.
Fitness-proportionate selection.
Scaling selection.
Rank selection.

Genetic algorithm in
MATLAB has the option of solving problems using different optimization
tools like: bintprog, fgoalattain, fminbnd, fmincon, fminimax, fminsearch,
fminunc, fseminf, fsolve, fzero, and Genetic Algorithm procedures. MATLAB
offers syntaxes as well as tool box for using GA. Every parameter related to the
GA while solving the problem could be set customized, and desired values can be
set. Thought it is well known for its simplicity, GAs can be made even more
effective by and can be made apt for solving various problems by placing suitable
values for parameters like Population, Fitness scaling, Selection, Reproduction,
Mutation, Crossover, Migration, Constraint parameters, Hybrid function, Stopping
criteria, Plot functions, Output function, Display to command window, User
function evaluation.

population of
the genetic
algorithm could
be set as desired.
It sould be noted
that high value
of initial
poplation though
helpful for more
solution, it leads
to too many
calculations. While
low population
leads to very low
accurate solution.

Though the initilization of population is called as random when it comes to

coputer it is not. Because computer runs on progams, it can initialize random
values on its own. Selection option is useful for defining the method by which the
chromozomes for the population should be selected.

Sometimes the
fittest solution of
one generation
might be the best solution. By blindly involving it in crossover there may be
wastage of
solution. So to stop
the loss of such
solutions, the
fittest solution in
each generation is
kept aside from crossover. The number of chromosomes that should be spared from
crossover is selected by elite count.



There are many reason for GA to come to end. We can set few of the
parameters that involve the halting of Genetic Algorithm. We can limit the number
of generations,
time, minimum
fitness required.

Some parameters of the Genetic Algorithm could be plotted by ticking the

plot option. Not just the result but also these parameters are helpful for the analysis
of the result. By observing this data one could get an idea about the status of the
GA and the status
of the result.





For finding the minimum value of a function:
The function should be written in the editor as a function initializing .m file.
The .m file has to be saved in .m files folder, or else the program will not
Open the optimizing toolbox.
Set the solver as genetic algorithm(GA).
The number of variables, lower bound, upper bound have to be
compulsorily filled.
Change other parameters in options if necessary.
And click the start button.
The GA stops and displays the result, when the process comes to a halt.
There could be various reasons for the process to come to a halt.
The value of the function at the result and the reason for the halting of the
process are shown in a dialog.
While the value of the final point comes beneath it.


Feed the software with the function through editor, whose minimum value
has to be identified. Here the function is:
Y = x^2
By setting different values of the GA parameters solution is found out.


Results are displayed in dialogs on the left bottom corner.


The solution obtained is 0. While the value of the function at final point is
4.33403588142492E-10, and the reason for halting is average change in the fitness
value less than options.
Or the simply the minimum of the function could be found by using the syntax
>> [x,fval,exitflag]=ga(@test,1)
Optimization terminated: average change in the fitness value less than

fval =
exitflag =
@test is our function. Which should be given through editor.
1 is the number of variables.
x is the indicates the final point.
Fval is the value of the value of function at x.
Exitflag is the reason for the halting of the process.