CopyRight

If you feel that any content has violated the copy-Right law please be free to mail me.
I will take suitable action.
Thanks

Monday 12 May 2014

Introduction to Matlab

Introduction to Matlab

THEORY:

MATLAB® is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation. Typical uses include
·         Math and computation
·         Algorithm development
·         Data acquisition
·         Modeling, simulation, and prototyping
·         Data analysis, exploration, and visualization
·         Scientific and engineering graphics
·         Application development, including graphical user interface building
MATLAB is an interactive system whose basic data element is an array that does not require dimensioning. This allows you to solve many technical computing problems, especially those with matrix and vector formulations, in a fraction of the time it would take to write a program in a scalar non interactive language such as C or Fortran.
The name MATLAB stands for matrix laboratory. MATLAB was originally written to provide easy access to matrix software developed by the LINPACK and EISPACK projects. Today, MATLAB engines incorporate the LAPACK and BLAS libraries, embedding the state of the art in software for matrix computation.

FUNCTIONS USED IN MATLAB:
1)    Logarithm:

Syntax:   Y = log(X)

Description:
The log function operates element-wise on arrays. Its domain includes complex and negative numbers, which may lead to unexpected results if used unintentionally.
Y = log(X) returns the natural logarithm of the elements of X. For complex or negative, where, the complex logarithm is returned. log(z) = log(abs(z)) + i*atan2(y,x).

Example: (log(-1))
ans =

    3.1416

2)    Plot:
Syntax:   plot(Y)
               plot(X1,Y1,...)

Description:
plot(Y) plots the columns of Y versus their index if Y is a real number. If Y is complex, plot(Y) is equivalent to plot(real(Y),imag(Y)). In all other uses of plot, the imaginary component is ignored.
plot(X1,Y1,...) plots all lines defined by Xn versus Yn pairs. If only Xn or Yn is a matrix, the vector is plotted versus the rows or columns of the matrix, depending on whether the vector's row or column dimension matches the matrix.

Example:     x = -pi:pi/10:pi;
                    y = tan(sin(x)) - sin(tan(x));
                    plot(x,y)

3)    Exponential:

Syntax:      Y = exp(X)

Description:
The exp function is an elementary function that operates element-wise on arrays. Its domain includes complex numbers.
Y = exp(X) returns the exponential for each element of X. For complex , it returns the complex exponential .

Example: y = exp(2)
y =

    7.3891

4)    Grid:

Syntax:     grid on
     grid off
     grid minor

            Description:    
The grid function turns the current axes' grid lines on and off.
grid on adds major grid lines to the current axes.
grid off removes major and minor grid lines from the current axes.
grid toggles the major grid visibility state.

Example:
x=-pi:0.01:pi;
            plot(x,sin(x)),grid on
           
5)    Stem:

Syntax:    stem(Y)
    stem(X,Y)

           
Description:
A two-dimensional stem plot displays data as lines extending from a baseline along the x-axis. A circle (the default) or other marker whose y-position represents the data value terminates each stem.
stem(Y) plots the data sequence Y as stems that extend from equally spaced and automatically generated values along the x-axis. When Y is a matrix, stem plots all elements in a row against the same x value.
stem(X,Y) plots X versus the columns of Y. X and Y must be vectors or matrices of the same size. Additionally, X can be a row or a column vector and Y a matrix with length(X) rows.

            Example:
            x=45;
h=stem(x,sin(x))
h =

  152.0028

6)    Xlabel, Ylabel & Title

Syntax:     xlabel('string'), ylabel('string'), title('string')


Description:
Each axes graphics object can have one label for the x-, y-, and z-axis. The label appears beneath its respective axis in a two-dimensional plot and to the side or beneath the axis in a three-dimensional plot.
xlabel('string'), ylabel('string') labels the x-axis, y-axis of the current axes.Each axes graphics object can have one title. The title is located at the top and in the center of the axes.
title('string') outputs the string at the top and in the center of the current axes.

Example:    x = [1,2,3];
       y=sin(x);
       z=stem(y);
       xlabel('x')
       ylabel('y')
       title('Stem Plot')

7)    Maximum:

Syntax:      C = max(A)
      C = max(A,B)

Description:
C = max(A) returns the largest elements along different dimensions of an array.
If A is a vector, max(A) returns the largest element in A.
If A is a matrix, max(A) treats the columns of A as vectors, returning a row vector containing the maximum element from each column.
If A is a multidimensional array, max(A) treats the values along the first non-singleton dimension as vectors, returning the maximum value of each vector.
C = max(A,B) returns an array the same size as A and B with the largest elements taken from A or B.

Example:
x = [1,2,3;4,5,6;7,8,9];
 max(x)
ans =

     7     8     9

8)    Minimum:

Syntax:    C = min(A)
    C = min(A,B)

Description:

C = min(A) returns the smallest elements along different dimensions of an array.
If A is a vector, min(A) returns the smallest element in A.
If A is a matrix, min(A) treats the columns of A as vectors, returning a row vector containing the minimum element from each column.
If A is a multidimensional array, min operates along the first nonsingleton dimension.
C = min(A,B) returns an array the same size as A and B with the smallest elements taken from A or B.

Example:
x = [1,2,3;4,5,6;7,8,9];
min(x)
ans =

     1     2     3

9)    Modulus:

Syntax:   M = mod(X,Y)

Description:
M = mod(X,Y) if Y ~= 0, returns X - n.*Y where n = floor(X./Y) . If Y is not an integer and the quotient X./Y is within roundoff error of an integer, then n is that integer. By convention, mod(X,0) is X. The inputs X and Y must be real arrays of the same size, or real scalars.

Example:
mod(13,5)
ans =

     3

10) Transfer Function:

Syntax:    sys = tf(num,den)
    sys = tf(num,den,Ts)
    sys = tf(M)
                sys = tf(num,den,ltisys)

Description:
tf is used to create real- or complex-valued transfer function models (TF objects) or to convert state-space or zero-pole-gain models to transfer function form.

Example:   h=tf([1,0],[1 2 10])

      Transfer function:
                s
        --------------
      s^2 + 2 s + 10

11) Absolute

Syntax:     Y = abs(X)

Description:
abs(X) returns an array Y such that each element of Y is the absolute value of the corresponding element of X.
If X is complex, abs(X) returns the complex modulus (magnitude), which is the same as sqrt(real(X).^2 + imag(X).^2).

Example:    abs(-5)
       ans =

                        5

12) Matrix addition

Syntax:    A+B

Description:
MATLAB has two different types of arithmetic operations. Matrix arithmetic operations are defined by the rules of linear algebra. Array arithmetic operations are carried out element by element, and can be used with multidimensional arrays. The period character (.) distinguishes the array operations from the matrix operations. However, since the matrix and array operations are the same for addition and subtraction, the character pairs .+ and .- are not used.
+ Addition or unary plus. A+B adds A and B. A and B must have the same size, unless one is a scalar. A scalar can be added to a matrix of any size.

Example:
a=[1,2,3;4,5,6];
b=[4,5,6;1,2,3];
a+b
ans =

     5     7     9
     5     7     9

13) Matrix Subtraction

Syntax:   A-B

Description:
MATLAB has two different types of arithmetic operations. Matrix arithmetic operations are defined by the rules of linear algebra. Array arithmetic operations are carried out element by element, and can be used with multidimensional arrays. The period character (.) distinguishes the array operations from the matrix operations. However, since the matrix and array operations are the same for addition and subtraction, the character pairs .+ and .- are not used.
- Subtraction or unary minus. A-B subtracts B from A. A and B must have the same size, unless one is a scalar. A scalar can be subtracted from a matrix of any size.

Example:
a=[1,2,3;4,5,6];
b=[4,5,6;1,2,3];
a-b
ans =

    -3    -3    -3
     3     3     3

14) Matrix Multiplication

Syntax:     A*B     A.*B

Description:
MATLAB has two different types of arithmetic operations. Matrix arithmetic operations are defined by the rules of linear algebra. Array arithmetic operations are carried out element by element, and can be used with multidimensional arrays. The period character (.) distinguishes the array operations from the matrix operations. However, since the matrix and array operations are the same for addition and subtraction, the character pairs .+ and .- are not used.
* Matrix multiplication. C = A*B is the linear algebraic product of the matrices A and B.
.* Array multiplication. A.*B is the element-by-element product of the arrays A and B. A and B must have the same size, unless one of them is a scalar.

Example:
a=[1,2,3;4,5,6];
b=[4,5,6;1,2,3];
a.*b
ans =

     4    10    18
     4    10    18

15) Matrix Power
Syntax:    A^B    

Description:
MATLAB has two different types of arithmetic operations. Matrix arithmetic operations are defined by the rules of linear algebra. Array arithmetic operations are carried out element by element, and can be used with multidimensional arrays. The period character (.) distinguishes the array operations from the matrix operations. However, since the matrix and array operations are the same for addition and subtraction, the character pairs .+ and .- are not used.
^ Matrix power. X^p is X to the power p, if p is a scalar. If p is an integer, the power is computed by repeated squaring. If the integer is negative, X is inverted first.
.^ Array power. A.^B is the matrix with elements A(i,j) to the B(i,j) power. A and B must have the same size, unless one of them is a scalar.

Example:
a=[1,2,3;4,5,6;7,8,9];
a.^2
ans =

     1     4     9
    16    25    36
    49    64    81
16) Fourier Transform
Syntax:  F = fourier(f)

Description:
The Fourier transform is a representation of an image as a sum of complex exponentials of varying magnitudes, frequencies, and phases. The Fourier transform plays a critical role in a broad range of image processing applications, including enhancement, analysis, restoration, and compression.
The Fourier transform of the expression f = f(x) with respect to the variable x at the point w is defined as follows:
http://www.mathworks.in/help/releases/R2013b/symbolic/eqn1327359241.png
Here c and s are parameters of the Fourier transform. The fourier function uses c = 1, s = –1.
Example:
syms x y
f = exp(-x^2);
fourier(f, x, y)
ans =

pi^(1/2)*exp(-y^2/4)

17)  Z-Transform
Syntax:  F = ztrans(f)

Description:
           The Z-transform of the expression f = f(n) is defined as follows:
   http://www.mathworks.in/help/releases/R2013b/symbolic/eqn1327618459.png

Example:
syms k x
f = sin(k);
ztrans(f, k, x)
 ans =

(x*sin(1))/(x^2 - 2*cos(1)*x + 1)

18) Laplace Transform
Syntax: L = laplace(F)

Description:
              The Laplace transform is defined as follows:
      http://www.mathworks.in/help/releases/R2013b/symbolic/eqn1327616965.png
Example:
syms a t y
f = exp(-a*t);
laplace(f, y)
 ans =

1/(a + y)

19)  Length
Syntax: length(X)

Description:
It finds the number of elements along the largest dimension of an array. array is an array of any MATLAB® data type and any valid dimensions. numberOfElements is a whole number of the MATLAB double class.
For nonempty arrays, numberOfElements is equivalent to max(size(array)). For empty arrays, numberOfElements is zero.

Example:
X = [5, 3.4, 72, 28/4, 3.61, 17 94 89];
length(X)
ans =

     8


20)  Size
Syntax: D = size(X)

Description:
If X is a scalar, then size(X) returns the vector [1 1]. Scalars are regarded as a 1-by-1          arrays in MATLAB®.
If X is a table, size(X) returns a two-element row vector consisting of the number of rows  and the number of variables in the table. Variables in the table can have multiple columns, but size only counts the variables and rows.

Example:
v=[1 2 3; 4 5 6]
size(v)
ans =

     2     3

21)  Display
Syntax: display(X)

Description:
prints the value of X. MATLAB® implicitly calls display after any variable or expression that is not terminated by a semicolon.
To customize the display of objects, overload the disp function instead of the display function. display calls disp.

Example:
X = magic(3);
display(X)
X =

     8     1     6
     3     5     7
     4     9     2

22)  Figure
Syntax: figure(H)

Description:
figure Create figure window.
figure, by itself, creates a new figure window, and returns
its handle.
 
figure(H) makes H the current figure, forces it to become visible,
and raises it above all other figures on the screen.  If Figure H
does not exist, and H is an integer, a new figure is created with
handle H.

Example:
Figure
Opens a figure window

23)  whos
Syntax: whos

Description:
whos List current variables, long form.
whos is a long form of WHO.  It lists all the variables in the current
workspace, together with information about their size, bytes, class,
etc.

In a nested function, variables are grouped into those in the nested
function and those in each of the containing functions, each group
separated by a line of dashes.  In nested functions and in functions
containing nested functions, even uninitialized variables are listed.
   
whos GLOBAL lists the variables in the global workspace.
whos -FILE FILENAME lists the variables in the specified .MAT file.
whos ... VAR1 VAR2 restricts the display to the variables specified.

The wildcard character '*' can be used to display variables that match
a pattern.  For instance, whos A* finds all variables in the current
workspace that start with A.

Example:
whos
  Name      Size            Bytes  Class     Attributes

  X         3x3                72  double  

24)  who
Syntax: who

Description:
who lists the variables in the current workspace.
In a nested function, variables are grouped into those in the nested
function and those in each of the containing functions.  who displays
only the variables names, not the function to which each variable
belongs.  For this information, use WHOS.  In nested functions and
in functions containing nested functions, even unassigned variables
are listed.

Example:
who

Your variables are:

Area   ans    k      n      s1     s3     theta  x      z     
a      f      m      r      s2     t      v      y     

25)  imread
Syntax: A = imread(FILENAME,FMT)

Description:
It reads a grayscale or color image from the file specified by the string filename. If the file is not in the current folder, or in a folder on the MATLAB® path, specify the full pathname.

The text string fmt specifies the format of the file by its standard file extension. For example, specify 'gif' for Graphics Interchange Format files. To see a list of supported formats, with their file extensions, use the imformats function. If imread cannot find a file named filename, it looks for a file named filename.fmt.

The return value A is an array containing the image data. If the file contains a grayscale image, A is an M-by-N array. If the file contains a truecolor image, A is an M-by-N-by-3 array. For TIFF files containing color images that use the CMYK color space, A is an M-by-N-by-4 array. See TIFF in the Format-Specific Information section for more information.

The class of A depends on the bits-per-sample of the image data, rounded to the next byte boundary. For example, imread returns 24-bit color data as an array of uint8 data because the sample size for each color component is 8 bits. See Tips for a discussion of bitdepths, and see Format-Specific Information for more detail about supported bit depths and sample sizes for a particular format.

Example:

A = imread('ngc6543a.jpg');
imread returns a 650-by-600-by-3 array, A.

26) Imresize
Snytax: B = imresize(A, SCALE)

Description:
imresize Resize image.
B = imresize(A, SCALE) returns an image that is SCALE times the
size of A, which is a grayscale, RGB, or binary image.
  
B = imresize(A, [NUMROWS NUMCOLS]) resizes the image so that it has
the specified number of rows and columns.  Either NUMROWS or NUMCOLS
may be NaN, in which case imresize computes the number of rows or
columns automatically in order to preserve the image aspect ratio.

Example:
I = imread('rice.png');        J = imresize(I, 0.005)
J =

  121  114
   67   59

27) fliplr
Snytax: fliplr(X)

Description:
fliplr Flip matrix in left/right direction.
fliplr(X) returns X with row preserved and columns flipped
in the left/right direction.

Example:
X=[1 2 3; 4 5 6];
fliplr(X)
ans =

     3     2     1
     6     5     4

28) display
Syntax: display(X)

Description:
Display text and numeric expressions

Example:
display(X)
X =

     1     2     3

     4     5     6