Page 25 -
P. 25

ble in MATLAB. In fact, ideally, a good MATLAB program will always mini-
                             mize the use of loops because MATLAB is an interpreted language — not a
                             compiled one. As a result, any looping process is very inefficient. Neverthe-
                             less, at times we use the for loops, when necessitated by pedagogical reasons.
                              To understand array operations more clearly, consider the following:

                                a=1:3 % a starts at 1, goes to 3 in increments of 1.
                             If the increment is not 1, you must specify the increment; for example:


                                b=2:2:6 % b starts at 2, goes to 6 in increments of 2
                             To distinguish arrays operations from either operations on scalars or on
                             matrices, the symbol for multiplication becomes .*, that of division ./, and
                             that of exponentiation .^. Thus, for example:

                                c=a.*b % takes every element of a and multiplies
                                   % it by the element of b in the same array location
                             Similarly, for exponentiation and division:


                                d=a.^b
                                e=a./b
                              If you try to use the regular scalar operations symbols, you will get an error
                             message.
                              Note that array operations such as the above require that the two arrays
                             have the same length (i.e., the same number of elements). To verify that two
                             arrays have the same number of elements (dimension), use the length com-
                             mand. Thus, to find the length of a and b, enter:

                                length(a)
                                length(b)

                             NOTE The expression x=linspace(0,10,200) is also the generator for
                             an x-array with first element equal to 0, a last element equal to 10, and having
                             200 equally spaced points between 0 and 100. Here, the number of points
                             rather than the increment is specified; that is, length(x)=200.






                             1.7  Curve and Surface Plotting
                             Review the sections of the Supplement pertaining to lines, quadratic func-
                             tions, and trigonometric functions before proceeding further.


                             © 2001 by CRC Press LLC
   20   21   22   23   24   25   26   27   28   29   30