01 intro matlab-basics

Upload: prashanth-balakrishna

Post on 10-Apr-2018

236 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 01 Intro Matlab-Basics

    1/26

    Introduction to Matlab

    [email protected]

    http://www.telecom.tuc.gr

  • 8/8/2019 01 Intro Matlab-Basics

    2/26

    Desktop Tools (Matlab v6)

    Command Window type commands

    Workspace view program variables clear to clear double click on a variable to see it in the Array Editor

    Command History view past commands save a whole session using diary

    Launch Pad access tools, demos and documentation

  • 8/8/2019 01 Intro Matlab-Basics

    3/26

    Matlab Files (.m)

    Use predefined functions or write your own functions

    Reside on the current directory or the search path add with File/Set Path

    Use the Editor/Debugger to edit, run

  • 8/8/2019 01 Intro Matlab-Basics

    4/26

    Matrices

    a vector x = [1 2 5 1]

    x =1 2 5 1

    a matrix x = [1 2 3; 5 1 4; 3 2 -1]

    x =1 2 35 1 4

    3 2 -1

    transpose y = x. y =1251

  • 8/8/2019 01 Intro Matlab-Basics

    5/26

    Matrices

    x(i,j) subscription

    whole row

    whole column

    y=x(2,3)

    y =

    4

    y=x(3,:)

    y =

    3 2 -1

    y=x(:,2)

    y =

    2

    1

    2

  • 8/8/2019 01 Intro Matlab-Basics

    6/26

    Operators (arithmetic)

    + addition- subtraction* multiplication

    / division^ power complex conjugate

    transpose

    .* element-by-element mult

    ./ element-by-element div

    .^ element-by-element power

    . transpose

  • 8/8/2019 01 Intro Matlab-Basics

    7/26

    Operators (relational, logical)

    == equal~= not equal

    < less than greater than>= greater than or equal

    & AND| OR~ NOT

    1pi 3.14159265

    j imaginary unit,

    i same as j

  • 8/8/2019 01 Intro Matlab-Basics

    8/26

    Generating Vectors from functions

    zeros(M,N) MxN matrix of zeros

    ones(M,N) MxN matrix of ones

    rand(M,N) MxN matrix of uniformlydistributed random numberson (0,1)

    x = zeros(1,3)

    x =

    0 0 0

    x = ones(1,3)x =

    1 1 1

    x = rand(1,3)

    x =0.9501 0.2311 0.6068

  • 8/8/2019 01 Intro Matlab-Basics

    9/26

    Operators

    [ ] concatenation

    ( ) subscription

    x = [ zeros(1,3) ones(1,2) ]

    x =

    0 0 0 1 1

    x = [ 1 3 5 7 9]

    x =

    1 3 5 7 9

    y = x(2)y =

    3

    y = x(2:4)

    y =

    3 5 7

  • 8/8/2019 01 Intro Matlab-Basics

    10/26

    Matlab Graphics

    x = 0:pi/100:2*pi;

    y = sin(x);

    plot(x,y)xlabel('x = 0:2\pi')

    ylabel('Sine of x')

    title('Plot of theSine Function')

  • 8/8/2019 01 Intro Matlab-Basics

    11/26

    Multiple Graphs

    t = 0:pi/100:2*pi;

    y1=sin(t);

    y2=sin(t+pi/2);

    plot(t,y1,t,y2)

    grid on

  • 8/8/2019 01 Intro Matlab-Basics

    12/26

    Multiple Plots

    t = 0:pi/100:2*pi;

    y1=sin(t);

    y2=sin(t+pi/2);

    subplot(2,2,1)

    plot(t,y1)

    subplot(2,2,2)

    plot(t,y2)

  • 8/8/2019 01 Intro Matlab-Basics

    13/26

    Graph Functions (summary)

    plot linear plot stemdiscrete plot

    grid add grid lines xlabel add X-axis label ylabel add Y-axis label title add graph title

    subplot divide figure window figure create new figure window pause wait for user response

  • 8/8/2019 01 Intro Matlab-Basics

    14/26

    Math Functions

    Elementary functions (sin, cos, sqrt, abs, exp, log10, round) type help elfun

    Advanced functions (bessel, beta, gamma, erf) type help specfun

    type help elmat

  • 8/8/2019 01 Intro Matlab-Basics

    15/26

    Functions

    function f=myfunction(x,y)

    f=x+y;

    save it in myfunction.m call it with y=myfunction(x,y)

  • 8/8/2019 01 Intro Matlab-Basics

    16/26

    Flow Control

    if A > B

    'greater'

    elseif A < B

    'less'

    else'equal'

    end

    for x = 1:10r(x) = x;

    end

    if statement switch statement

    for loops while loops

    continue statement

    break statement

  • 8/8/2019 01 Intro Matlab-Basics

    17/26

    Miscellaneous

    Loading data from a file load myfile.dat

    Suppressing Output x = [1 2 5 1];

  • 8/8/2019 01 Intro Matlab-Basics

    18/26

    Getting Help

    Using the Help Browser (.html, .pdf) View getstart.pdf, graphg.pdf, using_ml.pdf

    Type help

    help function , e.g. help plot

    Running demos type demos type help demos

  • 8/8/2019 01 Intro Matlab-Basics

    19/26

    Random Numbers

    x=rand(100,1);

    stem(x);

    hist(x,100)

  • 8/8/2019 01 Intro Matlab-Basics

    20/26

    Coin Tosses

    Simulate the outcomes of 100 fair coin tossesx=rand(100,1);p=sum(x

  • 8/8/2019 01 Intro Matlab-Basics

    21/26

    Coin Tosses

    Simulate the outcomes of 1000 biased coin tosses withp[Head]=0.4

    x=rand(1000,1);

    p=sum(x

  • 8/8/2019 01 Intro Matlab-Basics

    22/26

    Sum of Two Dies

    Simulate 10000 observations of the sum of two fair dies

    .

    ..

    .

    .

    . . . . .

    .

    . . . .. .

    ... .

    .

    . ..

    .

    ...

    .. .. .. .

    (1,1) (2,1) (3,1) (4,1) (5,1) (6,1)

    (3,2) (4,2) (5,2) (6,2)

    (3,3) (4,3) (5,3) (6,3)

    (2,2)(1,2)

    (2,3)(1,3)

    (1,4) (2,4) (3,4) (4,4) (5,4) (6,4)

    (3,5) (4,5) (5,5) (6,5)(2,5)(1,5)

    (3,6) (4,6) (5,6) (6,6)(2,6)(1,6)

    1 2 3 4 5 6

    1

    2

    3

    4

    5

    6

  • 8/8/2019 01 Intro Matlab-Basics

    23/26

    Sum of Two Dies

    Simulate 10000 observations of the sum of two fair dies

    x1=floor(6*rand(10000,1)+1);x2=floor(6*rand(10000,1)+1);y=x1+x2;

    sum(y==2)/10000 ans = 0.0275 p[2]=0.0278sum(y==3)/10000 ans = 0.0554 p[3]=0.0556sum(y==4)/10000 ans = 0.0841 p[4]=0.0833sum(y==5)/10000 ans = 0.1082 p[5]=0.1111sum(y==6)/10000 ans = 0.1397 p[6]=0.1389sum(y==7)/10000 ans = 0.1705 p[7]=0.1667sum(y==8)/10000 ans = 0.1407 p[8]=0.1389sum(y==9)/10000 ans = 0.1095 p[9]=0.1111sum(y==10)/10000 ans = 0.0794 p[10]=0.0833sum(y==11)/10000 ans = 0.0585 p[11]=0.0556sum(y==12)/10000 ans = 0.0265 p[12]=0.0278

  • 8/8/2019 01 Intro Matlab-Basics

    24/26

    Sum of Two Dies

    for i=2:12

    z(i)=sum(y==i)/10000

    end

    bar(z)

  • 8/8/2019 01 Intro Matlab-Basics

    25/26

    Bernoulli Trials-Binomial Distribution

    k=0:20;

    y=binopdf(k,20,0.5);

    stem(k,y)

    ( ) (1 ) , 0,1,...,k n k n p k p p k nk

    -

    = - =

    Bernoulli 1720

    k=0:20;

    y=binopdf(k,20,0.2);

    stem(k,y)

    20 0.5n p= = 20 0.1n p= =

  • 8/8/2019 01 Intro Matlab-Basics

    26/26

    Combinatorics

    Permutations: n objects

    Permutations: choose k objects from n(hint: fill 2 spaces on a bookshelf with books chosen from 5 available books)

    Combinations: choose k objects from n without

    regard to the order(hint: make a committee of 2 people chosen from a group of 5 people)

    !n

    !

    ( )!n

    k

    n P

    n k =

    -

    !

    !( )!

    n nk k n k

    =

    -

    n n

    k n k

    =

    -

    !1

    0 0! !

    n n n

    n n

    = = =

    !

    1 1 1!( 1)!

    n n nn

    n n

    = = =

    - -