Docstoc

DISP-2003_ Introduction to Digital Signal Processing_8_

Document Sample
DISP-2003_ Introduction to Digital Signal Processing_8_ Powered By Docstoc
					INTRODUCTION TO
    MATLAB

   Dr. Hugh Blanton

     ENTC 4347
                            TOPICS

1.    Basic MATLAB
2.    Matrices
3.    Operators
4.    Script and function files
5.    Flow control
6.    Plotting




Dr. Blanton - ENTC 4347 -   MATLAB   2 / 45
                      Basic MATLAB

optional windows


workspace
current directory                       type commands here



                                        command window




               screen shot of the Matlab window
   Dr. Blanton - ENTC 4347 -   MATLAB               3 / 45
             Matlab’s help features




          type “help” at the command prompt
         and Matlab returns a list of help topics
Dr. Blanton - ENTC 4347 -   MATLAB                  4 / 45
         Matlab’s help features
                                                  >> help lang




                   Matlab‟s language constructs
Dr. Blanton - ENTC 4347 -   MATLAB                  5 / 45
         Matlab’s help features
                                                >> help for




          how to use Matlab‟s “for” statement
Dr. Blanton - ENTC 4347 -   MATLAB               6 / 45
         Matlab’s help features
       you can also access “on-line” help by clicking the
       question mark in the toolbar




             separate window


Dr. Blanton - ENTC 4347 -   MATLAB                  7 / 45
              MATLAB Variables
   all variables are stored in 32bit floating point format

   no distinction between real and integer

              >>a = 3;
                              same assignment for “a”
              >>a = 3.0;

   Matlab is case sensitive
        >>A=3;
                              Aa


          >>a=2;
Dr. Blanton - ENTC 4347 -   MATLAB                      8 / 45
              MATLAB Variables
       can use numbers and underscore in variable names
              >>case34=6.45;
                                      OK
              >>case_34=6.45;

      names must start with a letter

             >>34case=23.45;          results in a syntax error


     string (text) variables enclosed in single quotes.
     The variable is stored as array of characters
            >>title=„This is the title‟;
Dr. Blanton - ENTC 4347 -   MATLAB                      9 / 45
              MATLAB Variables
       if a variable is defined,
       typing the variable name returns its value

           >>a=45.57;

            >>a

                a=
                  45.57      Matlab returns the value


       to clear a variable from memory
               >>a=4
               >>clear a

Dr. Blanton - ENTC 4347 -   MATLAB                      10 / 45
              MATLAB Variables
       Matlab will “echo” commands unless a semi-colon is used

        >>a=23.2;
        >>


        >>a=23.2
        a=
           23.2         Matlab echoes the command
        >>



Dr. Blanton - ENTC 4347 -   MATLAB                  11 / 45
                MATLAB Variables
                              Vectors
 column vectors                           row vectors
        1 
         
    a  2                              a  1 2 3
        3 
         
 >>a=[1;2;3];                           >>a=[1,2,3];
 >>a                                    >>a
 a=                                     a=
   1                                     1 2 3
   2
   3
use semi-colon                          use comma
to separate rows                        to separate columns
  Dr. Blanton - ENTC 4347 -    MATLAB               12 / 45
                MATLAB Variables
                              Matrices

                                   1 2 3
2-dimensional matrices           a
                                    4 5 6
                                          

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



again, separate columns with commas and rows with semi-colons
  Dr. Blanton - ENTC 4347 -    MATLAB             13 / 45
              MATLAB Variables
                 Indexing Matrix elements

            A vector is a special type of matrix

     row vector is a 1 x n matrix, 1 row n columns

    column vector is a n x 1 matrix, n rows 1 column

    >>a=[1,2,3];
    >>a(2)                  could also reference by a(1,2)
    ans =
         2                  note, a(2,1) would produce an error
                            because “a” only has one row

Dr. Blanton - ENTC 4347 -   MATLAB                     14 / 45
              MATLAB Variables
                 Indexing Matrix elements

                            more examples

          1 2 3 
        a
            4 5 6
                                   >>a=[1,2,3;4,5,6];
                


                                                 assigning
  addressing
                                                >>a(2,2)=9;
  >>a(2,3)                                      >>a
  ans =                                         a=
        6                                          1 2 3
                                                   4 9 6
Dr. Blanton - ENTC 4347 -      MATLAB                   15 / 45
              MATLAB Variables
                  complex-valued numbers

Typically, the variable “i” or “j” is used to represent the
complex variable; e.g.

                        i  1



Then, a complex number is represented as

              z = a + ib         Re(z) = a

                                 Im(z) = b
Dr. Blanton - ENTC 4347 -   MATLAB                    16 / 45
                 MATLAB Variables
                     complex-valued numbers

   Unless i or j has been previously defined, Matlab assigns
   i and j the complex variable value

   In Matlab, a complex variable is represented in the
   following format

             (assuming all variables are cleared)
     >>z=23+i*56;                   >>z=23+j*56;
     >>z                            >>z
     z=                             z=
        23.00 + 56.00i                  23.00 + 56.00i
Matlab always uses the symbol “i” to represent a complex number
   Dr. Blanton - ENTC 4347 -   MATLAB                    17 / 45
                MATLAB Variables
                    complex-valued numbers



What happens in this case?             What happens in this case?
        >>i=3;                           >>a=sqrt(-1);
        >> z=23+i*56;                    >>z=23+a*56;
        >>z                              >>z
        z=                               z=




  Dr. Blanton - ENTC 4347 -   MATLAB                   18 / 45
              MATLAB Variables
                  complex-valued numbers

Note, a real-valued number is a special case of a
complex-valued number


assigning any element of a matrix as complex-valued
makes the entire matrix complex-valued


>>a=[1,2];                    >>a(1)=1+i*5;
>>a                           >>a
a=                            a=
    1 2                         1.00+5.00i          2.00+0.00i

Dr. Blanton - ENTC 4347 -   MATLAB                    19 / 45
              MATLAB Variables
          Advanced data types
                   n-dimensional arrays

                   structures

                   cell arrays




Dr. Blanton - ENTC 4347 -   MATLAB        20 / 45
              MATLAB Variables
    Basic operations
              addition            +
              subtraction         -
              multiplication      *
              division
                 right division      /
                                          ?
                 left division       \


  >>a=3;b=4;
  >>c1=a/b;                 c1=0.75
                                              so, be careful!
  >>c2=a\b;                 c2=1.3333….

Dr. Blanton - ENTC 4347 -   MATLAB                 21 / 45
               MATLAB Variables
         Mixed Real and Complex valued Variables


if both variables are real-valued, a real-valued result is obtained


if one variable is complex-valued, Matlab recasts the real
variable as complex and then performs the operation. The
result is complex-valued

however, the type casting is done internally, the real-valued
variable remains real after the operation


 Dr. Blanton - ENTC 4347 -   MATLAB                    22 / 45
                 MATLAB Variables
                  Other (Scalar) Operations
Math representation           Matlab interpretation
 z  yx                         >>z=y^x;
 y  ex                         >>y=exp(x);
 y  ln(x)                      >>y=log(x);
 y  log(x)                     >>y=log10(x)
 y  sin(x) y  sin 1 (x)      >>y=sin(x);      >>y=asin(x);
 y  cos(x) y  cos 1 (x)      >>y=cos(x);      >>y=acos(x);
 y  tan(x) y  tan 1 (x)      >>y=tan(x);      >>y=atan(x);

   Dr. Blanton - ENTC 4347 -   MATLAB                 23 / 45
              MATLAB Variables
                            Examples


                                       >>y=x^0.5;
          y x                         >>y=x^(1/2);
                                       >>y=sqrt(x);

  All variables in the preceding operations can be
  real or complex, negative or positive


for x < 0, y is complex. Matlab assumes you allow complex
valued numbers. If y is not to be complex, you must
provide error checking.
Dr. Blanton - ENTC 4347 -     MATLAB                  24 / 45
                 MATLAB Variables
                                Matrices
•Only matrices of the same dimension can be added and subtracted

•For multiplication, the inner dimensions must be the same
                                                   4 5
       1 2 3               2 3 4
   A                    B                 C  6 7 
        4 5 6              5 6 7 
                                                        
                                                   8 9 
                                                        
        No error                            Error
                                                 >>D=A+C;
      >>D=A+B;                                   >>D=A*B;
      >>D=A-B;           Matrix multiplication   >>D=B*A;
      >>D=A*C;           not commutative
      >>D=C*A;
   Dr. Blanton - ENTC 4347 -     MATLAB                     25 / 45
              MATLAB Variables
            Left(\) and Right(/) Matrix “division”


 Math representation                 Matlab interpretation

      C  A 1B                          >>C=A\B;

      C  BA 1                          >>C=B/A;


        Remember, A must be square and full rank
        (linearly independent rows/columns)


Dr. Blanton - ENTC 4347 -   MATLAB                     26 / 45
              MATLAB Variables
                       Matrix Transpose

 Math representation                 Matlab interpretation
      C  AT                      >>C=A’;
For complex-valued matrices, complex conjugate transpose
    1 2 3
  A                                a  1  j2 3  j4
     4 5 6
           
   >>B=A’;                            >>b=a’;

       1 4 
   B  2 5                           1  j2 
                                     b
           
                                        3  j4 
                                                
       3 6
           
Dr. Blanton - ENTC 4347 -   MATLAB                         27 / 45
                MATLAB m-files
                   Two types of m-files

script files
         collection of commands that Matlab executes
         when the script is “run”

function files
       collection of commands which together
       represent a function, a procedure or a method


   Both types are separate files with a “.m” extension


Dr. Blanton - ENTC 4347 -   MATLAB                  28 / 45
                MATLAB m-files
     To create an m-file, open the Matlab text editor




                             Click on the “page” icon



                            The Matlab text editor window will open




Dr. Blanton - ENTC 4347 -     MATLAB                    29 / 45
                   MATLAB m-files
                               Script Files
On the command line                  In the script file named test.m
 >>x=3.0;
 >>y=x^2;
 >>y
 y =
    9.0
 >>
                                          On the command line
                                         >>test
                                         y =
                                             9.0
                                         >>
   Dr. Blanton - ENTC 4347 -    MATLAB                     30 / 45
                 MATLAB m-files
                             Script Files
      script files share the workspace memory


                                       test.m script
>>x=5.0;
>>test
>>y
y =
   25.0
>>




 Dr. Blanton - ENTC 4347 -    MATLAB                   31 / 45
                  MATLAB m-files
                              Script Files
       script files can call other script files
                                        inner.m script



>>outter
y =                                     outter.m script
   36.0
>>




  Dr. Blanton - ENTC 4347 -    MATLAB                     32 / 45
                MATLAB m-files
                       Function Files
     Matlab identifies function files from script files by
     using the “function” and “return” keywords




           the name of the function file must be
           the same name as the function
Dr. Blanton - ENTC 4347 -   MATLAB                    33 / 45
                  MATLAB m-files
                              Function Files
                       The function file x2.m




              >>r=3;                           >>h=x2(4.2);
              >>d=x2(r);                       >>h
              >>d                              h =
              d =                                  17.64
                  9.0                          >>
Dr. Blanton   >>
              - ENTC 4347 -     MATLAB                    34 / 45
                 MATLAB m-files
                             Function Files

                     Multiple Inputs and Outputs




outputs in square brackets, [ ]               inputs in parentheses ( )


 Dr. Blanton - ENTC 4347 -     MATLAB                        35 / 45
                MATLAB m-files
                            Function Files
  variables created in the function are not retained
  in the workspace, except for the output variables



   the function does not have access to workspace
   variables, except for the inputs


   variables passed to the function are “copies” of the
   workspace variables. Changing their value inside the
   function has no effect on their value in the workspace.

Dr. Blanton - ENTC 4347 -     MATLAB                   36 / 45
           MATLAB Flow Control
                 The “while” and “if” statements


while expression            if expression       if expression
       statements                  statements          statements1
end                         end                 else
                                                        statements2
                                                end

  Matlab evaluates expression as logical “true” or “false”
      “false” equivalent to zero
      “true” equivalent to any non-zero number

 statements, any valid Matlab command
Dr. Blanton - ENTC 4347 -    MATLAB                   37 / 45
              MATLAB Flow Control
                         evaluating expression
any valid equation                      conditional operators
  a=4;                                  == equal to
  b=5;                                  <     less than
  c=5;                                  >     greater than
  if a+b      “True”                    <= less than or equal to
  if b-c      “False”                   >= greater than or equal to
                                        ~= not equal to
watch out for round-off
and word length error                   logical operators
if sin(0)    “False”                     & and
if sin(pi) “True”                        | or
sin(pi) = 1.22e-16
                                        while(3<=a)&(a<=5)
   Dr. Blanton - ENTC 4347 -   MATLAB                       38 / 45
           MATLAB Flow Control
                      The “for” statement

              for index = start : [increment :] end
                      statements
              end
index, start, increment, and end do not need to be integer valued

increment is optional, if increment is not specified
increment defaults to 1

index can be incremented positive (increment > 0) or
negative (increment < 0)

loop stops when index > end (or index < end)
Dr. Blanton - ENTC 4347 -   MATLAB                     39 / 45
           MATLAB Flow Control
                            example



                                script file to cycle through x values




                               function file to generate the y values



Dr. Blanton - ENTC 4347 -    MATLAB                    40 / 45
         MATLAB PLOTTING
                   Basic 2D plotting functions

      plot(x1,y1[,x2,y2,x3,y3.....])
      xlabel(‘x axis name’)
      ylabel(‘y axis name’)
      title(‘graph name’)



                       Additional functions
        grid on
        grid off
        axis([xmin,xmax,ymin,ymax])


Dr. Blanton - ENTC 4347 -   MATLAB               41 / 45
             MATLAB PLOTTING
                           example y = sin(t)




the “plot” function alone




    Dr. Blanton - ENTC 4347 -   MATLAB          42 / 45
           MATLAB PLOTTING
                          example y = sin(t)




script file to generate
a graph of y = sin(t)


  Dr. Blanton - ENTC 4347 -    MATLAB          43 / 45
             MATLAB PLOTTING
                             example y = sin(t)




function file to generate
a graph of y = sin(t)
>>graphsin
>> Dr. Blanton -   ENTC 4347 -   MATLAB           44 / 45
            MATLAB PLOTTING
                 Adding a Legend for multiple graphs




“legend” remembers
the order the graphs
were plotted

   Dr. Blanton - ENTC 4347 -   MATLAB                  45 / 45

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:9
posted:9/23/2011
language:English
pages:45