part 1 - introduction to matlab

Upload: nhatthang299

Post on 05-Apr-2018

229 views

Category:

Documents


1 download

TRANSCRIPT

  • 7/31/2019 Part 1 - Introduction to Matlab

    1/21

    C.B. Pham 1

    1. Introduction to Matlab

    Matlab: Matrix Laboratory Numerical Computationswith matrices

    Every number can be

    represented as matrix

    Why Matlab?

    User Friendly (GUI)

    Easy to work with

    Powerful tools for complex mathematics.

  • 7/31/2019 Part 1 - Introduction to Matlab

    2/21

  • 7/31/2019 Part 1 - Introduction to Matlab

    3/21

    C.B. Pham 3

    Help in Matlab

    >> help Shows help document for a give function

    Eg. help sqrt

    >> lookfor

    Searches all the help documents for a given keyword

    Eg. lookfor sqrt

    >> demo

  • 7/31/2019 Part 1 - Introduction to Matlab

    4/21

    C.B. Pham 4

    2. Matrix operationsAddition>> c = a + b

    Substraction>> c = a - b

    Multiplication

    >> c = a*b % matrix multiplication>> c = a.*b % element wise multiplication

    Division (left, right)

    >> c = a/b % a * b-1

    >> c = a./b % element wise division>> c = a\b % a-1 * b

    >> c = a.\b % element wise division

  • 7/31/2019 Part 1 - Introduction to Matlab

    5/21

    C.B. Pham 5

    Real>> x = 5x =

    5

    Complex>> x = 5 + 10*ix =

    5.0000 + 10.0000i

    Variable: constant, vector, matrix

    Row vector>> x = [1, 2, 3] % [1 2 3]x =

    1 2 3

    Column vector>> x = [1; 2; 3]x =

    123Matrix

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

    1 2 3

    4 5 6Note: Variable Names are case sensitive

  • 7/31/2019 Part 1 - Introduction to Matlab

    6/21

    C.B. Pham 6

    >> [0 : 0.2 : 1] % 0 to 1 in increments of 0.2ans =

    0.0 0.2 0.4 0.6 0.8 1.0

    Vectors

    >> linspace(0, 1, 6) % 6 points from 0 to 1 on a linear sclans =

    0.0 0.2 0.4 0.6 0.8 1.0

    >> logspace(0, 1, 6) % 6 points from 100 to 101 on a log scl

    ans =

    1.0000 1.5849 2.5119 3.9811 6.3096 10.0000

  • 7/31/2019 Part 1 - Introduction to Matlab

    7/21

    C.B. Pham 7

    Matrix

    >> ones(2, 3) % generates an all one 2 x 3 matrixans =

    1 1 1

    1 1 1

    >> zeros(2, 3) % generates an all zero 2 x 3 matrix

    ans =

    0 0 0

    0 0 0 >> [1 2 3; 4 5 6]ans =

    1 2 34 5 6

  • 7/31/2019 Part 1 - Introduction to Matlab

    8/21

  • 7/31/2019 Part 1 - Introduction to Matlab

    9/21

    C.B. Pham 9

    Accessing matrix elements

    >> M = [ 1 2 3 4; 5 6 7 8; 9 10 11 12]M =

    1 2 3 45 6 7 89 10 11 12

    >> x = M(2, 3) % element at row 2 & column 3 of M

    x =

    7>> y = M(2, :) % select the 2nd row M

    y =

    5 6 7 8>> z = M(2:3, 2:3) % select sub-matrix of Mz =

    6 7

    10 11

  • 7/31/2019 Part 1 - Introduction to Matlab

    10/21

    C.B. Pham 10

    Concatenating, Appending,

    >> A = [ 1 2 3 ] ;>> B = [ 4 5 6 ] ;

    >> R = [ A B ]

    R =

    1 2 3 4 5 6

    >> S = [ A; B ]

    S =

    1 2 3

    4 5 6

    >> S(3, 3) = 7

    S =

    1 2 3

    4 5 60 0 7

    Note: if you store a value in an element outside of the

    matrix, the size increases to accommodate the newcomer.

  • 7/31/2019 Part 1 - Introduction to Matlab

    11/21

    C.B. Pham 11

    Complex number operations

    >> abs(x) % Absolute value

    ans =

    5

    >> x = 3 + 4*i;

    >> angle(x) % Phase angle (in radians)

    ans =

    0.9273

    >> conj(x) % Complex conjugate

    ans =

    3.0000 4.0000i

    >> imag(x) % Complex imaginary part

    ans =

    4

    >> real(x) % Complex real part

    ans =

    3

  • 7/31/2019 Part 1 - Introduction to Matlab

    12/21

    C.B. Pham 12

    Some Useful Functions

    Some useful math functions:sin(x), cos(x), tan(x), atan(x), exp(x), log(x), log10(x),

    sqrt(x), >> t = [0 : 0.1 : 10] ;>> x = sin(2 * pi * t) ;

    Some useful matrix and vector functions:

    >> length(x)ans =

    101

    >> size(x)ans =

    1 101

  • 7/31/2019 Part 1 - Introduction to Matlab

    13/21

    C.B. Pham 13

    >> M = [ 1 2; 5 6 ]

    M =

    1 2

    5 6>> sum(M)ans =

    6 8

    >> sum(ans) % equivalent to sum(sum(M))

    ans =

    14

    >> M % equivalent to transpose(M)

    ans =

    1 5

    2 6

    >> diag(M) % diagonal elements

    ans =

    1

    6

    More Operators and Functions

  • 7/31/2019 Part 1 - Introduction to Matlab

    14/21

    C.B. Pham 14

    Find the roots of the polynomial

    >> f = [ 1 2 3 4 ] ; % create f = x3 + 2x2 + 3x + 4

    >> roots(f)

    ans =

    -1.6506

    -0.1747 + 1.5469i-0.1747 - 1.5469i

    Partial Fraction Expansion

    >> [R, P, K] = residue([ 5 3 ] , [ 1 3 0 -4 ])R = P = K =

    -0.8889 -2.0000 [ ]

    2.3333 -2.0000

    0.8889 1.0000

    43

    35

    23

    xx

    x

    1

    98

    )2(

    37

    2

    98

    2

    xxx

    More Operators and Functions

  • 7/31/2019 Part 1 - Introduction to Matlab

    15/21

    C.B. Pham 15

    3. Plots in Matlab

  • 7/31/2019 Part 1 - Introduction to Matlab

    16/21

    C.B. Pham 16

    The plot function has different forms, depending on the input arguments.

    Continuous time plots - plot

    >> x = [ 0 : 0.1 : 2*pi ] ;

    >> y = sin(x) ;

    >> plot(y) >> plot(x, y)

  • 7/31/2019 Part 1 - Introduction to Matlab

    17/21

    C.B. Pham 17

    Stem function is very similar to plot. It is used to plot discrete time sequences.

    Discrete time plots - stem

    >> x = [ 0 : 0.5 : 10 ] ;

    >> y = x.^2 ;

    >> stem(x, y)

    >> grid on

    >> xlabel(Time, t)

    >> ylabel(Speed, s)

    >> title(Graph s = t^2)

  • 7/31/2019 Part 1 - Introduction to Matlab

    18/21

    C.B. Pham 18

    Multiple Data Sets in One Graph

    Approach 1

    >> plot(t, x, t, y, t, z)

    >> t = [ 0 : 0.001 : 2*pi ] ;

    >> x = sin(t);

    >> y = sin(t - 0.5);

    >> z = sin(t - 1);

    >> grid on

    >> legend(sin(t), sin(t 0.5), sin(t - 1))

    Approach 2

    >> plot(t, x, t, y, t, z)

    >> plot(t, x, b)

    >> hold on

    >> plot(t, y, g)

    >> plot(t, z, r)

    >> hold off

  • 7/31/2019 Part 1 - Introduction to Matlab

    19/21

    C.B. Pham 19

    Plot with two different y-axes

    >> t = [ 0 : 0.001 : 2*pi ] ;

    >> p = sin(2*t);

    >> v = 2*cos(2*t);

    >> plot(t, p)>> plot(t, v)

    >> plotyy(t, p, t, v)

  • 7/31/2019 Part 1 - Introduction to Matlab

    20/21

    C.B. Pham 20

    4. Function in Matlab Syntax

    function [out1, out2, ] = function_name (in1, in2, )

    ..

    .. % commands

    ..

    Description:

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

    function_name that accepts inputs in1, in2, etc. and returns outputs out1,

    out2, etc.

    Note:

    When you create a function in a Matlab .m file and you want to call it from

    the workspace, make sure the name of the .m file is the same as the name

    of the function itself (good programming practice).

  • 7/31/2019 Part 1 - Introduction to Matlab

    21/21

    C.B. Pham 21

    function a = dien_tich(l1, l2, d)

    if (d == 1) % rectangle

    a = l1 * l2 ;

    elseif (d == 2) % triangle

    a = l1 * l2 / 2 ;

    else %others

    a = -1 ;

    end

    Example: make function of area

    a

    b

    A = a.b

    a

    hA = a.h / 2

    File saved as dien_tich.m >> dien_tich(2, 3, 1)

    ans =

    6

    >> dien_tich(2, 3, 2)

    ans =

    3

    >> dien_tich(2, 3, 3)

    ans =

    -1