Saturday, April 12, 2014

How to create a Matrix Using Freemat

By now we have come to know some basics about Matrix. Now how to create them and later work with them. Its very easy to create matrix in Freemat.
The commands are similar to Matlab.

1. Row Matrix
---------------------------------------------------------------------------------------------------------------------
A = [ 1 2 3 4 ]

A is the name of the matrix and the value of all the elements are stored in the variable A. You can name it anything .Its an array and give a location number to all the elements of the matrix. The picture of freemat is given below.

If we are defining Matrix we have to use square brackets and inside the brackets we have to write the elements. In creating row matrix we just separate the element with blank spaces only.

2. Column Matrix
---------------------------------------------------------------------------------------------------------------------
B = [ 1; 2; 3; 4 ]

We assigned another name to a column matrix.  Here each element is separated by a ';' . Square bracket is need and every element is given an address to identify it.
Like what does
B(2,1) signify
Its the 'B' Matrix
2nd Row
1st Column
So B(2,1) is 2
The pic is given Below.

3. Any size Matrix
---------------------------------------------------------------------------------------------------------------------
C = [1 2 3 4;5 6 7 8;9 10 11 12]

Here we have created 3 row matrices and a ';' in between them to make the program understand that next segment belong to the next row. So we have 3 row staked together making it a 3X3 matrix.

4. Random Number matrix
---------------------------------------------------------------------------------------------------------------------
D = rand(3,3)

'rand' is the command for random matrix. After a command you don't need to use the square brackets but use first brackets ( ) . This contain the specifications for 'rand' command i.e a 3X3 matrix. The first element signify the number of rows and the second signify the number of columns.




5. Null matrix or Zero Matrix
---------------------------------------------------------------------------------------------------------------------

A = zeros(3,3)

will produce all the 9 elements as zero which is also called null matrix

6. Identity Matrix
---------------------------------------------------------------------------------------------------------------------
B   = eye(3)

Identity matrix is a square matrix of all zeros except the diagonal. The diagonal is one.

7. All One Matrix
-------------------------------------------------------------------------------------------------------------------

C = ones(3,3)

Creates all the elements as one




Thursday, April 10, 2014

Freemat Matrix Operations

Being an oil industry guy and the interest of using Freemat is mainly for using seismic, logs and related application i will mainly concentrate on these and some basic mathematical operations.

Matrix Addition. 
--------------------------------------------------------------------------------------------------

Source - mathsisfun.com

In order to add two matrices the size of matrix has to be same . Whats size? click here
If 
A is (5X2) and B is (5X2 ) then they can be added or else not possible.
You can individually add each element of the same location like the image.
Location of '3' of first matrix is (1,1) and
Location of '4' of second matix is (1,1) 

so they can be added (3+4) = 7 for the same location (1,1)


Matrix subtraction.
--------------------------------------------------------------------------------------------------------------


Source :- mathsisfun.com

The theory is same as the addition but here we are just subtracting. 


Matrix Multiplication
------------------------------------------------------------------------------------------------------------------

1. Scalar Multiplication

Multiply a matrix by a constant value is a scalar multiplication. Like

Source :- mathplanet.com


2. Vector Multiplication



This is an operation where we don't require a similar size of matrix but we need to check the number of columns of first matrix and number of rows of second matrix.
If they match then we can multiply two Matrix.
Result = Column * Row
The multiplication is shown in the above pic.

Matrix Division
----------------------------------------------------------------------------------------------------------------------------------
1. Scalar Division

This means you can divide a matrix by a scalar like

A= | 4 8 10|
      |6 4  12|

A/2= |4/2 8/2 10/2|
         |6/2 4/2 12/2|

So that is easy but what about dividing a matrix by another matrix
That part is a little tricky
Direct Matrix Division is not possible where you divide each element by another 
So if

A= | 4 8 10|                                    B= | 1 2 3 |
      |6 4  12|                                          | 5 8 9 |

then A/B = tricky
A/B can be done by A*B(Inverse)
Just like in real number 10/2 = 10*2(inverse)
           2 inverse is 1/2 = 0.5

But finding  the inverse of a matrix is not so easy .
But in Freemat you can do it easily 
Points to remember is that 
a. Only Square Matrix have Inverse 
b. Not all square matrix have inverse . Those who dont have inverse are called Singular.



Wednesday, April 2, 2014

Freemat See Things in Matrix

Freemat provides a matrix environment of numerical analysis. Freemat sees things like its a matrix, so have to know a little about matrix if you are really interested in using Freemat.

  | 1 2 3 4|
  | 4 5 6 7|       This is a matrix.

Matrix elements should be inside a third brackets like [ 1 2 3]  but here let it make simple for writing here and i will write it like | 1 2 3|

Now 1,2, 3     are elements of the matrix and every element have a address to recognize the element . Just like out home address relates to the person who lives in the address. So we can know every element if someone give us the address of the element.

so this address of the elements of the matrix is given by two term
1)  Row
2) Column

what is row ?

The horizontal line of the matrix is called row.

A=    |1 2 3 |    --------------------Row 1
        |4 5 6 |    --------------------Row 2

What is Column ?
The vertical line is called column.


A=     | 1 2 3 |                                A=  | 1 2 3 |
         | 4 5 6 |                                      | 4 5 6 |
            ^                                                   ^
      Column 1                                     Column 2

Columns are perpendicular to rows

So how many rows and Columns does this matrix have ?

B =     | 1 2 3 4 |
          | 5 6 7 8 |

So B matrix have 2 Rows and 4 Columns

So whats the Size of B
                 = 2 4.