labs-presentation 3 - matlab

Upload: shahid-nawaz

Post on 07-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Labs-Presentation 3 - Matlab

    1/40

    Introduction to Matlab

  • 8/6/2019 Labs-Presentation 3 - Matlab

    2/40

    Topics

    Introduction

    MATLAB Environment

    Getting Help

    Variables

    Vectors, Matrices, and Linear Algebra

    Plotting

    Built in Functions

    Selection Programming

    M-Files User Defined Functions

    Specific Topics

  • 8/6/2019 Labs-Presentation 3 - Matlab

    3/40

    Introduction

    What is MATLAB ?

    MATLAB is a computer program that combines computation andvisualization power that makes it particularly useful tool for engineers.

    MATLAB is an executive program, and a script can be made with a list

    of MATLAB commands like oth

    er programming language.

    MATLAB Stands forMATrix LABoratory.

    The system was designed to make matrix computation particularly easy.

    The MATLAB environment allows the user to: manage variables import and export data perform calculations generate plots develop and manage files for use with MATLAB.

  • 8/6/2019 Labs-Presentation 3 - Matlab

    4/40

    To start MATLAB:START PROGRAMS

    MATLAB 7.0 MATLAB

    7.0

    MATLAB

    Environment

  • 8/6/2019 Labs-Presentation 3 - Matlab

    5/40

    Display Windows

  • 8/6/2019 Labs-Presentation 3 - Matlab

    6/40

    Display Windows (cont)

    Graphic (Figure) Window

    Displays plots and graphs

    Created in response to graphics commands.

    M-file editor/debugger window Create and edit scripts of commands called M-files.

  • 8/6/2019 Labs-Presentation 3 - Matlab

    7/40

    Getting Help

    type one of following commands in the command window: help lists all the help topic

    help topic provides help for the specified topic help command provides help for the specified command

    help help provides information on use of the help command

    helpwin opens a separate help window for navigation

    lookforkeyword Search all M-files forkeyword

  • 8/6/2019 Labs-Presentation 3 - Matlab

    8/40

    Getting Help (cont)

    Google MATLAB helpdesk

    Go to the online HelpDesk provided bywww.mathworks.com

    You can find EVERYTHING you

    need to know about MATLABfrom the online HelpDesk.

  • 8/6/2019 Labs-Presentation 3 - Matlab

    9/40

    Variables

    Variable names:

    Must start with a letter

    May contain only letters, digits, and the underscore _

    Matlab is case sensitive, i.e. one & OnE are different variables.

    Matlab only recognizes the first 31 characters in a variable name.

    Assignment statement:

    Variable = number;

    Variable = expression;

    Example:

    >> tutorial = 1234;>> tutorial = 1234

    tutorial =

    1234

    NOTE: when a semi-colon; is placed at the end ofeach command, the resultis not displayed.

  • 8/6/2019 Labs-Presentation 3 - Matlab

    10/40

    Variables (cont)

    Special variables: ans : default variable name for the result

    pi: T = 3.1415926

    eps: = 2.2204e-016, smallest amount by which 2 numbers can differ.

    Inforinf: g, infinity NaN ornan: not-a-number

    Commands involving variables: who: lists the names of defined variables

    whos: lists the names and sizes of defined variables

    clear: clears all varialbes, reset the default values of special

    variables. clearname: clears the variable name

    clc: clears the command window

    clf: clears the current figure and the graph window.

  • 8/6/2019 Labs-Presentation 3 - Matlab

    11/40

    Vectors, Matrices and Linear Algebra

    Vectors

    Array Operations

    Matrices

    Solutions to Systems of Linear Equations.

  • 8/6/2019 Labs-Presentation 3 - Matlab

    12/40

    MATLAB BASICSMATLAB BASICS

    Variables and Arrays

    Array: A collection of data values organized into rowsand columns, and known by a single name.

    Row 1

    Row 2

    Row 3

    Row 4

    Col 1 Col 2 Col 3 Col 4 Col 5

    arr(3,2)

  • 8/6/2019 Labs-Presentation 3 - Matlab

    13/40

    MATLAB BASICSMATLAB BASICS

    Arrays

    The fundamental unit of data in MATLAB

    Scalars are also treated as arrays by MATLAB(1 row and 1 column).

    Row and column indices of an array start from 1.

    Arrays can be classified as vectors and

    matrices.

  • 8/6/2019 Labs-Presentation 3 - Matlab

    14/40

    MATLAB BASICSMATLAB BASICS

    Vector:Array with one dimension

    Matrix:Array with more than one dimension

    Size of an array is specified by the number of rowsand the number of columns, with the number ofrows mentioned first (For example: n x m array).

    Total number of elements in an array is the

    product of the number of rows and the number ofcolumns.

  • 8/6/2019 Labs-Presentation 3 - Matlab

    15/40

    MATLAB BASICSMATLAB BASICS

    1 2

    3 4

    5 6a= 3x2 matrix 6 elements

    b=[1 2 3 4] 1x4 array 4 elements, row vector

    c=

    1

    3

    5

    3x1 array 3 elements, column vector

    a(2,1)=3 b(3)=3 c(2)=3

    Row # Column #

  • 8/6/2019 Labs-Presentation 3 - Matlab

    16/40

    Vectors

    A row vector in MATLAB can be created by an explicit list, starting with a left bracket, enteringthe values separated by spaces (or commas) and closing the vector with a right bracket.

    A column vector can be created the same way, and the rows are separated by semicolons.

    Example:

    >> x = [ 0 0.25*pi 0.5*pi 0.75*pi pi ]

    x =0 0.7854 1.5708 2.3562 3.1416

    >> y = [ 0; 0.25*pi; 0.5*pi; 0.75*pi; pi ]

    y =

    0

    0.7854

    1.5708

    2.3562

    3.1416

    x is a row vector.

    y is a column vector.

  • 8/6/2019 Labs-Presentation 3 - Matlab

    17/40

    Vectors (cont)

    Vector Addressing A vector element is addressed in MATLAB with an integer index

    enclosed in parentheses.

    Example:

    >> x(3)

    ans =1.5708

    1st to 3rd elements of vector x

    The colon notation may be used to address a block of elements.

    (start : increment : end)

    start is the starting index, increment is the amount to add to each successive index, and end is the

    ending index. A shortened format (start : end) may be used if increment is 1.

    Example:

    >> x(1:3)

    ans =

    0 0.7854 1.5708

    NOTE: MATLAB index starts at1.

    3rd element of vector x

  • 8/6/2019 Labs-Presentation 3 - Matlab

    18/40

    Vectors (cont)

    Some useful commands:

    x = start:end create row vector x starting with start, counting by one,ending at end

    x = start:increment:endcreate row vector x starting with start, counting by

    increment, ending at or before end

    length(x) returns the length of vector x

    y = x transpose of vector x

    dot (x, y) returns the scalar dot product of the vector x and y.

  • 8/6/2019 Labs-Presentation 3 - Matlab

    19/40

    Array Operations

    Scalar-Array Mathematics

    For addition, subtraction, multiplication, and division of an array by ascalar simply apply the operations to all elements of the array.

    Example:

    >> f = [ 1 2; 3 4]

    f =1 2

    3 4

    >> g = 2*f 1

    g =

    1 3

    5 7

    Each element in the array f is

    multiplied by 2, then subtractedby 1.

  • 8/6/2019 Labs-Presentation 3 - Matlab

    20/40

    Array Operations (cont)

    Element-by-Element Array-Array Mathematics.

    Operation Algebraic Form MATLAB

    Addition a + b a + b

    Subtraction a b a b

    Multiplication a x b a .* b

    Division a z b a ./ b

    Exponentiation ab a .^ b

    Example:>> x = [ 1 2 3 ];

    >> y = [ 4 5 6 ];

    >> z = x .* y

    z =

    4 10 18

    Each element in x is multiplied bythe corresponding element in y.

  • 8/6/2019 Labs-Presentation 3 - Matlab

    21/40

    Matrices

    A is an m x n matrix.

    A Matrix array is two-dimensional, having both multiple rows and multiple columns,

    similar to vector arrays:

    it begins with [, and end with ]

    spaces or commas are used to separate elements in a row

    semicolon or enter is used to separate rows.

    Example:

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

    f =

    1 2 3

    4 5 6

    >> h = [ 2 4 61 3 5]

    h =

    2 4 6

    1 3 5the main diagonal

  • 8/6/2019 Labs-Presentation 3 - Matlab

    22/40

    Matrices (cont)

    Matrix Addressing:

    -- matrixname(row, column)

    -- colon may be used in place of a row or column reference to select theentire row or column.

    recall:f =

    1 2 34 5 6

    h =2 4 61 3 5

    Example:

    >> f(2,3)

    ans =

    6

    >> h(:,1)

    ans =

    2

    1

  • 8/6/2019 Labs-Presentation 3 - Matlab

    23/40

    Matrices (cont)

    Some useful commands:

    zeros(n)zeros(m,n)

    ones(n)ones(m,n)

    size (A)

    length(A)

    returns a n x n matrix of zerosreturns a m x n matrix of zeros

    returns a n x n matrix of onesreturns a m x n matrix of ones

    for a m x n matrix A, returns the row vector [m,n]containing the number of rows and columns inmatrix.

    returns the larger of the number of rows orcolumns in A.

  • 8/6/2019 Labs-Presentation 3 - Matlab

    24/40

    Matrices (cont)

    Transpose B = A

    Identity Matrix eye(n) returns an n x n identity matrix

    eye(m,n) returns an m x n matrix with ones on the main

    diagonal and zeros elsewhere.

    Addition and subtraction C = A + B

    C = A B

    Scalar Multiplication B = EA, where E is a scalar.

    Matrix Multiplication C = A*B

    Matrix Inverse B = inv(A), A must be a square matrix in this case.

    rank (A) returns the rank of the matrix A.Matrix Powers B = A.^2 squares each element in the matrix

    C = A * A computes A*A, and A must be a square matrix.

    Determinant det (A), and A must be a square matrix.

    more commands

    A, B, C are matrices, and m, n, E are scalars.

  • 8/6/2019 Labs-Presentation 3 - Matlab

    25/40

    Solutions to Systems ofLinear Equations

    Example: a system of 3 linear equations with 3 unknowns (x1, x2, x3):

    3x1+ 2x

    2 x

    3= 10

    -x1+ 3x

    2+ 2x

    3= 5

    x1 x

    2 x

    3= -1

    Then, the system can be described as:

    Ax = b

    !

    111

    231

    123

    A

    3

    2

    1

    x

    x

    x

    x

    1

    5

    10

    b

    Let :

  • 8/6/2019 Labs-Presentation 3 - Matlab

    26/40

    Solutions to Systems ofLinear Equations (cont)

    Solution by Matrix Inverse:Ax = b

    A-1Ax = A-1b

    x = A-1b

    MATLAB:>> A = [ 3 2 -1; -1 3 2; 1 -1 -1];

    >> b = [ 10; 5; -1];

    >> x = inv(A)*b

    x =

    -2.0000

    5.0000

    -6.0000

    Answer:

    x1 = -2, x2 = 5, x3 = -6

    Solution by Matrix Division:The solution to the equation

    Ax = bcan be computed using left division.

    Answer:

    x1 = -2, x2 = 5, x3 = -6

    NOTE:left division: A\b b zA right division: x/y x z y

    MATLAB:>> A = [ 3 2 -1; -1 3 2; 1 -1 -1];

    >> b = [ 10; 5; -1];

    >> x = A\b

    x =

    -2.0000

    5.0000-6.0000

  • 8/6/2019 Labs-Presentation 3 - Matlab

    27/40

    The input function displays a prompt string in theCommand Window and then waits for the user torespond.

    my_val = input( Enter an inputvalue: );

    in1 = input( Enter data: );

    in2 = input( Enter data: ,`s );

    Initializing with Keyboard Input

  • 8/6/2019 Labs-Presentation 3 - Matlab

    28/40

    How to display dataHow to display data

    The disp( ) function>> disp( 'Hello' )

    Hello

    >> disp(5)

    5

    >> disp( [ 'Bilkent ' 'University' ] )

    Bilkent University

    >> name = 'Alper';>> disp( [ 'Hello ' name ] )

    Hello Alper

  • 8/6/2019 Labs-Presentation 3 - Matlab

    29/40

    Plotting

    For more information on 2-D plotting, type help graph2d

    Plotting a point:

    >> plot ( variablename,symbol)the function plot () creates agraphics window,calleda Figurewindow,andnamed bydefault

    Figure No. 1

    Example : Complex number>> z = 1 + 0.5j;>> plot (z, .)

  • 8/6/2019 Labs-Presentation 3 - Matlab

    30/40

    Plotting (cont)

  • 8/6/2019 Labs-Presentation 3 - Matlab

    31/40

    Built-in MATLAB Functions

    result= function_name( input ); abs, sign

    log, log10, log2

    exp

    sqrt

    sin, cos, tan

    asin, acos, atan

    max, min

    round, floor, ceil, fix

    mod, rem help elfunp help for elementary math functions

  • 8/6/2019 Labs-Presentation 3 - Matlab

    32/40

    Selection Programming

    Flow Control

    Loops

  • 8/6/2019 Labs-Presentation 3 - Matlab

    33/40

    Flow Control

    Simple ifstatement:iflogicalexpression

    commandsend

    Example: (Nested)if d d

    b=0;end

    end Example: (else and elseifclauses)

    if temperature > 100disp (Too hot equipment malfunctioning.)

    elseif temperature > 90disp (Normal operating range.);

    elseif (Below desired operating range.)else

    disp (Too cold turn off equipment.)end

  • 8/6/2019 Labs-Presentation 3 - Matlab

    34/40

    Flow Control (cont)

    The switch statement:switch expression

    case testexpression 1commands

    case testexpression 2commands

    otherwisecommands

    end Example:

    switch interval < 1case 1

    xinc = interval /10;case 0

    xinc = 0.1;end

  • 8/6/2019 Labs-Presentation 3 - Matlab

    35/40

    Loops

    forloop

    forvariable = expression

    commands

    end

    while loop

    while expression

    commands

    end

    Example (forloop):for t = 1:5000

    y(t) = sin (2*pi*t/10);end

    Example (while loop):EPS = 1;while ( 1+EPS) >1

    EPS = EPS/2;endEPS = 2*EPS

    the break statement

    break is used to terminate the execution of the loop.

  • 8/6/2019 Labs-Presentation 3 - Matlab

    36/40

    M-Files

    The M-file is a text file that consists a group of

    MATLAB commands. MATLAB can open and execute the commands

    exactly as if they were entered at the MATLABcommand window.

    To run th

    e M-files, just type th

    e file name in th

    ecommand window. (make sure the current workingdirectory is set correctly)

    All MATLAB commands are M-files.

    So far, we have executed the commands in the commandwindow.Butamore practicalwayistocreate aM-file.

  • 8/6/2019 Labs-Presentation 3 - Matlab

    37/40

    User-Defined Function

    Add the following command in the beginning of your m-file:

    function [output variables] = function_name (input variables);

    NOTE: the function_name shouldbe the same as your file name to

    avoid confusion.

    calling your function:-- a user-defined function is called by the name of the m-file, notthe name given in the function definition.

    -- type in the m-file name like other pre-defined commands.

    Comments:-- The first few lines should be comments, as they will bedisplayed if help is requested for the function name. the firstcomment line is reference by the lookfor command.

  • 8/6/2019 Labs-Presentation 3 - Matlab

    38/40

    Specific Topics

    This tutorial gives you a general background on the

    usage of MATLAB.

    There are thousands of MATLAB commands for

    many different applications, therefore it is

    impossible to cover all topics here.

    For a specific topic relating to a class, you should

    consult the TA or the Instructor.

  • 8/6/2019 Labs-Presentation 3 - Matlab

    39/40

    Questions?

  • 8/6/2019 Labs-Presentation 3 - Matlab

    40/40

    Topics

    Introduction

    MATLAB Environment

    Getting Help

    Variables

    Vectors, Matrices, and Linear Algebra

    Mathematical Functions and Applications

    Plotting

    Selection Programming

    M-Files

    User Defined Functions

    Specific Topics