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

18        Chapter 2: MATLAB Basics

                         √
                     1 ±  5. To get numerical solutions, type double(ans),or vpa(ans) to dis-
                     play more digits.
                       The command solve can solve higher-degree polynomial equations, as well
                     as many other types of equations. It can also solve equations involving more
                     than one variable. If there are fewer equations than variables, you should spec-
                     ify (as strings) which variable(s) to solve for. For example, type solve(’2*x -
                     log(y) = 1’, ’y’) to solve 2x − log y = 1 for y in terms of x. You can
                     specify more than one equation as well. For example,

                       >> [x, y] = solve(’xˆ2-y=2’,’y-2*x=5’)


                       x=
                       [ 1+2*2^(1/2)]
                       [ 1-2*2^(1/2)]

                       y=
                       [ 7+4*2^(1/2)]
                       [ 7-4*2^(1/2)]
                     This system of equations has two solutions. MATLAB reports the solution by
                     giving the two x values and the two y values for those solutions. Thus the first
                     solution consists of the first value of x together with the first value of y.You
                     can extract these values by typing x(1) and y(1):


                       >> x(1)

                       ans =
                       1+2*2^(1/2)
                       >> y(1)

                       ans =
                       7+4*2^(1/2)

                     The second solution can be extracted with x(2) and y(2).
                       Note that in the preceding solve command, we assigned the output to the
                     vector [x, y]. If you use solve on a system of equations without assigning
                     the output to a vector, then MATLAB does not automatically display the values
                     of the solution:

                       >> sol = solve(’xˆ2-y=2’,’y-2*x=5’)
   32   33   34   35   36   37   38   39   40   41   42