home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / info / calc.info-8 (.txt) < prev    next >
GNU Info File  |  1994-12-22  |  51KB  |  906 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: Programming Answer 2,  Next: Programming Answer 3,  Prev: Programming Answer 1,  Up: Answers to Exercises
  20. Programming Tutorial Exercise 2
  21. -------------------------------
  22. One way is to move the number to the top of the stack, operate on it,
  23. then move it back:  `C-x ( M-TAB n M-TAB M-TAB C-x )'.
  24.    Another way is to negate the top three stack entries, then negate
  25. again the top two stack entries:  `C-x ( M-3 n M-2 n C-x )'.
  26.    Finally, it turns out that a negative prefix argument causes a
  27. command like `n' to operate on the specified stack entry only, which is
  28. just what we want:  `C-x ( M-- 3 n C-x )'.
  29.    Just for kicks, let's also do it algebraically:
  30. `C-x ( ' -$$$, $$, $ RET C-x )'.
  31. File: calc.info,  Node: Programming Answer 3,  Next: Programming Answer 4,  Prev: Programming Answer 2,  Up: Answers to Exercises
  32. Programming Tutorial Exercise 3
  33. -------------------------------
  34. Each of these functions can be computed using the stack, or using
  35. algebraic entry, whichever way you prefer:
  36. Computing `sin(x) / x':
  37.    Using the stack:  `C-x (  RET S TAB /  C-x )'.
  38.    Using algebraic entry:  `C-x (  ' sin($)/$ RET  C-x )'.
  39. Computing the logarithm:
  40.    Using the stack:  `C-x (  TAB B  C-x )'
  41.    Using algebraic entry:  `C-x (  ' log($,$$) RET  C-x )'.
  42. Computing the vector of integers:
  43.    Using the stack:  `C-x (  1 RET 1  C-u v x  C-x )'.  (Recall that
  44. `C-u v x' takes the vector size, starting value, and increment from the
  45. stack.)
  46.    Alternatively:  `C-x (  ~ v x  C-x )'.  (The `~' key pops a number
  47. from the stack and uses it as the prefix argument for the next command.)
  48.    Using algebraic entry:  `C-x (  ' index($) RET  C-x )'.
  49. File: calc.info,  Node: Programming Answer 4,  Next: Programming Answer 5,  Prev: Programming Answer 3,  Up: Answers to Exercises
  50. Programming Tutorial Exercise 4
  51. -------------------------------
  52. Here's one way:  `C-x ( RET V R + TAB v l / C-x )'.
  53. File: calc.info,  Node: Programming Answer 5,  Next: Programming Answer 6,  Prev: Programming Answer 4,  Up: Answers to Exercises
  54. Programming Tutorial Exercise 5
  55. -------------------------------
  56.      2:  1              1:  1.61803398502         2:  1.61803398502
  57.      1:  20                 .                     1:  1.61803398875
  58.          .                                            .
  59.      
  60.         1 RET 20         Z < & 1 + Z >                I H P
  61. This answer is quite accurate.
  62. File: calc.info,  Node: Programming Answer 6,  Next: Programming Answer 7,  Prev: Programming Answer 5,  Up: Answers to Exercises
  63. Programming Tutorial Exercise 6
  64. -------------------------------
  65. Here is the matrix:
  66.      [ [ 0, 1 ]   * [a, b] = [b, a + b]
  67.        [ 1, 1 ] ]
  68. Thus `[0, 1; 1, 1]^n * [1, 1]' computes Fibonacci numbers `n+1' and
  69. `n+2'.  Here's one program that does the job:
  70.      C-x ( ' [0, 1; 1, 1] ^ ($-1) * [1, 1] RET v u DEL C-x )
  71. This program is quite efficient because Calc knows how to raise a
  72. matrix (or other value) to the power `n' in only `log(n,2)' steps.  For
  73. example, this program can compute the 1000th Fibonacci number (a
  74. 209-digit integer!) in about 10 steps; even though the `Z < ... Z >'
  75. solution had much simpler steps, it would have required so many steps
  76. that it would not have been practical.
  77. File: calc.info,  Node: Programming Answer 7,  Next: Programming Answer 8,  Prev: Programming Answer 6,  Up: Answers to Exercises
  78. Programming Tutorial Exercise 7
  79. -------------------------------
  80. The trick here is to compute the harmonic numbers differently, so that
  81. the loop counter itself accumulates the sum of reciprocals.  We use a
  82. separate variable to hold the integer counter.
  83.      1:  1          2:  1       1:  .
  84.          .          1:  4
  85.                         .
  86.      
  87.          1 t 1       1 RET 4      Z ( t 2 r 1 1 + s 1 & Z )
  88. The body of the loop goes as follows:  First save the harmonic sum so
  89. far in variable 2.  Then delete it from the stack; the for loop itself
  90. will take care of remembering it for us.  Next, recall the count from
  91. variable 1, add one to it, and feed its reciprocal to the for loop to
  92. use as the step value.  The for loop will increase the "loop counter"
  93. by that amount and keep going until the loop counter exceeds 4.
  94.      2:  31                  3:  31
  95.      1:  3.99498713092       2:  3.99498713092
  96.          .                   1:  4.02724519544
  97.                                  .
  98.      
  99.          r 1 r 2                 RET 31 & +
  100.    Thus we find that the 30th harmonic number is 3.99, and the 31st
  101. harmonic number is 4.02.
  102. File: calc.info,  Node: Programming Answer 8,  Next: Programming Answer 9,  Prev: Programming Answer 7,  Up: Answers to Exercises
  103. Programming Tutorial Exercise 8
  104. -------------------------------
  105. The first step is to compute the derivative `f'(x)' and thus the
  106. formula `x - f(x)/f'(x)'.
  107.    (Because this definition is long, it will be repeated in concise form
  108. below.  You can use `M-# m' to load it from there.  While you are
  109. entering a `Z ` Z '' body in a macro, Calc simply collects keystrokes
  110. without executing them.  In the following diagrams we'll pretend Calc
  111. actually executed the keystrokes as you typed them, just for purposes
  112. of illustration.)
  113.      2:  sin(cos(x)) - 0.5            3:  4.5
  114.      1:  4.5                          2:  sin(cos(x)) - 0.5
  115.          .                            1:  -(sin(x) cos(cos(x)))
  116.                                           .
  117.      
  118.      ' sin(cos(x))-0.5 RET 4.5  m r  C-x ( Z `  TAB RET a d x RET
  119.      2:  4.5
  120.      1:  x + (sin(cos(x)) - 0.5) / sin(x) cos(cos(x))
  121.          .
  122.      
  123.          /  ' x RET TAB -   t 1
  124. Now, we enter the loop.  We'll use a repeat loop with a 20-repetition
  125. limit just in case the method fails to converge for some reason.
  126. (Normally, the `Z /' command will stop the loop before all 20
  127. repetitions are done.)
  128.      1:  4.5         3:  4.5                     2:  4.5
  129.          .           2:  x + (sin(cos(x)) ...    1:  5.24196456928
  130.                      1:  4.5                         .
  131.                          .
  132.      
  133.        20 Z <          RET r 1 TAB                 s l x RET
  134.    This is the new guess for `x'.  Now we compare it with the old one
  135. to see if we've converged.
  136.      3:  5.24196     2:  5.24196     1:  5.24196     1:  5.26345856348
  137.      2:  5.24196     1:  0               .               .
  138.      1:  4.5             .
  139.          .
  140.      
  141.        RET M-TAB         a =             Z /             Z > Z ' C-x )
  142.    The loop converges in just a few steps to this value.  To check the
  143. result, we can simply substitute it back into the equation.
  144.      2:  5.26345856348
  145.      1:  0.499999999997
  146.          .
  147.      
  148.       RET ' sin(cos($)) RET
  149.    Let's test the new definition again:
  150.      2:  x^2 - 9           1:  3.
  151.      1:  1                     .
  152.          .
  153.      
  154.        ' x^2-9 RET 1           X
  155.    Once again, here's the full Newton's Method definition:
  156.      C-x ( Z `  TAB RET a d x RET  /  ' x RET TAB -  t 1
  157.                 20 Z <  RET r 1 TAB  s l x RET
  158.                         RET M-TAB  a =  Z /
  159.                    Z >
  160.            Z '
  161.      C-x )
  162.    It turns out that Calc has a built-in command for applying a formula
  163. repeatedly until it converges to a number.  *Note Nesting and Fixed
  164. Points::, to