Matlab PPTworkshop1

32
Title Page MATLAB Basics Plotting Page 1 of 32 JJ II J I - , Full Screen Search Close MATLAB Workshop Lecture 1 MATLAB Workshop MATLAB Basics Mohamed Taha Communication Engineering Department Princess Sumaya University [email protected]

description

Matlab PPTworkshop1

Transcript of Matlab PPTworkshop1

  • Title Page

    MATLAB Basics

    Plotting

    Page 1 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    MATLAB WorkshopMATLAB Basics

    Mohamed Taha

    Communication Engineering DepartmentPrincess Sumaya University

    [email protected]

  • Title Page

    MATLAB Basics

    Plotting

    Page 2 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    1 MATLAB Basics

    What is MATLAB

    It is an abbreviation to MATrix LABoratory Front end for a matrix library It is an interpreted language It contains huge amount of built in functions and

    accept user defined functions

    Compatible with almost all programming languages(C++, JAVA, .net applications), etc.

    Memory allocations and de-allocations are done au-tomatically

  • Title Page

    MATLAB Basics

    Plotting

    Page 3 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    MATLAB data structures and data types

    Basic data structure is matrix MATLAB calls matrices arrays Basic data type is double

  • Title Page

    MATLAB Basics

    Plotting

    Page 4 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    MATLAB Layout

  • Title Page

    MATLAB Basics

    Plotting

    Page 5 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    Interactive computing with MATLAB

    Using MATLAB as a calculator ans is a default variable used if a variable is not

    specified to hold the result

    At the command prompt

    >> 2+6-4*sin(4)ans = 11.0272

    If we assign the variable x the last statement

    >> x = 2+6-4*sin(4)x = 11.0272

    ans disappeared

  • Title Page

    MATLAB Basics

    Plotting

    Page 6 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    % sign is used to write a comment% This is my first command

    The last statement is totaly ignored by MATLAB

    ; is used to suppress the result, useful when theresult is a huge matrix (image)>>a=1;

    To display the result>> a or disp(a)

  • Title Page

    MATLAB Basics

    Plotting

    Page 7 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    MATLAB vectors

    Row vector A = [1 0 1] is entered in MATLABas A=[1 0 -1]

    spaces are used between the elements

    commas can be used as well

    >> A=[1 0 -1]A = 1 0 -1

    The size and data structure can be checked bytyping >> whose A

    Name Size Bytes Class

    A 1x3 24 double

  • Title Page

    MATLAB Basics

    Plotting

    Page 8 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    Column vector A =

    1

    0

    1

    >> A=[1;0;-1]A = 1

    0-1

    To convert a row vector to a column vector, usetranspose operator

    A = 10

    -1>> B=AB = 1 0 -1

  • Title Page

    MATLAB Basics

    Plotting

    Page 9 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    Matrices

    Matrices are collection of column or row vectors m n matrix, contains m rows and n columns

    The matrix A =

    [1 12 0

    1 j 2

    ]is entered as

    >> A=[1 1/2 0;-1 j 2]A =

    1.0000 0.5000 0-1.0000 0 + 1.0000i 2.0000

    Size of the matrix or vector can be returned as>> [m,n]=size(A)m = 2n = 3

  • Title Page

    MATLAB Basics

    Plotting

    Page 10 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    Accessing the elements of a matrix/vector

    For a vector (column or row), elements can be ac-cessed by specifying the number of the element

    First element is a number 1 and not 0 in the arrayA = [1 0 -1]>> A(1)ans = 1

    >> A(2)ans = 0

    >> A(3)ans = -1

    >> A(end)ans = -1

  • Title Page

    MATLAB Basics

    Plotting

    Page 11 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    For matrix, elements are accessed by specifying theirlocations in the row and column

    A =

    1 1 5j 0 2 + 3 j1 2 7/2

    >> A=[1 -1 5 ; -j 0 2+3*j ; 1 2 -7/2];

  • Title Page

    MATLAB Basics

    Plotting

    Page 12 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    Elements of A are accessed as

    Colon operator : is used to specify range ofcolumns and rows

    1:3, produces [1 2 3]

  • Title Page

    MATLAB Basics

    Plotting

    Page 13 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    Creating vectors automatically

    It is much efficient to create matrices and vectors auto-matically

    Functions used to create matrices can be used to createvectors, opposite is not always possible.

    For vectors:

    x = linspace(StartVal , EndVal, N); Creates a row vector of N linearly spaced numbersbetween StartVal and EndVal

    Increment = difference between any two consec-utive numbers = EndV alStartV alN1 Creates 10 elements between 1 and 5>> x=linspace(-1,5,10);

  • Title Page

    MATLAB Basics

    Plotting

    Page 14 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    The increment can be checked by subtracting anytwo consecutive numbers in x

    >> x(2)-x(1)>> ans = 0.667

    Exactly equals to 5(1)101 = 0.667

    x = StartVal : Increment : EndVal

    Exactly the same as linspace

    x=-1:0.667:5

    Produces the same x produced previously bylinspace

  • Title Page

    MATLAB Basics

    Plotting

    Page 15 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    Creating matrices automatically

    Several functionsFunction Operation

    eye(m,n) m n identity matrixzeros(m,n) m n matrix of zerosones(m,n) m n matrix of onesdiag(m) mm diagonal matrixdiag(m) extract diagonalsrand(m,n) m n randomly generated matrixrandn(m,n) m n randomly generated matrixhilb(m) mm Hilbert matrixmagic(m) mm magic square

  • Title Page

    MATLAB Basics

    Plotting

    Page 16 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    Matrix algebra

    AB, A and B are vectors or matrices of the samesize

    >> A=[1 0 -1; 1/2 j -3];>> B=ones(2,3);>> C=A-B;

    AB, columns {A} = rows {B}

    >> A=[1 0 -1; 1/2 j -3];>> B=ones(2,3);>> C=A*B;

    A. B, multiply the corresponding elements of Aand B, A and B must be of the same size

  • Title Page

    MATLAB Basics

    Plotting

    Page 17 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    >> A=[1 0 -1; 1/2 j -3];>> B=ones(2,3);>> C=A.*BC =

    1.0000 0 -1.00000.5000 0 + 1.0000i -3.0000

    A./B, divide the corresponding elements in A andB, A and B must be of the same size

    >> A=[1 0 -1; 1/2 j -3];>> B=ones(2,3);>> C=A./BC =

    1.0000 0 -1.00000.5000 0 + 1.0000i -3.0000

  • Title Page

    MATLAB Basics

    Plotting

    Page 18 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    Inverse of a matrix A, A is a square, full rank matrix>> A=randn(3);>> B=inv(A)B =

    0.1749 -0.6997 -0.7158-0.2860 0.1538 1.05680.9737 -0.2917 -0.5160

  • Title Page

    MATLAB Basics

    Plotting

    Page 19 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    Matrix functions

  • Title Page

    MATLAB Basics

    Plotting

    Page 20 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    MATLAB built in variables

    MATLAB has several built in variables

    These variables name should not be used for user definedvariables

  • Title Page

    MATLAB Basics

    Plotting

    Page 21 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    User defined variables

    Should starts with characters a-z or A-Z Can have any remaining characters Maximum length 31 Should not have the name of a built in vari-

    able/function or a user defined function

    A is different from a, case sensitive

  • Title Page

    MATLAB Basics

    Plotting

    Page 22 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    Polynomials

    Most engineering problems based on polynomial manip-ulations

    Some functions

    The polynomial f(x) = x2 + 2x + 1 is entered asf=[1 2 1];

    The polynomial g(x) = x2 + 1 is entered as

    g=[1 0 1];

    The value of g(0.5)polyval(g,-0.5)

  • Title Page

    MATLAB Basics

    Plotting

    Page 23 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    c(x) = f(x)g(x) is computed as

    >> f=[1 2 1];>> g=[1 0 1];>> c=conv(f,g)

    c =

    1 2 2 2 1

    The result is interpreted from right to left as

    c(x) = 1 + 2x + 2x2 + 2x3 + x4

  • Title Page

    MATLAB Basics

    Plotting

    Page 24 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    Vectorization

    Transforming any problem to matrix-vector manipula-tion

    Useful in engineering problems

    In this case loops are avoided.

    Consider the C-loop

    pi=3.14;N=5;

    for (i=0; i < N ; i++){

    x[i]=pi*i/4;}

    MATLAB version x=0:pi/4:pi

    Uses 15 of the time required for the C-loop

  • Title Page

    MATLAB Basics

    Plotting

    Page 25 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    2 Plotting

    Plotting (x,y) dataTwo dimensional plots are created with the plotfunction.

    plot(x,y)plot(xdata,ydata,symbol)plot(x1,y1,x2,y2,...)plot(x1,y1,symbol1,x2,y2,symbol2,...)

    x and y must have the same size, x1 and y1 musthave the same size, x2 and y2 must have the samesize, etc.

  • Title Page

    MATLAB Basics

    Plotting

    Page 26 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    Different axis scaling

    Name Axis scaling

    loglog log10(y) versus log10(x)plot linear y versus xsemilogx linear y versus log10(x)semilogy log10(y) versus linear x

  • Title Page

    MATLAB Basics

    Plotting

    Page 27 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    Linear Scale

    >>x=-10:0.1:10;>>y=x.2;>>plot(x,y)>>grid

  • Title Page

    MATLAB Basics

    Plotting

    Page 28 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    Log Scale

    >>x=linspace(0,1,10);>>y=exp(2*x);>>semilogy(x,y)

  • Title Page

    MATLAB Basics

    Plotting

    Page 29 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    Practice

    1. Use colon operator to create a 100 elements vectorbetween 3 and 10 with uniform spacing betweenelements.

    2. For the vector created in (1), square all elements.

    3. Create 4 4 normal random matrix, A. Find a newmatrix, B such that its columns are the diagonal ofthe matrix A.

    Ans. diag(A)*ones(1,4)

    4. Create a 3 3 uniform random matrix, X. Findthe eigen value decomposition of the matrix, X =V V 1.

  • Title Page

    MATLAB Basics

    Plotting

    Page 30 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    5. Create 1 100 random vector with zero mean andunity variance. Check the values of the mean andthe variance using MATLAB commands.

    6. You need to use the randn, mean, var functions.To find help about these functions, at the commandprompt, type, help FunctionName.

    7. Create the matrix1 3 0

    2 9 18 0.5 7

    Write MATLAB commands to:

  • Title Page

    MATLAB Basics

    Plotting

    Page 31 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    Extract the elements of the second row Interchange column 3 and column 1 Subtract row 1 from row 2 and store the result

    in the second row

    Multiply all elements in row 1 and all elementsin column 2 and store the result in row 1

    8. Given the vectors x =[

    1 3 7],

    y =[

    2 4 2], A =

    [3 1 6

    5 2 7

    ], and

    B =

    1 4

    7 8

    2 2

    .Determine which of the following statements willcorrectly execute (and if not, state why). For thecorrectly executed statements give the final result

  • Title Page

    MATLAB Basics

    Plotting

    Page 32 of 32

    JJ IIJ I

    Full Screen

    Search

    Close

    MATLABWorkshopLecture 1

    using MATLAB.

    1. x + y 2.[x; y] 3.B A 4.B./x5.x + A 6.[x; y] 7.B A 8.B./x9.A [x, y] 10.ones(1, 3) A 11.[x; y] + A

    MATLAB BasicsPlotting