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

58        Chapter 4: Beyond the Basics


                       r = sqrt(xˆ2 + yˆ2);
                       theta = atan2(y,x);

                     If you type polarcoordinates(3,4), only the first output argument is re-
                     turned and stored in ans; in this case, the answer is 5. To see bothoutputs,
                     you must assign them to variables enclosed in square brackets:
                       >> [r, theta] = polarcoordinates(3,4)

                       r=
                             5

                       theta =
                             0.9273
                     By typing r = polarcoordinates(3,4) you can assign the first output ar-
                     gument to the variable r, but you cannot get only the second output argument;
                     typing theta = polarcoordinates(3,4) will still assign the first output,
                     5,to theta.

           Complex Arithmetic



                     MATLAB does most of its computations using complex numbers, that is, num-
                                                       √
                     bers of the form a + bi, where i =  −1 and a and b are real numbers. The
                     complex number i is represented as i in MATLAB. Although you may never
                     have occasion to enter a complex number in a MATLAB session, MATLAB
                     often produces an answer involving a complex number. For example, many
                     polynomials withreal coefficients have complex roots:
                       >> solve(’xˆ2 + 2*x+2=0’)

                       ans =
                       [ -1+i]
                       [ -1-i]
                       Bothroots of this quadratic equation are complex numbers, expressed in
                     terms of the number i. Some common functions also return complex values
                     for certain values of the argument. For example,
                       >> log(-1)

                       ans =
                             0 + 3.1416i
   72   73   74   75   76   77   78   79   80   81   82