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

Arithmetic         9




























                                Figure 2-1: MATLAB Output.

                     and “ˆ” to exponentiate. For example,
                       >> 3ˆ2 - (5 + 4)/2 + 6*3

                       ans =
                             22.5000
                       MATLAB prints the answer and assigns the value to a variable called ans.
                     If you want to perform further calculations with the answer, you can use the
                     variable ans rather than retype the answer. For example, you can compute
                     the sum of the square and the square root of the previous answer as follows:

                       >> ansˆ2 + sqrt(ans)

                          ans =
                             510.9934

                       Observe that MATLAB assigns a new value to ans witheachcalculation.
                     To do more complex calculations, you can assign computed values to variables
                     of your choosing. For example,
                       >> u = cos(10)

                       u=
                             -0.8391
   23   24   25   26   27   28   29   30   31   32   33