Tuesday, August 20, 2013

PART-I : Introduction to Matlab and Matrix Operations

What is MATLAB?

MATLAB stands for MATrix LABoratory.

MATLAB is a high-performance language for technical computing,
  • Math and computation 
  • Algorithm development (optimized for DSP)
  • Data acquisition 
  • Modeling, simulation, and prototyping 
  • Data analysis, exploration, and visualization 
  • Scientific and engineering graphics 
  • Application development, including graphical user interface building 
The MATLAB System

Development Environment, 
  • Editor and debugger for MATLAB programs (“m-files”)
  • Browsers for help, built-in and on-line documentation
  • Extensive demos 
The MATLAB Mathematical Function Library. 
  • Elementary functions, like sum, sine, cosine, and complex arithmetic 
  • More sophisticated functions like matrix inverse, matrix eigenvalues, Bessel functions, and fast Fourier transforms. 
  • “Toolboxes” for special application areas such as Signal Processing, Communication, Control System, etc.
Graphics.   
  • 2D and 3D plots
  • Editing and annotation features
The MATLAB Application Program Interface (API). 
  • A library that allows you to write C and Fortran programs that interact with MATLAB. 
MATLAB “Help” Utilities

MATLAB is so rich that ‘help’ is essential
  • Command name and syntax
  • Command input/output parameters
  • Usage examples
Help command
  • help command_name
  • help [partial_name] tab
Help documents
Demos

Basic MATLAB Computation: 

Representation of Numbers and Variables

MATLAB operates on n row by m column matrices:
  • A n x m quantity is an array
                         [ 8     1     6
                               3     5     7
                                 4     9     2 ]
      • A 1 x m or a n x 1 quantity is a vector
                             [ 8     1     6 ]
      • A 1 x 1 quantity is a scalar
                         [ 8 ]

      MATRIX:


      • Separate the elements of a row with blanks or commas.
      • Use a semicolon, to indicate the end of each row.
      • Surround the entire list of elements with square brackets, [ ]
      simply type in the Command Window
      
      
           >> A=[ 1 2 3 4 ; 2 3 4 5 ; 5 6 7 4 ]
      
      
      MATLAB displays the matrix you just entered:
      
      
           A =
               1     2     3     4
               2     3     4     5
               5     6     7     4

      Array manipulation
      Sum, diagonal, transpose, indexing, colon operator

      Sum
      >> sum(A)
      
      ans =
      
           8    11    14    13
      
      
      Diagonal
      >> diag(A)
      
      ans =
      
           1
           3
           7
      
      
      Transpose
      >> A'
      ans =
      
           1     2     5
           2     3     6
           3     4     7
           4     5     4
      
      
      Indexing
      The element in row i and column j of A is denoted by A(i,j). For example, A(4,2) is the number in the fourth row and second column. For the magic square, A(4,2) is 5.
      So to compute the sum of the elements in the fourth column of A, type
      
      
      >> A(1,4) + A(2,4) + A(3,4)
      
      ans =
      
          13
      If you try to use the value of an element outside of the matrix, it is an error
      
      
      >>  A(4,5)
      ??? Attempted to access A(4,5); index out of bounds because
      size(A)=[3,4]
      The Colon Operator
      The colon, is one of the most important MATLAB operators. It occurs in several different forms.

      The expression

      
      
      >> 1:10

      is a row vector containing the integers from 1 to 10:

      
      
         1     2     3     4     5     6     7     8     9    10

      To obtain non unity spacing, specify an increment. For example,

      
      
      >> 100:-7:50

      ans =

      
      
          100    93    86    79    72    65    58    51
      and
      
      
      >> 0:pi/4:pi

      ans =

      
      
          0    0.7854    1.5708    2.3562    3.1416

      Subscript expressions involving colons refer to portions of a matrix:

      
      
      A(1:k,j)

      is the first k elements of the jth column of A.


      >> A(1:3,2)


      ans =


           2

           3
           6

      >> A(3,2:4)


      ans =


           6     7     4

      Operators
      Expressions use familiar arithmetic operators and precedence rules.


      + Addition
      -Subtraction
      *Multiplication
      /Division
      \Left division (described in Linear Algebra in the MATLAB documentation)
      ^Power
      'Complex conjugate transpose
      ( )Specify evaluation order

      Matrix and vector addition and multiplication


      Consider Matrices,


      >> A=[1 2 3; 4 5 6]


      A =


           1     2     3

           4     5     6

      >> B=[2 1 4; 3 6 4]


      B =


           2     1     4

           3     6     4


      >> C=[1 2;4 5;6 7]

      C =

           1     2
           4     5
           6     7

      Addition: dimension of the operands should be same

      >> A+B

      ans =


           3     3     7

           7    11    10

      Substraction: dimension of the operands should be same

      >> A-B

      ans =


          -1     1    -1

           1    -1     2

      Multiplication: Colon of first operand should be same as the row of second operand

      >> A*C

      ans =


          27    33

          60    75


      Element-by-element operations:  uses '.' dot operator, dimension of the two operands should be same

      >> A.*B


      ans =


           2     2    12


          12    30    24


      >> A./B


      ans =


          0.5000    2.0000    0.7500

          1.3333    0.8333    1.5000

      >> A.^2


      ans =


           1     4     9

          16    25    36


      TO BE CONTINUED  ..... :)