matlab doc

Upload: mayank-laroia

Post on 06-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Matlab Doc

    1/15

    MATLAB

    MATLAB stands for Matrix Laboratory.

    Available in the form of an interpreter, compiler, and a package

    High performance Rapid Application Development tool forScientific Computing

    Available in the form of tool boxes

    Its having an interface of Fortran and C & C++ programminglanguage

    Tool boxes available are:

    MATLAB

    SimulinkStateflow

    Real time workshop

    Control System

    System identification

    Robust Control

    Optimization

    Spline

    Signal Processing

    Mu Analysis

    Neural Network

    Image Processing

    Non Linear Control Design

    Statistics

    Higher Order Spectral Analysis

    Frequency Domain identification

    Model Predictive Control

    Fuzzy Logic

    Digital Signal Processing

    Fixed Point BlocksetQFT Control Design

    LMI Control

    Financial

    Ordinary Differential Equation

    Partial Differential Equation

    Wavelet

    1

  • 8/3/2019 Matlab Doc

    2/15

    Mapping

    Nag Foundation

    Symbolic Math

    Power System Blockset

    Communications

    Data Base

    Other Libraries

    We shall be using the general MATLAB engine.

    In this language, there is no type declaration and no dimensioning of

    variables, each is going to be viewed as a matrix.

    Variables

    Consists of a letter, followed by any number of letters,digits or underscores. Uses only the first 31 characters of a variable name.

    Matlab is case sensitive.

    Numbers

    Imaginary numbers use either i orj as a suffix. E.g 3.105i

    Operators

    + , - , * , / , ^ , , ( )

    Functions

    some special functions provide values of useful constants.

    pi 3.141.

    i imaginary unit -1j same as i

    eps Floating point relative precision 2-52

    realmin Smallest floating-point number 2-1022

    realmax Largest floating-point number (2-)21023

    Inf Infinity

    NaN Not-a-Number

    2

  • 8/3/2019 Matlab Doc

    3/15

    Working with matrices

    Matlab provide functions that generates basic matrices

    ZEROS Zeros array.

    ZEROS(N) is an N-by-N matrix of zeros.

    ZEROS(M,N) or ZEROS([M,N]) is an M-by-N matrix of zeros.

    ZEROS(M,N,P,...) or ZEROS([M N P ...]) is an M-by-N-by-P-by-...

    array of zeros.

    ZEROS(SIZE(A)) is the same size as A and all zeros.

    ONES Ones array.

    ONES(N) is an N-by-N matrix of ones.ONES(M,N) or ONES([M,N]) is an M-by-N matrix of ones.

    ONES(M,N,P,...) or ONES([M N P ...]) is an M-by-N-by-P-by-...

    array of ones.

    ONES(SIZE(A)) is the same size as A and all ones.

    RAND Uniformly distributed random elements from a ( 0 1 ) range.

    RAND(N) is an N-by-N matrix with random entries, chosen from

    a uniform distribution on the interval (0.0,1.0).

    RAND(M,N) and RAND([M,N]) are M-by-N matrices with random

    entries.

    RAND(M,N,P,...) or RAND([M,N,P,...]) generate random arrays.

    RAND with no arguments is a scalar whose value changes each time it

    is referenced. RAND(SIZE(A)) is the same size as A.

    RANDN Normally distributed random elements ( mean is 0 and variance 1)

    RANDN(N) is an N-by-N matrix with random entries, chosen from

    a normal distribution with mean zero and variance one.

    RANDN(M,N) and RANDN([M,N]) are M-by-N matrices with randomentries.

    RANDN(M,N,P,...) or RANDN([M,N,P...]) generate random arrays.

    RANDN with no arguments is a scalar whose value changes each time it

    is referenced. RANDN(SIZE(A)) is the same size as A.

    3

  • 8/3/2019 Matlab Doc

    4/15

    EYE Identity matrix

    EYE(N) is the N-by-N identity matrix.

    EYE(M,N) or EYE([M,N]) is an M-by-N matrix with 1's on

    the diagonal and zeros elsewhere.

    EYE(SIZE(A)) is the same size as A.

    CLEAR

    Clear variables and functions from memory.

    CLEAR removes all variables from the workspace.

    CLEAR VARIABLES does the same thing.

    CLEAR GLOBAL removes all global variables.CLEAR FUNCTIONS removes all compiled M-functions.

    CLEAR MEX removes all links to MEX-files.

    CLEAR ALL removes all variables, globals, functions and MEX links.

    CLEAR VAR1 VAR2 ... clears the variables specified. The wildcard

    character '*' can be used to clear variables that match a pattern.

    For instance, CLEAR X* clears all the variables in the current

    workspace that start with X.

    If X is global, CLEAR X removes X from the current workspace,

    but leaves it accessible to any functions declaring it global.

    CLEAR GLOBAL X completely removes the global variable X.

    CLEAR FUN clears the function specified. If FUN has been locked

    by MLOCK it will remain in memory.

    CLEAR ALL also has the side effect of removing all debugging

    breakpoints since the breakpoints for a file are cleared whenever

    the m-file changes or is cleared.

    Use the functional form of CLEAR, such as CLEAR('name'),

    when the variable name or function name is stored in a string.

    4

  • 8/3/2019 Matlab Doc

    5/15

    Load command

    load filename

    A = [ 2 4

    5 6 ] ;

    Matrices functions

    sum , transpose and diag

    sum(A) gives sum of A column wise

    A gives the transpose of A

    Diag(A) gives the diagonal elements value.

    The colon operator

    Colon plays a very important role in Matlab.

    This occurs in several different forms.

    e.g, 1:10 gives integers 1 to 10

    i.e 1 2 3 4 5 6 7 8 9 10

    100 : -7.50

    and 0 : pi/4 : pi

    subscript expressions

    A(1:k,j) refers first k elements of the jth columns of A.

    sum(A(1:4,4)) gives sum of the fourth column.

    The colon by itself refers to all the elements in a row or column of a matrix

    and the keyword endrefers to the last row or column.

    sum(A(: , end)) gives the sum of the elements in the last column of A.

    5

  • 8/3/2019 Matlab Doc

    6/15

    concatenation

    The pair of [ ] , is the concatenation operator.

    Let us start with 4-by-4 A

    16 3 2 13

    A = 5 10 11 8

    9 6 7 12

    4 15 14 1

    and form B = [ A A+32; A+48 A+16 ]

    The result is an 8-by-8 matrix obtained by joining the four submatrices.

    [ 16 3 2 13 | 48 35 34 45

    5 10 11 8 | 37 42 43 40

    9 6 7 12 | 41 38 39 44

    4 15 14 1 | 36 47 46 33

    --------------------|-----------------------

    B = 64 51 50 61 | 32 19 18 29

    53 58 59 56 | 21 26 27 24

    57 54 55 60 | 25 22 23 28

    52 63 62 49 | 20 31 30 17 ]

    sum(B)

    ans =

    260 260 260 260 260 260 260 260

    Deleting rows and columns

    % storage in Fortran is COLUMN wise

    % storage in C & C++ is ROW wise

    % storage in MATLAB is COLUMN wiseX= A;

    X(: , 2) = []; % deleting the 2nd column of X.

    X(1, 2) = [] % result in an error

    X(2 : 2 : 10) = [ ] % however using a single subscript reshape the

    % remaining elements into a row.

    X = 16 9 2 7 13 12

    6

  • 8/3/2019 Matlab Doc

    7/15

    To log the session journal in a file

    diary filename

    To stop recording

    diary off

    echo on

    echo off

    Help and online documentation

    help topicname or functionname

    Integration , Differentiation

    int(f) or int(f,0,2*pi)

    diff(f)

    syms a b c x

    S= a*x^2+b*x+c;

    int(S)

    ans =

    1/3*a*x^3+1/2*b*x^2+c*x

    diff(S)

    ans =

    2*a*x+b

    7

  • 8/3/2019 Matlab Doc

    8/15

    Solving algebraic equations

    If S is a symbolic expression

    solve(S) find the values of symbolic variable in S.

    syms a b c x

    S= a*x^2+b*x+c;

    solve(S)

    ans =

    [ 1/2/a*(-b+(b^2-4*a*c)^(1/2))]

    [ 1/2/a*(-b-(b^2-4*a*c)^(1/2))]

    ordinary differential equations

    e.g dy

    --- = 1+y^2dx

    dsolve( Dy=1+y^2), gives ans = tan(t + c1)

    with initial conditions y(0)=1

    y = dsolve(Dy=1+y^2,y(0)=1)

    gives y= tan(t + 1/4*pi)

    y = dsolve('Dy=1+y^2','y(0)=1','x')

    gives y = tan(x + 1/4*pi)

    8

  • 8/3/2019 Matlab Doc

    9/15

    Array Construction

    Two important functions arelinspace & logspace

    LINSPACE Linearly spaced vector.

    LINSPACE(x1, x2) generates a row vector of 100

    linearly equally spaced points between x1 and x2.

    LINSPACE(x1, x2, N) generates N points between x1and x2.

    LOGSPACE logarithmically spaced vector.

    LOGSPACE(d1, d2) generates a row vector of 50

    logarithmically equally spaced points between decades

    10^d1 and 10^d2. If d2 is pi, then the points arebetween 10^d1 and pi.

    LOGSPACE(d1, d2, N) generates N points.

    Relational operators

    The six relational operators are =, ==, and

    ~=.

    A < B does element by element comparisons between A

    and B and returns a matrix of the same size with

    elements set to one where the relation is true and

    9

  • 8/3/2019 Matlab Doc

    10/15

    elements set to zero where it is not. A and B must have

    the same dimensions (or one can be a scalar).

    Logical operators

    & Logical AND.

    A & B is a matrix whose elements are 1's where both A

    and B have non-zero elements, and 0's where either has

    a zero element.

    A and B must have the same dimensions (or one can be

    a scalar).

    | Logical OR.

    A | B is a matrix whose elements are 1's where either A

    or B has a non-zero element, and 0's where both have

    zero elements.

    A and B must have the same dimensions (or one can be

    a scalar).

    ~ Logical complement (NOT).

    ~A is a matrix whose elements are 1's where A has zero

    elements, and 0's where A has non-zero elements.

    xor Exclusive OR.

    xor(A,B) is 1 where either A or B, but not both, is non-

    zero.

    10

  • 8/3/2019 Matlab Doc

    11/15

    Control statements

    For

    FOR used to repeat statements a specific number of

    times.

    The general form of a FOR statement is:

    FOR variable = expr, statement, ..., statement END

    The columns of the expression are stored one at a

    time in the variable and then the following statements,

    up to the END, are executed. The expression is often of

    the form X:Y, in which case its columns are simply

    scalars. Some examples

    (assume N has already been assigned a value).

    FOR I = 1 : N,

    FOR J = 1 : N,A (I , J) = 1/(I+J-1);

    END

    END

    FOR S = 1.0 : -0.1 : 0.0, END steps S with increments

    of -0.1

    FOR E = EYE(N), ... END sets E to the unit N-vectors.

    Long loops are more memory efficient when the colon

    expression appears in the FOR statement since the

    index vector is never created.

    11

  • 8/3/2019 Matlab Doc

    12/15

    The BREAK statement can be used to terminate the

    loop prematurely.

    While

    WHILE is used to repeat statements an indefinite

    number of times.

    The general form of a WHILE statement is:

    WHILE expressionstatements

    END

    The statements are executed while the real part of the

    expression has all non-zero elements. The expression is

    usually the result of expr rop expr where rop is ==, , =, or ~=.

    The BREAK statement can be used to terminate the

    loop prematurely.

    For example (assuming A already defined):

    E = 0*A; F = E + eye(size(E)); N = 1;

    while norm(E+F-E,1) > 0,

    E = E + F;F = A*F/N;

    N = N + 1;

    End

    12

  • 8/3/2019 Matlab Doc

    13/15

    If_else_end construction

    IF statement condition.

    The general form of the IF statement isIF expression

    statements

    ELSEIF expression

    statements

    ELSE

    statements

    END

    The statements are executed if the real part of the

    expression has all non-zero elements. The ELSE and

    ELSEIF parts are optional. Zero or more ELSEIF parts

    can be used as well as nested IF's.

    The expression is usually of the form expr rop expr

    where rop is ==, , =, or ~=.

    Example

    if I == J

    A(I,J) = 2;

    elseif abs(I-J) == 1

    A(I,J) = -1;

    else

    A(I,J) = 0;

    end

    13

  • 8/3/2019 Matlab Doc

    14/15

    SWITCH

    The general form of the SWITCH statement is:

    SWITCH switch_expr

    CASE case_expr,

    statement, ..., statement

    CASE {case_expr1, case_expr2, case_expr3,...}

    statement, ..., statement

    ...

    OTHERWISE,

    statement, ..., statementEND

    Solving set of Linear equations

    A .X = b

    This will have a unique answer whenever thedeterminant of the matrix A is non zero.

    i.e A = [ 1 2 3;

    4 5 6;

    7 8 0 ];

    B = [366; 804; 351]

    det(A)

    ans = 27

    14

  • 8/3/2019 Matlab Doc

    15/15

    We can find the solution by a number of methods.

    1. Gaussian elimination

    2. LU factorization (also called left division of A into b,

    X = A\b)

    3. Direct use of A-1

    X = inv(A) * b

    X= [25.00; 22.00; 99.00]

    Runge-Kutta Method

    A fourth order Runge-Kutta method for solving

    simultaneous first order differential equations is given

    by

    Yn+1 = Yn + 1/6(k1 + 2 k2 + 2 k3 + k4 )

    Where k1 = h f(xn , yn )k2 = h f(xn + 1/2h , yn + 1/2 k1)k3 = h f(xn + 1/2h, yn + 1/2 k2 )k4 = h f(xn + h , yn + k3)

    This has a truncation error O(h5) and is useful for

    starting a fourth order multi-step method.

    15