Page 69 - Chapra y Canale. Metodos Numericos para Ingenieros 5edición_Neat
P. 69

2.5  MATLAB                                                        45


                                         a) Seudocódigo                          b) MATLAB
                                         IF/THEN:
                                         IF condición THEN                       if b ~= 0
                                          Bloque verdadero                         r1 = —c / b;
                                         ENDIF                                   end

                                         IF/THEN/ELSE:
                                         IF condición THEN                       if a < 0
                                          Bloque verdadero                         b = sqrt(abs(a));
                                         ELSE                                    else
                                          Bloque falso                             b = sqrt(a);
                                         ENDIF                                   end

                                         IF/THEN/ELSEIF:
                                         IF condición  THEN                      if class == 1
                                                     1
                                          Bloque                                   x = x + 8;
                                                 1
                                         ELSEIF condición                        elseif class < 1
                                                         2
                                          Bloque                                   x = x – 8;
                                                 2
                                         ELSEIF condición                        elseif class < 10
                                                         3
                                          Bloque                                   x = x — 32;
                                                 3
                                         ELSE                                    else
                                          Bloque                                   x = x — 64;
                                                 4
                                         ENDIF                                   end
                                         CASE:
                                         SELECT CASE Expresión de prueba         switch a + b
                                          CASE Valor                               case 1
                                                     1
                                           Bloque                                    x = —5;
                                                   1
                                          CASE Valor                               case 2
                                                     2
                                           Bloque                                    x = —5 — (a + b) / 10;
                                                   2
                                          CASE Valor                               case 3
                                                     3
                                           Bloque                                    x = (a + b) / 10;
                                                   3
                                          CASE ELSE                                otherwise
                                           Bloque                                    x = 5;
                                                   4
                                         END SELECT                              end
                                         DOEXIT:
                                         DO                                      while (1)
                                          Bloque                                   i = i + 1;
                                                 1
                                           IF condición EXIT                       if i >= 10, break, end
                                          Bloque                                   j = i*x;
                                                 2
                                         ENDIF                                   end
                 FIGURA 2.9
                 Estructuras de control   LOOP CONTROLADO POR CONTADOR:
                 fundamentales en a) seudo-  DOFOR i = inicio, fi n, incremento   for i = 1:10:2
                 código y b) lenguaje de    Bloque                                 x = x + i;

                 programación en MATLAB.  ENDO                                   end






                                                                                                         6/12/06   13:43:45
          Chapra-02.indd   45                                                                            6/12/06   13:43:45
          Chapra-02.indd   45
   64   65   66   67   68   69   70   71   72   73   74