Thursday, June 16, 2011

Basics of the Genetic Algorithm in Matlab

The Genetic algorithm is based on the idea of Natural Selection as propounded by Charles Darwin. Study of the Genetic Algorithm serves to important purposes:-
1) It leads to a better understanding of Nature itself.
2) Provides a means for optimizing artificial systems based on the Genetic Algorithm.


The Genetic Algorithm is superior to the other optimization techniques because :-
1) It does not require the existence of the derivative as in the calculus method.
2) It is faster and more stable than enumeration methods and/or random methods like Dynamic Programming, Branch & Bound, Backtracking etc. It's memory requirements are also much less.


The Genetic algorithm requires a fitness function which decides the selection, the reproduction function, mutation & Cross Over.

Working Procedure:-
1)Create a function file giving the fitness function.
2)Create the inequality matrix, the equality matrix, the lower bound matrix, the upper bound matrix.Remember all of these are optional.
3) Multiply by -1 to adjust the inequalities as well as to transform minimization problems to maximization and vice versa.
Let us solve two optimization problems using the Genetic Algorithm:-
1) Minimize z=-x1 + 2x2
subject to the constraints
-x1 + 3x2<10
x1 + x2<6
x1-x2<2
x1>=0 and x2>=0
First of all create the inequalities matrix a
>> a=[-1 3;1 1;1 -1];
>> a

a =

    -1     3
     1     1
     1    -1

>> 

Create matrix b
>> b=[10; 6; 2];
>>
>> b

b =

    10
     6
     2
>> Create the lower bound matrix lb
>> lb=[0;0];
>> lb

lb =

     0
     0

>> 

Now, create the function file abc.m containing the fitness function:-
abc.m
function y=  abc( x)
y=-x(1) + 2*x(2);

end












Call the optimizer using ga as the solver. use empty bracket [] for the optional parts which are not to be given.
>> [x,fval,exitflag] = ga(@abc,2,a,b,[],[],lb)
Optimization terminated: average change in the fitness value less than options.TolFun.

x =

    2.0010         0


fval =

   -2.0010


exitflag =

     1

>>


The answer is x1=2.0010 , x2=0
and the minimized value is -2.0010


Problem No 2
Minimize z= 20x1 + 40 x2
subject to the constraints
36x1 + 6x2 >=108
3x1 + 12x2 >=36
20x1 + 12x2>=100
x1>0 and x2>0
since the constraints are of the >= type , we shall multiply each equation by -1

-36x1 - 6x2 <=-108
-3x1 - 12x2 <=-36
-20x1 - 12x2<=-100


>> a=[-36 -6;-3 -12;-20 -10];
>> a

a =

   -36    -6
    -3   -12
   -20   -10

>> b=[-108;-36;-100];
>> b

b =

  -108
   -36
  -100

>> lb=[0;0];
>> lb

lb =

     0
     0

>> [x,fval,exitflag] = ga(@abc,2,a,b,[],[],lb)
Optimization terminated: average change in the fitness value less than options.TolFun.

x =

    4.0000    1.9999


fval =

  159.9966


exitflag =

     1

>> 

abc.m

function y=  abc( x)
y=20*x(1) + 40*x(2);

end

















Tuesday, June 14, 2011

Solving Equations using Matlab

Equations, Polynomials & Curve Fitting

Algebra has it's origins in the Communal Era of Indian History. The Celebrated book Brāhmasphuṭasiddhānta  of Brahmagupta 598–668 CE is probably the most influential ancient book on the topic. It contains the following contents:-

  • The sum of two positive quantities is positive
  • The sum of two negative quantities is negative
  • The sum of zero and a negative number is negative
  • The sum of zero and a positive number is positive
  • The sum of zero and zero is zero.
  • The sum of a positive and a negative is their difference; or, if they are equal, zero
  • In subtraction, the less is to be taken from the greater, positive from positive
  • In subtraction, the less is to be taken from the greater, negative from negative
  • When the greater however, is subtracted from the less, the difference is reversed
  • When positive is to be subtracted from negative, and negative from positive, they must be added together
  • The product of a negative quantity and a positive quantity is negative
  • The product of a negative quantity and a negative quantity is positive
  • The product of two positive, is positive.
  • Positive divided by positive or negative by negative is positive
  • Positive divided by negative is negative. Negative divided by positive is negative
  • A positive or negative number when divided by zero is a fraction with the zero as denominator
  • Zero divided by a negative or positive number is either zero or is expressed as a fraction with zero as numerator and the finite quantity as denominator
  • Zero divided by zero is zero
His ideas about division by zero were wrong, however it was the earliest attempt at division by zero.
Methods for finding the square root were developed in the Sulba Sutras & by Aryabhatt another renowned Communalist. 
" Brahmagupta's theorem is a result in geometry. It states that if a cyclic quadrilateral is orthodiagonal (that is, has perpendicular diagonals), then the perpendicular to a side from the point of intersection of the diagonals always bisects the opposite side. It is named after the Indian mathematician Brahmagupta." The Wikipedia.

Seminal contributions have come in from the Babylonians, Egyptians, Greeks, and the Persians. Diophantus, Heron of Alexandria and of course the Incomparable Al Khwarizmi on whose book "Kitab-i-Al-Jabr-Wal-Muqabla" Algebra is named.

The basic tenet of equations can be summarized as follows:-
1) if LHS=RHS then
=> LHS-x=RHS-x;
=> LHS+x=RHS+x;
=> LHS*x=RHS*x;
=> LHS/x=RHS/x;

The above rules are actually a consequence of The Law of Conservation of Energy & Mass

An event from the recent past however proves that this Law no longer holds. This event was the beaming of light from a Teresa Photo on Sept. 5, 1998. So the equation now is:-


 Photo=Photo + Light
So LHS=RHS no longer Holds

So Einstein's famous equatiion Energy=Mass * Speed of Light squared.
becomes
Energy = Mass * Speed of Light squared + T raised to the power A.
where T is the amount of SCAM Money given by Charles Keating to Teresa.
and A is the number of Child Abusing Priests protected by RATzinger.





This new equation has not yet been adopted by the IITs and they are still sticking with the Blasphemous and unsecular Einstein version which I  believe Jairam Ramesh deplored via his IIT faculty are not World Class comments.



Polynomials in Matlab

>> p=[1 0 6 20];
will create the polynomial x^3 + 6x + 20;
To evaluate it for some given value of x we use the polyval function:

for x=1
>> polyval(p,1)

ans =

    27

>>


for x=2.5

>> polyval(p,2.5)

ans =

   50.6250

>>


for x= 1 + i
>> polyval(p,1 + i)

ans =

  24.0000 + 8.0000i

>>


To find the roots of the equation use 

>> roots(p)

ans =

   1.0000 + 3.0000i
   1.0000 - 3.0000i
  -2.0000         

>>


To store the roots in a vector:

>> r=roots(p);
>> r

r =

   1.0000 + 3.0000i
   1.0000 - 3.0000i
  -2.0000         

>>


Using the Poly function we can get the equation back
>> v=poly(r);
>> v

v =

    1.0000    0.0000    6.0000   20.0000

>>


To plot the function create a vector, then use the polyval function to get the values; Then create a plot using the plot function:
>> x=[0:0.01:10];
>> plot(x,polyval(p,x));
>>





Multiplication and Division of Polynomials:
Multiplication is done using the conv function, while Division is done using the deconv function:

Using the polynomial from before :

>> p

p =

     1     0     6    20

>> 

which means x^3 + 6x + 20
to multiply with x-2
>> product=conv(p,[1 -2]);
>> product

product =

     1    -2     6     8   -40

>>

which means x^4 -2x^3 + 6x^2 +8x-40

Dividing using deconv:
>> deconv(product,[1 -2])

ans =

     1     0     6    20

>>

we get back the original polynomial.
This Post will be further enhanced











Saturday, June 11, 2011

3 D Plots in Matlab

3 D Plots in Matlab

To create 3D plots , first of all we need to create 3 variables. x,y and z.

Create the x and y  arrays:-
>> x=-3:0.25:3;
>> y=-3:0.25:3;

Create the meshgrid:-

>> [X Y]=meshgrid(x,y);


Define:-
Z=sqrt(X.^2 + Y.^2);

mesh(X,Y,Z);





>>surf(X,Y,Z);



>>surf(X,Y,-Z);





>>waterfall(X,Y,Z):-
 >>contour(x,y,z):
 >>>> surfc(X,Y,Z):-
 Now we arrive to the view part:
View is used to give the viewing angle of the plot. the syntax of the command is :-
view(azimuth,elevation);
where  the azimuth is an angle measured in degrees  in the x-y plane and is measured relative to the negative y axis and positive as always in the anti clockwise direction.

The elevation is the angle measured from the x-y plane in the direction of the z - axis.
default values are azimuth=-37.5 and elevation =35.
Here are the results of applying different views to the last plot:-

>> view(-65,30);

 >> view(-37.5,40);

Next we shall move to interpolation using Matlab.

Matrices in Matlab

Matrices in Matlab

Matrix manipulation is probably the most important part of Matlab. It is what most people will use it for. In this post we shall learn how to create matrices, perform basic manipulations, and to solve simultaneous equations using Matlab.

Creating a Matrix:- 

>> a=[1,4,-1;1,1,-6;3,-1,-1];
a

a =

     1     4    -1
     1     1    -6
     3    -1    -1

>>

Transposing the matrix :-

>> a'

ans =

     1     1     3
     4     1    -1
    -1    -6    -1

>>

Inverse of a Matrix:-

>> inv(a)

ans =

    0.0986   -0.0704    0.3239
    0.2394   -0.0282   -0.0704
    0.0563   -0.1831    0.0423

>>

>> a*inv(a)

ans =

    1.0000   -0.0000         0
   -0.0000    1.0000         0
    0.0000    0.0000    1.0000

>> The identity matrix as you would expect.

Now let's try and solve the given equation using Matlab:-

1) x + y + z =9
2)2x - 3y + 4z =13
3)3x + 4y + 5z=40

Create two matrices a & b:-

>> a=[1,1,1;2,-3,4;3,4,5]

a =

     1     1     1
     2    -3     4
     3     4     5

>> b=[9;13;40]

b =

     9
    13
    40

>>
This equation can be represented as 
AX=B
=>inv(A) * A * X= inv(A) * B multiplying both sides by inv(A)
=> X=inv(A)* B 

>> inv(a)*b

ans =

    1.0000
    3.0000
    5.0000

>>
which means 
x=1
y=3
z=5

Now, let's try it the other way round.

The same equation can be represented as X'*A'=B' where ' represents the Transpose.
multiplying both sides by inv(A')
=>X'*A'*inv(A')=B'*inv(A')
=>X'=B'*inv(A')


>> b'*inv(a')

ans =

    1.0000    3.0000    5.0000

which means 
x=1
y=3
z=5



Thursday, June 9, 2011

Simple Plots in Matlab

Simple Plots in Matlab

This is a continuation of the earlier post Matlab Basics
Creating Plots in Matlab is probably the easiest in all possible options present in the Software Universe. We shall study only the most basic methods in this Post and then advance to comparatively complex features.

Create a Vector for representing the X - Axis
>> x=[-2*pi:.001:2*pi];
>>
>> y=sin(x);
>> plot(x,y)
>> 




>> plot(cos(x),sin(x))
>>

 


 
>> plot(cos(x),sin(x).*cos(x))
>> 
%Remember the .* represents the dot product, and is necessary because all variables in Matlab are vectors and by default it will try to multiply them as matrices.


Next, we shall advance to more complex formatting of Plots, and then 3D plots.


Creating Variables and Simple Expressions in MATLAB

Basics of Matlab
The easiest way to begin learning Matlab is to start with the Command Window under the Desktop Menu.

Executing expressions in Matlab is absolutely simple !!! Just write the expression and view the results.

1) >> 3+4/10

ans =

    3.4000

>> 


To create a Variable

Simply write the expression
>> x=10

x =

    10

>> x*x + x

ans =

   110

>>

The variable is created automatically and initialized


To view the created variables write
>> x=1,y=2

x =

     1


y =

     2

>> who

Your variables are:

ans  x    y   


Point to remember: There are no scalars in Matlab. Only vectors. Thus x would  actually be [x].
Point to remember: To suppress an output use ; at the statements end.
Point to remember: use clc to clear the display.

>> x=5;
>> x=5

x =

     5

>> 




Creating Vectors

Vectors can be created in the following manners :
>> a=[1,2,3,4,5]

a =

     1     2     3     4     5

>>



>> a=[1:1:10]

a =

     1     2     3     4     5     6     7     8     9    10

>>



The first case is a simple enumeration, the second case specifies the starting value, the increment/decrement and the last value.

>> a=[10:-1:1]

a =

    10     9     8     7     6     5     4     3     2     1

>>


We shall learn 2D and 3D plotting next.