home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / info / calc.info-5 (.txt) < prev    next >
GNU Info File  |  1994-12-22  |  31KB  |  569 lines

  1. This is Info file calc.info, produced by Makeinfo-1.55 from the input
  2. file calc.texinfo.
  3.    This file documents Calc, the GNU Emacs calculator.
  4.    Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  5.    Permission is granted to make and distribute verbatim copies of this
  6. manual provided the copyright notice and this permission notice are
  7. preserved on all copies.
  8.    Permission is granted to copy and distribute modified versions of
  9. this manual under the conditions for verbatim copying, provided also
  10. that the section entitled "GNU General Public License" is included
  11. exactly as in the original, and provided that the entire resulting
  12. derived work is distributed under the terms of a permission notice
  13. identical to this one.
  14.    Permission is granted to copy and distribute translations of this
  15. manual into another language, under the above conditions for modified
  16. versions, except that the section entitled "GNU General Public License"
  17. may be included in a translation approved by the author instead of in
  18. the original English.
  19. File: calc.info,  Node: Basic Algebra Tutorial,  Next: Rewrites Tutorial,  Prev: Algebra Tutorial,  Up: Algebra Tutorial
  20. Basic Algebra
  21. -------------
  22. If you enter a formula in algebraic mode that refers to variables, the
  23. formula itself is pushed onto the stack.  You can manipulate formulas
  24. as regular data objects.
  25.      1:  2 x^2 - 6       1:  6 - 2 x^2       1:  (6 - 2 x^2) (3 x^2 + y)
  26.          .                   .                   .
  27.      
  28.          ' 2x^2-6 RET        n                   ' 3x^2+y RET *
  29.    (*) *Exercise 1.*  Do `' x RET Q 2 ^' and `' x RET 2 ^ Q' both wind
  30. up with the same result (`x')?  Why or why not?  *Note 1: Algebra
  31. Answer 1. (*)
  32.    There are also commands for doing common algebraic operations on
  33. formulas.  Continuing with the formula from the last example,
  34.      1:  18 x^2 + 6 y - 6 x^4 - 2 x^2 y    1:  (18 - 2 y) x^2 - 6 x^4 + 6 y
  35.          .                                     .
  36.      
  37.          a x                                   a c x RET
  38. First we "expand" using the distributive law, then we "collect" terms
  39. involving like powers of `x'.
  40.    Let's find the value of this expression when `x' is 2 and `y' is
  41. one-half.
  42.      1:  17 x^2 - 6 x^4 + 3      1:  -25
  43.          .                           .
  44.      
  45.          1:2 s l y RET               2 s l x RET
  46. The `s l' command means "let"; it takes a number from the top of the
  47. stack and temporarily assigns it as the value of the variable you
  48. specify.  It then evaluates (as if by the `=' key) the next expression
  49. on the stack.  After this command, the variable goes back to its
  50. original value, if any.
  51.    (An earlier exercise in this tutorial involved storing a value in the
  52. variable `x'; if this value is still there, you will have to unstore it
  53. with `s u x RET' before the above example will work properly.)
  54.    Let's find the maximum value of our original expression when `y' is
  55. one-half and `x' ranges over all possible values.  We can do this by
  56. taking the derivative with respect to `x' and examining values of `x'
  57. for which the derivative is zero.  If the second derivative of the
  58. function at that value of `x' is negative, the function has a local
  59. maximum there.
  60.      1:  17 x^2 - 6 x^4 + 3      1:  34 x - 24 x^3
  61.          .                           .
  62.      
  63.          U DEL  s 1                  a d x RET   s 2
  64. Well, the derivative is clearly zero when `x' is zero.  To find the
  65. other root(s), let's divide through by `x' and then solve:
  66.      1:  (34 x - 24 x^3) / x    1:  34 x / x - 24 x^3 / x    1:  34 - 24 x^2
  67.          .                          .                            .
  68.      
  69.          ' x RET /                  a x                          a s
  70.      1:  34 - 24 x^2 = 0        1:  x = 1.19023
  71.          .                          .
  72.      
  73.          0 a =  s 3                 a S x RET
  74. Notice the use of `a s' to "simplify" the formula.  When the default
  75. algebraic simplifications don't do enough, you can use `a s' to tell
  76. Calc to spend more time on the job.
  77.    Now we compute the second derivative and plug in our values of `x':
  78.      1:  1.19023        2:  1.19023         2:  1.19023
  79.          .              1:  34 x - 24 x^3   1:  34 - 72 x^2
  80.                             .                   .
  81.      
  82.          a .                r 2                 a d x RET s 4
  83. (The `a .' command extracts just the righthand side of an equation.
  84. Another method would have been to use `v u' to unpack the equation
  85. `x = 1.19' to `x' and `1.19', then use `M-- M-2 DEL' to delete the `x'.)
  86.      2:  34 - 72 x^2   1:  -68.         2:  34 - 72 x^2     1:  34
  87.      1:  1.19023           .            1:  0                   .
  88.          .                                  .
  89.      
  90.          TAB               s l x RET        U DEL 0             s l x RET
  91. The first of these second derivatives is negative, so we know the
  92. function has a maximum value at `x = 1.19023'.  (The function also has a
  93. local *minimum* at `x = 0'.)
  94.    When we solved for `x', we got only one value even though `34 - 24
  95. x^2 = 0' is a quadratic equation that ought to have two solutions.  The
  96. reason is that `a S' normally returns a single "principal" solution.
  97. If it needs to come up with an arbitrary sign (as occurs in the
  98. quadratic formula) it picks `+'.  If it needs an arbitrary integer, it
  99. picks zero.  We can get a full solution by pressing `H' (the Hyperbolic
  100. flag) before `a S'.
  101.      1:  34 - 24 x^2 = 0    1:  x = 1.19023 s1      1:  x = -1.19023
  102.          .                      .                       .
  103.      
  104.          r 3                    H a S x RET  s 5        1 n  s l s1 RET
  105. Calc has invented the variable `s1' to represent an unknown sign; it is
  106. supposed to be either +1 or -1.  Here we have used the "let" command to
  107. evaluate the expression when the sign is negative.  If we plugged this
  108. into our second derivative we would get the same, negative, answer, so
  109. `x = -1.19023' is also a maximum.
  110.    To find the actual maximum value, we must plug our two values of `x'
  111. into the original formula.
  112.      2:  17 x^2 - 6 x^4 + 3    1:  24.08333 s1^2 - 12.04166 s1^4 + 3
  113.      1:  x = 1.19023 s1            .
  114.          .
  115.      
  116.          r 1 r 5                   s l RET
  117. (Here we see another way to use `s l'; if its input is an equation with
  118. a variable on the lefthand side, then `s l' treats the equation like an
  119. assignment to that variable if you don't give a variable name.)
  120.    It's clear that this will have the same value for either sign of
  121. `s1', but let's work it out anyway, just for the exercise:
  122.      2:  [-1, 1]              1:  [15.04166, 15.04166]
  123.      1:  24.08333 s1^2 ...        .
  124.          .
  125.      
  126.        [ 1 n , 1 ] TAB            V M $ RET
  127. Here we have used a vector mapping operation to evaluate the function
  128. at several values of `s1' at once.  `V M $' is like `V M '' except that
  129. it takes the formula from the top of the stack.  The formula is
  130. interpreted as a function to apply across the vector at the next-to-top
  131. stack level.  Since a formula on the stack can't contain `$' signs,
  132. Calc assumes the variables in the formula stand for different
  133. arguments.  It prompts you for an "argument list", giving the list of
  134. all variables in the formula in alphabetical order as the default list.
  135. In this case the default is `(s1)', which is just what we want so we
  136. simply press RET at the prompt.
  137.    If there had been several different values, we could have used
  138. `V R X' to find the global maximum.
  139.    Calc has a built-in `a P' command that solves an equation using
  140. `H a S' and returns a vector of all the solutions.  It simply automates
  141. the job we just did by hand.  Applied to our original cubic polynomial,
  142. it would produce the vector of solutions `[1.19023, -1.19023, 0]'.
  143. (There is also an `a X' command which finds a local maximum of a
  144. function.  It uses a numerical search method rather than examining the
  145. derivatives, and thus requires you to provide some kind of initial
  146. guess to show it where to look.)
  147.    (*) *Exercise 2.*  Given a vector of the roots of a polynomial (such
  148. as the output of an `a P' command), what sequence of commands would you
  149. use to reconstruct the original polynomial?  (The answer will be unique
  150. to within a constant multiple; choose the solution where the