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

54        Chapter 4: Beyond the Basics


                       a=
                               1
                       >> b = a/sym(2)
                       b=
                       1/2

                       MATLAB was designed so that some floating point numbers are restored
                     to their exact values when converted to symbolic. Integers, rational numbers
                     withsmall numerators and denominators, square roots of small integers, the
                     number π, and certain combinations of these numbers are so restored. For
                     example,

                       >> c = sqrt(3)

                       c=
                              1.7321
                       >> sym(c)

                       ans =
                       sqrt(3)

                       Since it is difficult to predict when MATLAB will preserve exact values, it is
                     best to suppress the floating point evaluation of a numeric argument to sym by
                     enclosing it in single quotes to make it a string, e.g., sym(’1 + sqrt(3)’).
                     We will see below another way in which single quotes suppress evaluation.



           Functions and Expressions


                     We have used the terms expression and function without carefully making a
                                                                                     3
                     distinction between the two. Strictly speaking, if we define f (x) = x − 1, then
                                                                                          3
                     f (written without any particular input) is a function while f (x) and x − 1
                     are expressions involving the variable x. In mathematical discourse we often
                                                           3
                     blur this distinction by calling f (x)or x − 1 a function, but in MATLAB the
                     difference between functions and expressions is important.
                       In MATLAB, an expression can belong to either the string or symbolic class
                     of data. Consider the following example:

                       >> f = ’xˆ3 - 1’;
                       >> f(7)

                       ans   =
                       1
   68   69   70   71   72   73   74   75   76   77   78