Page 71 - A Guide to MATLAB for Beginners and Experienced Users
P. 71

52        Chapter 4: Beyond the Basics


                     array, sometimes called a scalar.A1 × n array is called a row vector, and
                     an m× 1 array is called a column vector. (A string is actually a row vector of
                     characters.) An m× narray of numbers is called a matrix; see More on Matrices
                     below. You can see the class and array size of every variable you have defined
                     by looking in the Workspace browser or typing whos (see Managing Variables
                     in Chapter 2). The set of variable definitions shown by whos is called your
                     Workspace.
                       To use MATLAB commands effectively, you must pay close attention to the
                     class of data eachcommand accepts as input and returns as output. The input
                     to a command consists of one or more arguments separated by commas; some
                     arguments are optional. Some commands, suchas whos, do not require any
                     input. When you type a pair of words, such as hold on, MATLAB interprets
                     the second word as a string argument to the command given by the first word;
                     thus, hold on is equivalent to hold(’on’). The help text (see Online Help in
                     Chapter 2) for each command usually tells what classes of inputs the command
                     expects as well as what class of output it returns.
                       Many commands allow more than one class of input, though sometimes
                     only one data class is mentioned in the online help. This flexibility can be a
                     convenience in some cases and a pitfall in others. For example, the integration
                     command, int, accepts strings as well as symbolic input, though its help
                     text mentions only symbolic input. However, suppose that you have already
                                                                                             2
                                                                                         2
                     defined a=10, b=5, and now you attempt to factor the expression a − b ,
                     forgetting your previous definitions and that you have to declare the variables
                     symbolic:

                       >> factor(aˆ2 - bˆ2)

                       ans =
                              3      5      5

                     The reason you don’t get an error message is that factor is the name of
                     a command that factors integers into prime numbers as well as factoring
                                                          2
                                         2
                                             2
                     expressions. Since a − b = 75 = 3 · 5 , the numerical version of factor is
                     applied. This output is clearly not what you intended, but in the course of a
                     complicated series of commands, you must be careful not to be fooled by such
                     unintended output.
                      Note that typing help factor only shows you the help text for the
                       numerical version of the command, but it does give a cross-reference to the
                       symbolic version at the bottom. If you want to see the help text for the
                       symbolic version instead, type help sym/factor. Functions suchas
                       factor withmore than one version are called overloaded.
   66   67   68   69   70   71   72   73   74   75   76