{"id":270,"date":"2020-12-09T22:52:43","date_gmt":"2020-12-09T22:52:43","guid":{"rendered":"http:\/\/med-space.org\/youssef-zaz-master\/?page_id=270"},"modified":"2020-12-12T14:23:18","modified_gmt":"2020-12-12T14:23:18","slug":"matlab-boucles-et-conditions","status":"publish","type":"page","link":"https:\/\/yzaz.net\/master\/matlab-boucles-et-conditions\/","title":{"rendered":"Matlab &#8211; Fonctions, boucles et conditions"},"content":{"rendered":"<h2>Demande d&#8217;aide:<\/h2>\n<p>Les deux commandes les plus utilis\u00e9es par Matlab pour demander l&#8217;aide:<\/p>\n<pre class=\"brush: matlabkey; title: ; notranslate\" title=\"\">\r\nhelp NomFonction % NomFonction: la commande souhait\u00e9e \r\nlookfor keyword % keyword : la mot cl\u00e9 recherch\u00e9\r\n<\/pre>\n<p><strong><br \/>\nExemple de calcul des integrales avec la m\u00e9thodes des trap\u00e8zes:<\/strong><\/p>\n<p style=\"text-align: center;\"><strong><span class=\"katex-eq\" data-katex-display=\"false\">\n\n\\int ^{10}_{0}\\left(\\frac{1}{2}\\sqrt{x} \\ +x\\ sin( x)\\right) \\ dx\n\n<\/span><\/strong><\/p>\n<pre class=\"brush: matlabkey; title: ; notranslate\" title=\"\">\r\nx = 0:0.5:10; y = 0.5 * sqrt(x) + x .* sin(x);\r\nintegral1 = trapz(x,y)\r\n<\/pre>\n<pre>\r\nintegral1 =\r\n18.1655\r\n<\/pre>\n<h2>Ecrire des fonctions d\u00e9finies par l&#8217;utilisateur<\/h2>\n<p>Les fonctions sont des fichiers m qui peuvent \u00eatre ex\u00e9cut\u00e9s en sp\u00e9cifiant certaines entr\u00e9es et fournir certaines sorties souhait\u00e9es.<br \/>\nLe code indiquant au Matlab qu&#8217;un fichier m est :<\/p>\n<p>function out1=NomFonction (in1)<br \/>\nfunction out1=NomFonction (in1,in2,in3)<br \/>\nfunction [out1,out2]=NomFonction (in1,in2)<\/p>\n<p>Vous devez indiquer cette commande au d\u00e9but du fichier m et vous devez enregistrer le fichier m avec un nom de fichier identique au nom de la fonction.<br \/>\n<strong>Exemple 1:<\/strong><br \/>\nUne fonction qui prend un tableau d&#8217;entr\u00e9e et renvoie la somme et le produit de ses \u00e9l\u00e9ments en tant que sorties:<\/p>\n<pre class=\"brush: matlabkey; title: ; notranslate\" title=\"\">\r\nfunction [a,b]=somprod(tab)\r\na=sum(tab);\r\nb=prod(tab);\r\n<\/pre>\n<p>La fonction sumprod (.) Peut \u00eatre appel\u00e9e depuis la fen\u00eatre de commande ou un .m file:<br \/>\n[x,y]=somprod(c)<\/p>\n<p><strong>Exemple 2:<\/strong><br \/>\nL&#8217;aire A d&#8217;un triangle dont la longueur des c\u00f4t\u00e9s a, b et c est donn\u00e9e par:<br \/>\n<span class=\"katex-eq\" data-katex-display=\"false\">A=\\sqrt{s( s-a)( s-b)( s-c) \\ } \\ avec\\ s=( a+b+c) \/2<\/span><\/p>\n<pre class=\"brush: matlabkey; title: ; notranslate\" title=\"\">\r\nfunction [A] = area(a,b,c)\r\ns = (a+b+c)\/2;\r\nA = sqrt(s*(s-a)*(s-b)*(s-c));\r\n<\/pre>\n<p>Pour \u00e9valuer l&#8217;aire d&#8217;un triangle de c\u00f4t\u00e9 de longueur 10, 15, 20:<\/p>\n<pre>Area = area(10,15,20)\r\nArea =\r\n72.6184\r\n<\/pre>\n<h2>Les conditions:<\/h2>\n<p>if (<em>Condition_1<\/em>)<br \/>\nCommandes Matlab<br \/>\nelseif (<em>Condition_2<\/em>)<br \/>\nCommandes Matlab<br \/>\nelseif (<em>Condition_3<\/em>)<br \/>\nCommandes Matlab<br \/>\nelse<br \/>\nCommandes Matlab<br \/>\nend<\/p>\n<p><strong>Exemple:<\/strong><\/p>\n<p>R\u00e9solution des \u00e9quations du 2<sup>\u00e8me<\/sup> degr\u00e9 en R: <span class=\"katex-eq\" data-katex-display=\"false\"> ax^{2} +bx+c=0 <\/span><\/p>\n<pre class=\"brush: matlabkey; title: ; notranslate\" title=\"\">\r\n% trinome.m\r\ndisp('Solve ax\u00b2+bx+c=0');\r\nchoix='Y';\r\nwhile (choix~='N' &amp; choix~='n')\r\na=input('a=? ');\r\nb=input('b=? ');\r\nc=input('c=? ');\r\ndelta=b*b-4*a*c;\r\nif (delta&lt;0),\r\ndisp('No solution');\r\nend\r\nif (delta==0),\r\ndisp('1 solution:');\r\nracine=-b\/(2*a);\r\ndisp(racine);\r\nend\r\nif (delta&gt;0),\r\ndisp('2 solutions');\r\nracine1=(-b+sqrt(delta))\/(2*a);\r\nracine2=(-b-sqrt(delta))\/(2*a);\r\ndisp(racine1);\r\ndisp(racine2); \r\nend\r\nchoix=input('Another equation (Y\/N)? ','s');\r\nend\r\n<\/pre>\n<p>R\u00e9solution des \u00e9quations du 2<sup>\u00e8me<\/sup> degr\u00e9 en C<\/p>\n<pre class=\"brush: matlabkey; title: ; notranslate\" title=\"\">\r\n% trinome1.m\r\ndisp('Solve ax\u00b2+bx+c=0');\r\np(1)=input('a=? ');\r\np(2)=input('b=? ');\r\np(3)=input('c=? ');\r\ndisp('Solutions :');\r\ndisp(roots(p)); \r\n<\/pre>\n<h2>Les Boucles<\/h2>\n<p>for i=Index_Array<br \/>\nCommandes Matlab<br \/>\nend<\/p>\n<p><strong>Exemple:<\/strong><\/p>\n<pre class=\"brush: matlabkey; title: ; notranslate\" title=\"\">\r\nx = -1:.05:1;\r\nfor n = 1:8\r\nsubplot(4,2,n);\r\nplot(x,sin(n*pi*x));\r\nend\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-274 size-full\" src=\"http:\/\/med-space.org\/youssef-zaz-master\/wp-content\/uploads\/2020\/12\/untitled-1.jpg\" alt=\"\" width=\"560\" height=\"420\" srcset=\"https:\/\/yzaz.net\/master\/wp-content\/uploads\/2020\/12\/untitled-1.jpg 560w, https:\/\/yzaz.net\/master\/wp-content\/uploads\/2020\/12\/untitled-1-300x225.jpg 300w, https:\/\/yzaz.net\/master\/wp-content\/uploads\/2020\/12\/untitled-1-326x245.jpg 326w, https:\/\/yzaz.net\/master\/wp-content\/uploads\/2020\/12\/untitled-1-80x60.jpg 80w\" sizes=\"(max-width: 560px) 100vw, 560px\" \/><\/p>\n<h2>Boucle While:<\/h2>\n<p>while (condition)<br \/>\nCommandes Matlab<br \/>\nend<\/p>\n<h2>Commande SWITCH<\/h2>\n<p>Basculer entre plusieurs cas en fonction de l&#8217;expression.<br \/>\nLa forme g\u00e9n\u00e9rale de l&#8217;instruction SWITCH est:<br \/>\nSWITCH switch_expr<br \/>\nCASE case_expr,<br \/>\nstatement, \u2026, statement<br \/>\nCASE {case_expr1, case_expr2, case_expr3, \u2026}<br \/>\nstatement, \u2026, statement<br \/>\n\u2026<br \/>\nOTHERWISE<br \/>\nstatement, \u2026, statement<br \/>\nEND<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Demande d&#8217;aide: Les deux commandes les plus utilis\u00e9es par Matlab pour demander l&#8217;aide: Exemple de calcul des integrales avec la m\u00e9thodes des trap\u00e8zes: integral1 = <a class=\"mh-excerpt-more\" href=\"https:\/\/yzaz.net\/master\/matlab-boucles-et-conditions\/\" title=\"Matlab &#8211; Fonctions, boucles et conditions\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":209,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/yzaz.net\/master\/wp-json\/wp\/v2\/pages\/270"}],"collection":[{"href":"https:\/\/yzaz.net\/master\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/yzaz.net\/master\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/yzaz.net\/master\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yzaz.net\/master\/wp-json\/wp\/v2\/comments?post=270"}],"version-history":[{"count":23,"href":"https:\/\/yzaz.net\/master\/wp-json\/wp\/v2\/pages\/270\/revisions"}],"predecessor-version":[{"id":294,"href":"https:\/\/yzaz.net\/master\/wp-json\/wp\/v2\/pages\/270\/revisions\/294"}],"wp:attachment":[{"href":"https:\/\/yzaz.net\/master\/wp-json\/wp\/v2\/media?parent=270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}