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

Solving Equations       17


                   ➱ To clear the value of the variable x, type clear x.
                       You can make very general assignments for symbolic variables and then
                     manipulate them. For example,

                       >> clear x; syms x y
                       >> z = xˆ2 - 2*x*y + y


                       z=
                       x^2-2*x*y+y

                       >> 5*y*z

                       ans =
                       5*y*(x^2-2*x*y+y)
                       A variable name or function name can be any string of letters, digits, and
                     underscores, provided it begins witha letter (punctuation marks are not al-
                     lowed). MATLAB distinguishes between uppercase and lowercase letters. You
                     should choose distinctive names that are easy for you to remember, generally
                     using lowercase letters. For example, you might use cubicsol as the name
                     of the solution of a cubic equation.

                   ➱ A common source of puzzling errors is the inadvertent reuse of
                       previously defined variables.

                     MATLAB never forgets your definitions unless instructed to do so. You can
                     check on the current value of a variable by simply typing its name.


           Solving Equations


                     You can solve equations involving variables with solve or fzero. For exam-
                                                                       2
                     ple, to find the solutions of the quadratic equation x − 2x − 4 = 0, type
                       >> solve(’xˆ2 - 2*x-4=0’)


                       ans =
                       [ 5^(1/2)+1]
                       [ 1-5^(1/2)]
                     Note that the equation to be solved is specified as a string; that is, it is sur-
                     rounded by single quotes. The answer consists of the exact (symbolic) solutions
   31   32   33   34   35   36   37   38   39   40   41