Le Langage de Programmation Matlab

  • Upload
    babiso

  • View
    234

  • Download
    0

Embed Size (px)

Citation preview

  • 8/20/2019 Le Langage de Programmation Matlab

    1/26

    Le langage de programmation Matlab :

    Utilisation des scripts 

    Un script se présente sous la forme d'un fichier texte avec l'extension .m

    Quand on lance un script, Matlab exécute une à une les commandes.

    Un script est un programme : Matlab est également un langage de programmation.

    • 1 Premier script

    • ! Affichage à l'écran : Fonction DISP

    • " Saisie a cla!ier : Fonction I"PU#

    • # $ocle F%& 

    • $ $ocle (IL)

    • % IF )LS) )LS)IF

    • & $&)A* 

    • )+emples de scripts 

    o &ésoltion d'ne é,ation d -.me degré /dans &0 espace des

    nombres réels1

    o &ésoltion d'ne é,ation d -.me degré /dans 20 espace desnombres comple+es1

    o 2alcl de la factorielle n3

    o Diagrammes de $ode d'n filtre analogi,e (/p1

    o Diagram mes de $ode d'n filtre nméri,e (/41

     

    (onvention de couleurs :

    http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#1http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#2http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#3http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#4http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#5http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#6http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#7http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#8http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#81http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#81http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#82http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#82http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#83http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#84http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#85http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#2http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#3http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#4http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#5http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#6http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#7http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#8http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#81http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#81http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#82http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#82http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#83http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#84http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#85http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#85http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/matlab_programmation.htm#1

  • 8/20/2019 Le Langage de Programmation Matlab

    2/26

    • en ble : commandes, données ... saisies par l'utilisateur )dans la fen*tre de

    commande+

    • en noir : résultat fourni par Matlab )dans la fen*tre de commande+

    • en roge : script )programme+ 

    Télécharger tous les scripts

    56 Premier script : 7raphe d'ne fonction à ne !ariable

     

    u lieu de taper la série de commandes -ui permet de tracer le graphe de la

    fonction y(x) =100x/((50+x)2 ) :

    88 fplot/'599+;//-1'0?9 -99@1

    88 grid on

    88 +label/'& /en ohms1'1

    88 label/'P /en Batts1'1

    88 title/'2orbe P/&1'1

    vous pouve une fois pour toute, écrire un script.

    /our cela, il vous faut un éditeur de texte )le blocnotes par exemple+ :

    0e signe C signifie -ue le reste de la ligne est un commentaire.

    /our exécuter le script, il suffit de taper son nom dans la fen*tre de commande : 

    88 script5

    http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/script_matlab.ziphttp://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/script_matlab.zip

  • 8/20/2019 Le Langage de Programmation Matlab

    3/26

     

    0a commande (23 34 permet d'afficher à l'écran, les commandes exécutées parle script :

    88 echo on

    88 script5

    C script5m

    fplot/'599+;//-1'0?9 -99@1E

    grid onE

    +label/'& /en ohms1'1E

    label/'P /en Batt1'1E

    title/'2orbe P/&1'1E

    88 echo off 

  • 8/20/2019 Le Langage de Programmation Matlab

    4/26

     

    -6 Affichage à l'écran : fonction DISP

    (ette fonction permet d'afficher proprement des valeurs numéri-ues ou bien dutexte )cha5ne de caract6res+ à l'écran.

     

    • ffichage de nombres :

    88 a5

  • 8/20/2019 Le Langage de Programmation Matlab

    5/26

    88 disp/c1

    $onor

    88 disp/'$onor'1

    $onor

    88 disp/?'Pierre' 'Pal' 'Nac,es'@1

    PierrePalNac,es

    0a commande 70489)n+ affiche n espaces :

    88 disp/?'Pierre' blanOs/51 'Pal' blanOs/51 'Nac,es'@1

    Pierre Pal Nac,es

    • ffichage combiné de textes et de nombres :

    0a fonction 4UM!9; convertit un nombre en une cha5ne de caract6res.

    88 disp/?'Il est ' nm-str/5-1 ' heres'@1

    Il est 5- heres

    88 a5GE

    88 disp/?'Il est ' nm-str/a1 ' heres ' nm-str/

  • 8/20/2019 Le Langage de Programmation Matlab

    6/26

    +9inpt/'#aper la !aler de & min : '1E

    +5inpt/'#aper la !aler de & ma+ : '1E

    disp/?'& min ' nm-str/+91@1E

    disp/?'& ma+ ' nm-str/+51@1E

    fplot/'599+;//-1'0?+9 +5@1 E

    grid on E

    +label/'& /en ohms1'1 E

    label/'P /en Batt1'1 E

    title/'2orbe P/&1'1

    disp/'A re!oir'1E

     

    88 script-

    $onor3

    #aper la !aler de & min : -9

    #aper la !aler de & ma+ : 599

    & min -9

    & ma+ 599

    A re!oir

     

  • 8/20/2019 Le Langage de Programmation Matlab

    7/26

     

    • 9aisie de textes :

    C scrip-5m

    nominpt/'#aper !otre nom : '0's'1E

    prenominpt/'#aper !otre prénom : '0's'1E

    ageinpt/'#aper !otre Qge : '1E

    disp/?nom blanOs/51 prenom@1E

    disp/?'Age : ' nm-str/age1@1E

     

    88 scrip-5

    #aper !otre nom : Dpont

  • 8/20/2019 Le Langage de Programmation Matlab

    8/26

    #aper !otre prénom : NérRme

    #aper !otre Qge : -5

    Dpont NérRme

    Age : -5

     

    6 $ocle F%& 

    (ette instruction permet de répéter un bloc d'instructions, un nombre déterminé de

    fois.

    9

  • 8/20/2019 Le Langage de Programmation Matlab

    9/26

    < -<

     

    C scripJ5m

    ninpt/'"ombre de bocles '1E

    for i5:n0

    disp/i1E

    end

     

    88 scripJ5

    "ombre de bocles

    5

    -

    J

     

    C scriptm

    for i5:-:590

    disp/i1E

    end

     

    88 script

    5

    J

  • 8/20/2019 Le Langage de Programmation Matlab

    10/26

    H

    C script

  • 8/20/2019 Le Langage de Programmation Matlab

    11/26

    9i la condition est vraie, le bloc d'instructions est exécuté, puis le programme teste

    à nouveau la condition ...

     

    ttention aux bugs de programmation >

    si la condition est tou?ours vraie, la boucle s'exécute indéfiniment : le programmeest @planté@.

     

    • 3pérateurs de comparaison :

    égal

    8 supérieur 

    T inférieur 

    8supérieur ou égal

    Tinférieur ou égal

    différent

    xemples :

    C scriptKm

    n9 E

    Bhile /nT5910

    nn=5E

    disp/n1E

    end

     

    0a boucle est exécutée tant -ue n est inférieur à 1A :

    88 scriptK

  • 8/20/2019 Le Langage de Programmation Matlab

    12/26

    5

    -

    J

    <

    K

    G

    H

    59

    • 3pérateurs logi-ues :

    V logi-ue

    )4=+

    W3U logi-ue

    )3;+

       434 logi-ue

     

    C scriptm

    note65E

    Bhile //noteT91 W /note8-9110

    noteinpt/'taper la note /9 à -91 : '1E

    end

    disp/?'note ' nm-str/note1@1E

     

  • 8/20/2019 Le Langage de Programmation Matlab

    13/26

    0a boucle est exécutée tant -ue la note est inférieure à A ou bien supérieure à !A.

    (ela permet de saisir une note forcément comprise entre A et !A :

    88 script

    taper la note /9 à -91 : J9

    taper la note /9 à -91 : 5

    note 5

     

    K6 Instrctions IF )LS) )LS)IF

    9

  • 8/20/2019 Le Langage de Programmation Matlab

    14/26

    0e bloc d'instructions 1 est exécuté si la condition est vraie.

    9inon, c'est le bloc d'instructions ! -ui est exécuté.

     

    • BC 09 BC

    if  condition1 0

    bloc d'instructions 1 E

    else if condition2 0

    bloc d'instructions 2 E

    end

     

    0e bloc d'instructions 1 est exécuté si la condition 1 est vraie.

    9inon, si la condition! est vraie, le bloc d'instructions ! est exécuté.

     

    C scriptGm

    note65E

    Bhile //noteT91 W /note8-9110

    noteinpt/'#aper la note /9 à -91 : '1E

    if /noteT910

    disp /'Xaler in!alide /note T 91'1E

    end

    if /note8-910

    disp /'Xaler in!alide /note 8 -91'1E

    end

  • 8/20/2019 Le Langage de Programmation Matlab

    15/26

    end

    disp/'note '1E

    disp/note1E

     

    88 scriptG

    #aper la note /9 à -91 : 6<

    Xaler in!alide /note T 91

    #aper la note /9 à -91 : -<

    Xaler in!alide /note 8 -91

    #aper la note /9 à -91 : 5-

    note

    5-

     

    6 Instrction $&)A* 

    0'instruction 7;8 permet de sortir d'une boucle C3; ou d'une boucle D2B0.

     

    C script55m

    for i5:590

    if i

  • 8/20/2019 Le Langage de Programmation Matlab

    16/26

    88 script55

    5

    -

    J

     

    C script5-m

    i9E

    Bhile 50

    ii=5E

    if i8

  • 8/20/2019 Le Langage de Programmation Matlab

    17/26

    G6 )+emples de scripts

     

    G656 &ésoltion d'ne é,ation d -.me degré /dans &1

    G6-6 &ésoltion d'ne é,ation d -.me degré /dans 21

    G6J6 2alcl de la factorielle n3

    G66 Diagrammes de $ode d'n filtre analogi,e (/p1

    G6

  • 8/20/2019 Le Langage de Programmation Matlab

    18/26

    deltabb6acE

    if /deltaT910

    disp/'Pas de racine réelle'1E

    end

    if /delta910

    disp/'Une racine doble :'1E

    racine6b;/-a1E

    disp/racine1E

    end

    if /delta8910

    disp/'De+ racines :'1E

    racine5/6b=s,rt/delta11;/-a1E

    racine-/6b6s,rt/delta11;/-a1E

    disp/racine51E

    disp/racine-1E

    end

    choi+inpt/'Un atre calcl /%;"1 '0's'1E

    end

     

    88 trinome

    2alcl dans & des racines de a+Y=b+=c9

    a 9

    Xaler non !alide 3

    a 5

  • 8/20/2019 Le Langage de Programmation Matlab

    19/26

    b -JK

    c 6<

    De+ racines :

    5-

    6JK9

    Un atre calcl /%;"1 n

    G6-6 &ésoltion d'ne é,ation d -.me degré /dans 20espace des nombres comple+es1

     

    C trinome5m

    disp/'2alcl dans 2 des racines de a+Y=b+=c9'1E

    p/51inpt/'a '1E

    p/-1inpt/'b '1E

    p/J1inpt/'c '1E

    disp/'&acines :'1E

    disp/roots/p11E

    88 trinome5

    2alcl dans 2 des racines de a+Y=b+=c9

    a -=i

    b 6<

    c 5

  • 8/20/2019 Le Langage de Programmation Matlab

    20/26

    9GJ-H = 9K99

  • 8/20/2019 Le Langage de Programmation Matlab

    21/26

     

    xemple :

    9oit un filtre passebas du deuxi6me ordre dont voici la fonction de transfert :

     

    88 bode

    2e script Matlab permet de tracer les diagrammes de $ode

    Il nécessite le fichier transm /fichier de définition de la fonction de transfert1

    7amme de fré,ences : f min /en (41 599

    f ma+ /en (41 599999

    "ombre de points 5999

    Figre 5 68 gain en fonction de la fré,ence

    Figre - 68 déphasage en fonction de la fré,ence

     

  • 8/20/2019 Le Langage de Programmation Matlab

    22/26

     

  • 8/20/2019 Le Langage de Programmation Matlab

    23/26

    Télécharger le script

    o!plé!ents

     

    G6

  • 8/20/2019 Le Langage de Programmation Matlab

    24/26

     

    88 bodenm

    2e script Matlab permet de tracer les diagrammes de $ode d'n filtre

    nméri,e

    Il nécessite le fichier transnmm /fichier de définition de la fonction detransfert en 41

    7amme de fré,ences :

    Fe : fré,ence d'échantillonnage

    f min ; Fe 9995

    f ma+ ; Fe 9<

    "ombre de points 5999

    Figre 5 68 Diagramme de $ode d gain

    Figre - 68 Diagramme de $ode d déphasage

    Figre J 68 Amplification en fonction de la fré,ence

    Figre 68 Déphasage en fonction de la fré,ence

     

  • 8/20/2019 Le Langage de Programmation Matlab

    25/26

     

  • 8/20/2019 Le Langage de Programmation Matlab

    26/26

    Télécharger le script

    /21 Fabrice Sinc.re E Xersion 5559

    http://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/script_matlab.ziphttp://fabrice.sincere.pagesperso-orange.fr/matlab/programmation/script_matlab.zip