beginning programming in matlab

Upload: vincent

Post on 08-Oct-2015

42 views

Category:

Documents


1 download

DESCRIPTION

Beginning Programming in MATLAB

TRANSCRIPT

  • 5/19/2018 Beginning Programming in MATLAB

    1/17

    Beginning Programming in

    MATLAB

    MAM1043H

  • 5/19/2018 Beginning Programming in MATLAB

    2/17

    What we know so far?

    Can do some basi math !+,-,*,/,^"

    #now how to set $% arra&s'(etors $sing a

    o)on o%erator !x=1:10

    " P)ot gra%hs !plot(x,y)"

    *ome basi f$ntions !sin(), log()+++"

  • 5/19/2018 Beginning Programming in MATLAB

    3/17

    ,s that not a)) we need??

    - .or the sim%)est %rob)ems/ &es

    B$t+++ its basia))& no different to $sing a

    a)$)ator

    - More om%)e2 a)$)ations re$ire more

    ste%s+

    Command )ine beomes tedio$s

    Key Idea:

    Can enter exactly any MATLAB commands in a

    script file (called a m-file) and rn it from

    t!e command line"

  • 5/19/2018 Beginning Programming in MATLAB

    4/17

    How do we do this?

    - Create a new mfi)e5 !File\New\M-File"

    MATLAB sri%t editor o%ens+

    - T&%e in an& (a)id MATLAB statements+- *a(e the fi)e !eg+ m&test+m"

    - 6$n the mfi)e from the ommand window

    >>mytest

    Mst #e "m7

  • 5/19/2018 Beginning Programming in MATLAB

    5/17

    A 8er& *im%)e 92am%)e

    Create the mfi)e

    92e$te the mfi)e

  • 5/19/2018 Beginning Programming in MATLAB

    6/17

    Creating o$r own f$ntions

    - :sing mfi)es a))ows $s to reate o$r own

    f$ntions+

    - B$t+++ we need to reinter%ret what we

    think of as f$ntions+

  • 5/19/2018 Beginning Programming in MATLAB

    7/17

    ;B)ak Bo27 .$ntions

    - Think of f$ntions as a ;b)ak bo27 that

    %erforms some o%eration5

    ffx y=f(x)

    The o$t%$t (ariab)e

    of the f$ntion5 ;y7

    The in%$t (ariab)e!s" to

    the f$ntion5 ;x7

    The f$ntion

    name5 ;f()7

    The at$a) a)$)ation is %erformed inside

    the b)ak bo2+++

  • 5/19/2018 Beginning Programming in MATLAB

    8/17

    The Anatom& of a .$ntion mfi)e

    A f$ntion mfi)e is

    identified with

    ;fnction7 on the

    first )ine

    The o$t%$t (ariab)e

    of the f$ntion5 ;y7

    The f$ntion

    name5 ;f()7

    The in%$t

    (ariab)e!s" to the

    f$ntion5 ;x7

    The a)$)ation of the o$t%$t (ariab)e/

    de%ending on the in%$t (ariab)e!s"

    *a(e mfi)e as

    ;f"m7

  • 5/19/2018 Beginning Programming in MATLAB

    9/17

    .$ntion mfi)es

    - Muststart with functionon the first)ine+

    - Mustha(e at )east one o$t%$t (ariab)e+

    - Mustha(e a $ni$e name+- Mustsa(e the mfi)e as the f$ntion name+

    - Mustassign a (a)$e to the o$t%$t (ariab)e+

    We will see some examples soon

  • 5/19/2018 Beginning Programming in MATLAB

    10/17

    6e%eating *tatements

    - s method5xn+!

    =xn-f(x

    n)/f&(x

    n)

  • 5/19/2018 Beginning Programming in MATLAB

    11/17

    The for Loo%

    - 'ne of te waysof re%eating statements

    is b& a for )oo%+

    - *tr$t$re5

    for x = Ni:Nf

    ...

    Statements

    end;

    The inde2ing (ariab)e

    *tarting (a)$e

    9nding (a)$e

    The statements to be

    re%eated

  • 5/19/2018 Beginning Programming in MATLAB

    12/17

    .or Loo% 92am%)es

    example$%"m

    for n=1:5 y=n^2;

    disp(y);

    end;

    example$%"m

    for n=1:5 y=n^2;

    disp(y);

    end;

    is%)a&s the s$aresfrom 1 to @

    example$&"m

    N = 5;S = 0;

    for n=1:N

    S = S + n^2;

    end;

    disp('SUM =');

    disp(S)

    example$&"m

    N = 5;S = 0;

    for n=1:N

    S = S + n^2;

    end;

    disp('SUM =');

    disp(S)

    Ca)$)ates the s$m of n

    from 1 to @

    =eed to kee% adding n

    to the s$m/ stored in *

    example$'"m

    N = 5;F = 1;

    for n=1:N

    F = F*n;

    end;

    example$'"m

    N = 5;F = 1;

    for n=1:N

    F = F*n;

    end;

    Ca)$)ates @

  • 5/19/2018 Beginning Programming in MATLAB

    13/17

    Making a .atoria) .$ntion

    .$ntion to a)$)ate =

    Bt is t!ere is a pro#lem it! t!is

    *!at a#ot $+

  • 5/19/2018 Beginning Programming in MATLAB

    14/17

    The .ibonai *e$ene

    Let $s a)$)ate the .ibonai n$mbers5

    0/1/1//3/@//13/1/ DE

    The eah n$mber in the se$ene is

    defined as the s$m of the %re(io$s two

    n$mbers5

    f)= f)-!+ f)-"

  • 5/19/2018 Beginning Programming in MATLAB

    15/17

    The .ibonai *e$ene

    We wi)) store the n$mbers in a etorf

    We a)so know the first two e)ements5

    f=[0 1]; *o we nowterms 1 and / we need to

    start a)$)ating from the 3rdterm+

    ,n MATLAB/ we wo$)d $se5 f(k)=f(k-1)+f(k-2);

    The kth

    term of the (etor

  • 5/19/2018 Beginning Programming in MATLAB

    16/17

    The .ibonai *e$ene

  • 5/19/2018 Beginning Programming in MATLAB

    17/17

    *ti)) to ome

    How an we so)(e )inear s&stem of

    e$ations?

    Making deisions and )ogia) o%erations+

    =$meria))& so)(ing differentia) e$ations+