Mann TA Tutorial

Embed Size (px)

Citation preview

  • 7/27/2019 Mann TA Tutorial

    1/21

    MATLAB Basic

  • 7/27/2019 Mann TA Tutorial

    2/21

    2

    What is MATLAB?

    A software environment for interactive numerical computations.

    MATLAB stands forMATrix LABoratory

    Examples:

    Matrix computations and linear algebra

    Solving nonlinear equations Numerical solution of differential equations

    Mathematical optimization

    Statistics and data analysis

    Signal processing

  • 7/27/2019 Mann TA Tutorial

    3/21

    3

    Matlab Windows

    o Command line

    Interface ( Main

    Window)

    o Editor Window

    o Present Directory

    o Directory Contents

    and Workspace

    variables

    o Command line

    o Command History

  • 7/27/2019 Mann TA Tutorial

    4/21

  • 7/27/2019 Mann TA Tutorial

    5/21

  • 7/27/2019 Mann TA Tutorial

    6/21

    6

    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 rows

    and the number of columns, with the number ofrows mentioned first (For example: n x m array).

    Total number of elements in an array is theproduct of the number of rows and the number ofcolumns.

  • 7/27/2019 Mann TA Tutorial

    7/21

    7

    MATLAB BASICSMATLAB BASICS

    1 2

    3 4

    5 6

    a= 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 #

  • 7/27/2019 Mann TA Tutorial

    8/21

    8

    MATLAB BASICSMATLAB BASICS

    Variables

    A region of memory containing an array, which is known

    by a user-specified name.

    Contents can be used or modified at any time.

    Variable names must begin with a letter, followed by anycombination of letters, numbers and the underscore (_)

    character.

    The MATLAB language is Case Sensitive. NAME, name

    and Name are all different variables.

  • 7/27/2019 Mann TA Tutorial

    9/21

    9

    Initializing Variables in Assignment Statements

    An assignment statement has the general form

    var = expressionExamples:

    >> var = 40 * i; >> a2 = [0 1+8];

    >> var2 = var / 5; >> b2 = [a2(2) 7 a];

    >> array = [1 2 3 4]; >> c2(2,3) = 5;

    >> x = 1; y = 2; >> d2 = [1 2];

    >> a = [3.4]; >> d2(4) = 4;

    >> b = [1.0 2.0 3.0 4.0];

    >> c = [1.0; 2.0; 3.0];

    >> d = [1, 2, 3; 4, 5, 6]; ; semicolon suppresses the

    >> e = [1, 2, 3 automatic echoing of values but

    4, 5, 6]; it slows down the execution.

    MATLAB BASICSMATLAB BASICS

  • 7/27/2019 Mann TA Tutorial

    10/21

    10

    MATLAB BASICSMATLAB BASICS

    Init ializing with Shortcut Expressions

    first: increment: last

    Colon operator: a shortcut notation used to initializearrays with thousands of elements

    >> x = 1 : 2 : 10;

    >> angles = (0.01 : 0.01 : 1) * pi;

    Transpose operator: () swaps the rows and columnsof an array

    >> f = [1:4];

    >> g = 1:4;

    >> h = [ g g ];

    1 1

    2 2

    3 3

    4 4

    h=

  • 7/27/2019 Mann TA Tutorial

    11/21

    11

    Initializing with Keyboard Input

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

    my_val = input( Enter an input value: );

    in1 = input( Enter data: );

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

    MATLAB BASICSMATLAB BASICS

  • 7/27/2019 Mann TA Tutorial

    12/21

    12

    MATLAB BASICSMATLAB BASICS

    A MATLAB script file (Called an M-file) is a text file thatcontains one or more MATLAB commands and,

    optionally, comments.

    The file is saved with the extension ".m".

    When the filename (without the extension) is issued as acommand in MATLAB, the file is opened, read, and the

    commands are executed as if input from the keyboard. The

    result is similar to running a program in C.

    MATLAB Script Files

  • 7/27/2019 Mann TA Tutorial

    13/21

    13

    MATLAB BASICSMATLAB BASICS

    A MATLAB function file (called an M-file) is a text file

    that contains a MATLAB function and, optionally,

    comments.

    The file is saved with thefunction name and the usualMATLAB script file extension, ".m".

    A MATLAB function may be called from the commandline or from any other M-file.

    MATLAB Function Files

  • 7/27/2019 Mann TA Tutorial

    14/21

    14

    MATLAB BASICSMATLAB BASICS

    When the function is called in MATLAB, the file is

    accessed, the function is executed, and control is returnedto the MATLAB workspace.

    Since the function is not part of the MATLAB workspace,its variables and their values are not known after control is

    returned.

    Any values to be returned must be specified in the function

    syntax.

    MATLAB Function Files

  • 7/27/2019 Mann TA Tutorial

    15/21

    15

    MATLAB BASICSMATLAB BASICS

    FUNCTIONS in MATLAB

    Syntaxfunction [out1, out2, ...] = funname(in1, in2, ...)

    Description

    function [out1, out2, ...] = funname(in1, in2, ...)

    defines function funname that accepts inputs in1, in2, etc.

    and returns outputs out1, out2, etc.

  • 7/27/2019 Mann TA Tutorial

    16/21

    16

    MATLAB BASICSMATLAB BASICS

    EXAMPLE:

    function [det_A]=solvexf(r) ;

    A=[5 2*r r ; 3 6 2*r-1 ; 2 r-1 3*r];

    det_A=det(A);

    Calling the function:

    [detA]=solvexf(1)

  • 7/27/2019 Mann TA Tutorial

    17/21

  • 7/27/2019 Mann TA Tutorial

    18/21

    18

    MATLAB BASICSMATLAB BASICS

    FOR statement

    for a=0:50disp('Hello World')

    end

    for a=10:10:50

    for b=0:0.1:1

    disp('MATLAB')

    end

    end

  • 7/27/2019 Mann TA Tutorial

    19/21

    19

    MATLAB BASICSMATLAB BASICS

    Summary help command Online help

    lookfor keyword Lists related commands which Version and location info

    clear Clears the workspace

    clc Clears the command window

    who, whos Lists content of the workspace Ctrl+c Aborts operation

    Continuation

    % Comments

  • 7/27/2019 Mann TA Tutorial

    20/21

    20

    MATLAB BASICSMATLAB BASICS

    -1

    2

    -2

    1

    1 2

    x(t)

    PROBLEM 1-

    USE MATLAB TO PLOT THIS SIGNAL

  • 7/27/2019 Mann TA Tutorial

    21/21

    21

    MATLAB BASICSMATLAB BASICS

    UNIT STEP

    FUNCTION

    UNIT STEP

    DELAYED BY t0