Matlab Tutorial_General Use

download Matlab Tutorial_General Use

of 44

Transcript of Matlab Tutorial_General Use

  • 8/10/2019 Matlab Tutorial_General Use

    1/44

  • 8/10/2019 Matlab Tutorial_General Use

    2/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    Introduction MATLAB stands for Matrix Laboratory

    The window below appears when MATLAB starts

    exit MATLAB with the command exit or quit

  • 8/10/2019 Matlab Tutorial_General Use

    3/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    Matrices can be introduced into MATLAB

    in several different ways:

    Entered by an explicit list of elements,

    Generated by built-in statements and functions, Created in M-files, Loaded from external data files.

  • 8/10/2019 Matlab Tutorial_General Use

    4/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    (1)Math functions and matrix

    operations:We deal with data in MATLAB as matrices.

    For example, An array of data X= 1, 0, 9, 11, 5 is a 1x5 matrix, A scalar number 9 is an 1x1 matrix.

  • 8/10/2019 Matlab Tutorial_General Use

    5/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    Basic operations : How to define a row matrix (1xn):

    >>X=[1 0 9 11 5] % define row Matrix 1x5% each element can be separated by spaces as shown or by%commas , .

    % Here is The MATLAB echoX =

    1 0 9 11 5NOTES :

    (1) The MATLAB will treat all entries after the% sign as a comment.

    (2) To stop the echo add ";"at the end of the line>> X=[1 0 9 11 5];

  • 8/10/2019 Matlab Tutorial_General Use

    6/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    How to define a column Matrix:

    >>Y=[1; 3; 5; 7; 8] %define column Matrix 5x1

    %Here is the Matlab echo:Y = 1

    357

    8

  • 8/10/2019 Matlab Tutorial_General Use

    7/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    How to define a 2 dimensional Matrix

    Y =1 2 33 6 7

    11 4 7

    >>Y= [1 2 3; 3 6 7; 11 4 7]

    %define 3x3 matrix where each row is separated%by semicolon%Here is the Matlab echo

  • 8/10/2019 Matlab Tutorial_General Use

    8/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    How to get the size of a Matrix:

    Note: ans means answer

    >> Y=[1; 3; 5; 7; 8];% Define column Matrix 5x1

    >> size(Y)% get the size of the Matrix

    % Here is the Matlab echo

    ans=5 1

    % 5 1 => means 5 rows and 1 column

  • 8/10/2019 Matlab Tutorial_General Use

    9/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    This table summarizes most Matricesoperations:

    ExampleDescriptionOperation

    Addition

    +

    Subtract

    -

    Multiply

    *

    Note:Matrix multiplication review is shown in the nextslide

  • 8/10/2019 Matlab Tutorial_General Use

    10/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    Review on matrix multiplication

    Note: To multiply two matrices the number of columns ofthe first matrix must be equal to number of rows in theother matrix

  • 8/10/2019 Matlab Tutorial_General Use

    11/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    ExampleDescriptionOperation

    Element by

    elementmultiplication

    .*

    Matrixtranspose

    >>C = A.*BC= 2 8 18

    0 15 4263 64 0

    >>C = AC= 1 4 7

    2 5 83 6 0

  • 8/10/2019 Matlab Tutorial_General Use

    12/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    Element-by-

    elementdivision

    ./

    Element

    -by-elementpower

    .^

  • 8/10/2019 Matlab Tutorial_General Use

    13/44

  • 8/10/2019 Matlab Tutorial_General Use

    14/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    >>a=[1 3 7 2 0]a = 1 3 7 2 0

    >>b=[0 3 7 7 8]b = 0 3 7 7 8

    % we initialized two matrices a and b>>c=a.*b % element by element multiplication

    c = 0 9 49 14 0>>d=b' % d is a matrix contains the transpose of b

    d = 0

    3778

    >>e=a*d % multiply a[1x5] d[5x1] to give e[1x1]e= 72

    1Example

  • 8/10/2019 Matlab Tutorial_General Use

    15/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    How to get the roots of a Polynomial :EXAMPLE 2:

    >> c=[1 2 3 4 5];% we write the coefficients of the polynomial

    % the row matrix c = [1 2 3 4 5] represents the%polynomial x4 + 2 x3 + 3 x2 + 4 x + 5=0

    >> roots(c)ans= 0.2878 + 1.4161i

    0.2878 - 1.4161i-1.2878 + 0.8579i-1.2878 - 0.8579i

    We want to get the roots of the polynomial x4 + 2 x3 + 3 x2 + 4 x + 5=0 The command roots computes the roots of thepolynomial.

  • 8/10/2019 Matlab Tutorial_General Use

    16/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    EXAMPLE 3:

    >> a=[-1 1] %The row matrix represents the rootsa = -1 1

    >> poly(a) %The command poly(a) computes the%polynomial (x-1)(x+1)=0

    ans = 1 0 -1%the output is the coefficient of the polynomial x^2-1%the coefficient of x^2 is 1, coefficient of x is 0 ,%and the constant is -1

    We want to get the polynomial x^2-1 from its roots-1 , 1

    We use the command poly that computes thepolynomial (x-1)(x+1)=0

    How to get a Polynomials from its roots:

  • 8/10/2019 Matlab Tutorial_General Use

    17/44

    Example function:

    num = [ 1 8 16 9 6]

    den = [ 1 6 11 6][ r , p, k] = r esi due( num, den)

    r =-6.0000-4.0000

    3.0000

    p =-3.0000-2.0000-1.0000

    k =1 2

    4 3 2

    3 2

    ( ) 8 16 9 6( ) 6 11 6

    6 4 323 2 1

    B s s s s s

    A s s s s

    ss s s

    + + + +=

    + + +

    = + + + +

    + + +Constants

    Roots

    Residue

    Partial-fraction expansion function

  • 8/10/2019 Matlab Tutorial_General Use

    18/44

  • 8/10/2019 Matlab Tutorial_General Use

    19/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    (2) 2d Plotting and 3d Plotting:FunctionMATLAB

    Command

    Syntax: start : step size : endEx: 0 : 0.1 : 5So this defines the array0,0.1,0.2,0.3..,4.8,4.9,5Note: this is used in plotting to specify

    the domain ( independent variable )

    Definingarray of

    numbers

    Note: for the function y = f(x)The domain: the values of the independent variable xThe range: values of y

  • 8/10/2019 Matlab Tutorial_General Use

    20/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    FunctionMATLABCommand

    Syntax: Plot (variable1,variable2,marker);plot variable1 versus variable2

    Note: Markers are optionalNote: Markers table is shown next

    Plot

    The plot command can be used to plot twosuperimposed plots on the same graph

    Syntax:Plot (variable1,variable2,variable3,variable4)plot variable1 versus variable2 and variable3versus 4 on the same graph

  • 8/10/2019 Matlab Tutorial_General Use

    21/44

  • 8/10/2019 Matlab Tutorial_General Use

    22/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    FunctionMATLABCommand

    Syntax: Xlabel (name)Labels x-axis with the name in betweenthe quotesNote: name is a string defined between

    the quotes

    Xlabel

    Syntax: Ylabel (name)Labels y-axis with the name in between

    the quotes

    Ylabel

    Syntax: title (name)Labels the graph with the name inbetween the quotes

    Title

  • 8/10/2019 Matlab Tutorial_General Use

    23/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    FunctionMATLABCommand

    Syntax: gridAdds grid to the graphNote: grid command has no arguments

    grid

    Syntax: gtext (name)Labels the graph line with the name inbetween the quotes

    gtext

  • 8/10/2019 Matlab Tutorial_General Use

    24/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    EXAMPLE 4:To plot the function:

    >> t=0:0.1:5;% The range of t from 0 to 5 with an increment of 0.1

    >> y=1./(t-exp(t)).^3+sin(t);% defining function y>> plot(t,y);% plot t on the horizontal axis versus y on the vertical axis

    >> title( Plot of Example); %adds title to the graph>> xlabel(t); ylabel(y) ; grid;>> gtext(1/(t-exp(t)^3+sin(t))

  • 8/10/2019 Matlab Tutorial_General Use

    25/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    output:5Example

  • 8/10/2019 Matlab Tutorial_General Use

    26/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    EXAMPLE 5:

    Two or more plots can be shown on thesame graph as shown in this example.

    In this example, an additional function"y2 is to be shown with function y1

    plotted on the same graph.

  • 8/10/2019 Matlab Tutorial_General Use

    27/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    >>x=0:0.01:1; %array starting from 0 to 1 with step 0.01>>y1=x; % y1 is equal to x, a straight line with slope 45>>y2=x.^2;>>plot(x,y1,bo',x,y2,gx')>>grid;>>xlabel('x')>>ylabel('y1,y2')>>title(Example4)

    Note:Markers are used in the "plot" function for the

    purpose of identification.

  • 8/10/2019 Matlab Tutorial_General Use

    28/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    FunctionMATLABCommand

    Syntax: plot3 (x,y,z)Plot 3d graphs

    It plots x versus y and z

    plot3

    Syntax: view ([1 0 0])

    To show the graph from another viewEx:view([1 0 0])=>Show view parallel tox axis, ie: perpendicular to y-z plane

    view

    Commands used for 3d plotting:

  • 8/10/2019 Matlab Tutorial_General Use

    29/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    Different uses of view command:view ([0 0 1])

    view ([1 0 0])

    view ([0 1 0])

  • 8/10/2019 Matlab Tutorial_General Use

    30/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    3D plotting using plot3 command:

    A 3-D plot is shown in the following example,where two functions sin(4t) , cos(4t) are plottedas parameter t increases from 0 to 5 at anincrement of 0.01.

    >>t=0:0.01:5; % array from 0 to 5 with step 0.01>>plot3(t,sin(4*t),cos(4*t));

    % plot command plot x versus y versus z>>xlabel('t');>>ylabel('sin(4*t)');>>zlabel('cos(4*t)');

    >>grid;

    :6EXAMPLE

  • 8/10/2019 Matlab Tutorial_General Use

    31/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    Example 6 output:

    Note: we can get different views using the viewcommand as illustrated below

  • 8/10/2019 Matlab Tutorial_General Use

    32/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    Add the following line to previous program:>>view([1 0 0])

    >>view([0 1 0])

    C d d f 3d l i

  • 8/10/2019 Matlab Tutorial_General Use

    33/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    FunctionMATLABCommand

    Syntax: [x,y]= meshgrid (array ofnumber1,array of number2)meshgrid function is used to transformthe domain for plotting using the surfcommand.

    meshgrid

    Syntax: surf (f)Where f is function to be plottedsurf

    Commands used for 3d plotting:

  • 8/10/2019 Matlab Tutorial_General Use

    34/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    Meshgrid and surf commands:At each intersection point between array X and array Y the

    function Z is being plotted

  • 8/10/2019 Matlab Tutorial_General Use

    35/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    3d plotting using surf command :

    To generate a 3-D plot using surf command, youfirst need to establish the domains of the "x andy using meshgrid command.

    Note:The meshgrid function is used to transform thedomain for plotting using the surf command.

    EXAMPLE 7:

    In this example, we want to plot the function: z=3sin(y) * cos(x)

  • 8/10/2019 Matlab Tutorial_General Use

    36/44

  • 8/10/2019 Matlab Tutorial_General Use

    37/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    output:7Example

  • 8/10/2019 Matlab Tutorial_General Use

    38/44

    AUC_MENG 375 Prof. Dr. Maki K. HabibMENG 476 and MENG 375

    (3)How to write in an M-file:

    We use m-file to:

    save set of commands to be executed in a singlestep, and

    write and save MATLAB functions.

    Note :We can use any text editor program and save the fileas .m extension

  • 8/10/2019 Matlab Tutorial_General Use

    39/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    Restrictions when saving the m-file:

    We can give any name to the m-file

    But:No spaces are allowed

    No dashes or commas or : or

  • 8/10/2019 Matlab Tutorial_General Use

    40/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    Steps:

    (1) click on the new m-file icon as shown:

    => The window below will appear:

  • 8/10/2019 Matlab Tutorial_General Use

    41/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    Note :

    You can specify the current working directory wherethe file is saved:

    (2) Write the required lines then save and run

    the program

  • 8/10/2019 Matlab Tutorial_General Use

    42/44

    AUC_MENG 375 Prof. Dr. Maki K. Habib

    Here is a simple program written in an m-file

    EXAMPLE 8:A = [1 2; 3 4] %define 2x2 matrixC=A.*A %perform element by elementmultiplication

    Save and run the programHeres the output:

    A =1 23 4

    C =1 49 16

    Note:the output appears in the command window.

  • 8/10/2019 Matlab Tutorial_General Use

    43/44

  • 8/10/2019 Matlab Tutorial_General Use

    44/44

    EXAMPLE 9:An m-file function named add_sub that has 2 inputsx, y and 1 output r, this function performs addition,Subtraction as follows..

    function r=add_sub(x,y)r(1)=x+y;r(2)=x-y;

    Calling the function will directly give the output>> add_sub(10,5)ans =

    15 5