v15i041: Patch for GNU Emacs Calc, version 1.04 -> 1.05, part 14/20
David Gillespie
daveg at csvax.cs.caltech.edu
Mon Oct 15 11:19:30 AEST 1990
Posting-number: Volume 15, Issue 41
Submitted-by: daveg at csvax.cs.caltech.edu (David Gillespie)
Archive-name: calc-1.05/part14
#!/bin/sh
# this is part 14 of a multipart archive
# do not concatenate these parts, unpack them in order with /bin/sh
# file calc.patch continued
#
CurArch=14
if test ! -r s2_seq_.tmp
then echo "Please unpack part 1 first!"
exit 1; fi
( read Scheck
if test "$Scheck" != $CurArch
then echo "Please unpack part $Scheck next!"
exit 1;
else exit 0; fi
) < s2_seq_.tmp || exit 1
sed 's/^X//' << 'SHAR_EOF' >> calc.patch
X+
X+ @node Answers to Exercises, , Programming Tutorial, Tutorial
X+ @section Answers to Exercises
X+
X+ This section includes answers to all the exercises in the Calc tutorial.
X+
X+ @menu
X+ * RPN Answer 1:: 1 RET 2 RET 3 RET 4 + * -
X+ * RPN Answer 2:: 2*4 + 7*9.5 + 5/4
X+ * RPN Answer 3:: Operating on levels 2 and 3
X+ * RPN Answer 4:: Joe's complex problems
X+ * Algebraic Answer 1:: Simulating Q command
X+ * Algebraic Answer 2:: Joe's algebraic woes
X+ * Algebraic Answer 3:: 1 / 0
X+ * Modes Answer 1:: Hexadecimal floating point
X+ * Modes Answer 2:: Joe's rounding bug
X+ * Modes Answer 3:: Why floating point?
X+ * Arithmetic Answer 1:: Why the \ command?
X+ * Arithmetic Answer 2:: Tripping up the B command
X+ * Vector Answer 1:: Normalizing a vector
X+ * Vector Answer 2:: Average position
X+ * Matrix Answer 1:: Row and column sums
X+ * Matrix Answer 2:: Symbolic system of equations
X+ * Matrix Answer 3:: Over-determined system
X+ * List Answer 1:: Powers of two
X+ * List Answer 2:: Least-squares fit with matrices
X+ * List Answer 3:: Geometric mean
X+ * List Answer 4:: Divisor function
X+ * List Answer 5:: Duplicate factors
X+ * List Answer 6:: Triangular list
X+ * List Answer 7:: Another triangular list
X+ * List Answer 8:: Maximum of Bessel function
X+ * List Answer 9:: Integers the hard way
X+ * List Answer 10:: All elements equal
X+ * Types Answer 1:: Square root of pi times rational
X+ * Types Answer 2:: Abbey Road
X+ * Types Answer 3:: Erroneous donut
X+ * Types Answer 4:: Dividing intervals
X+ * Types Answer 5:: Squaring intervals
X+ * Types Answer 6:: Fermat's primality test
X+ * Types Answer 7:: pi * 10^7 seconds
X+ * Types Answer 8:: Abbey Road on CD
X+ * Types Answer 9:: Not quite pi * 10^7 seconds
X+ * Types Answer 10:: Supercomputers and c
X+ * Types Answer 11:: Sam the Slug
X+ * Algebra Answer 1:: Squares and square roots
X+ * Algebra Answer 2:: Solving a quartic equation
X+ * Algebra Answer 3:: Integral of x sin(pi x)
X+ * Algebra Answer 4:: Simpson's rule
X+ * Programming Answer 1:: Negate third stack element
X+ * Programming Answer 2:: Compute sin(x) / x
X+ * Programming Answer 3:: Average value of a list
X+ * Programming Answer 4:: Continued fraction phi
X+ * Programming Answer 5:: Matrix Fibonacci numbers
X+ * Programming Answer 6:: Harmonic number greater than 4
X+ * Programming Answer 7:: Newton's method
X+ * Programming Answer 8:: Digamma function
X+ * Programming Answer 9:: Unpacking a polynomial
X+ * Programming Answer 10:: Recursive Stirling numbers
X+ @end menu
X+
X+ @node RPN Answer 1, RPN Answer 2, Answers to Exercises, Answers to Exercises
X+ @subsection RPN Tutorial Exercise 1
X+
X+ @kbd{1 @key{RET} 2 @key{RET} 3 @key{RET} 4 + * -}
X+
X+ The result is @samp{1 - (2 * (3 + 4)) = -13}.
X+
X+ @node RPN Answer 2, RPN Answer 3, RPN Answer 1, Answers to Exercises
X+ @subsection RPN Tutorial Exercise 2
X+
X+ @samp{2*4 + 7*9.5 + 5/4 = 75.75}
X+
X+ After computing the intermediate term @samp{2*4 = 8}, you can leave
X+ that result on the stack while you compute the second term. With
X+ both of these results waiting on the stack you can then compute the
X+ final term, then press @kbd{+ +} to add everything up.
X+
X+ @group
X+ @example
X+ 2: 2 1: 8 3: 8 2: 8
X+ 1: 4 . 2: 7 1: 66.5
X+ . 1: 9.5 .
X+ .
X+
X+ 2 RET 4 * 7 RET 9.5 *
X+ @end example
X+ @end group
X+
X+ @group
X+ @example
X+ 4: 8 3: 8 2: 8 1: 75.75
X+ 3: 66.5 2: 66.5 1: 67.75 .
X+ 2: 5 1: 1.25 .
X+ 1: 4 .
X+ .
X+
X+ 5 RET 4 / + +
X+ @end example
X+ @end group
X+
X+ Alternatively, you could add the first two terms before going on
X+ with the third term.
X+
X+ @group
X+ @example
X+ 2: 8 1: 74.5 3: 74.5 2: 74.5 1: 75.75
X+ 1: 66.5 . 2: 5 1: 1.25 .
X+ . 1: 4 .
X+ .
X+
X+ ... + 5 RET 4 / +
X+ @end example
X+ @end group
X+
X+ On an old-style RPN calculator this second method would have the
X+ advantage of using only three stack levels. But since Calc's stack
X+ can grow arbitrarily large this isn't really an issue. Which method
X+ you choose is purely a matter of taste.
X+
X+ @node RPN Answer 3, RPN Answer 4, RPN Answer 2, Answers to Exercises
X+ @subsection RPN Tutorial Exercise 3
X+
X+ The @key{TAB} key provides a way to operate on the number in level 2.
X+
X+ @group
X+ @example
X+ 3: 10 3: 10 4: 10 3: 10 3: 10
X+ 2: 20 2: 30 3: 30 2: 30 2: 21
X+ 1: 30 1: 20 2: 20 1: 21 1: 30
X+ . . 1: 1 . .
X+ .
X+
X+ TAB 1 + TAB
X+ @end example
X+ @end group
X+
X+ Similarly, @key{M-TAB} gives you access to the number in level 3.
X+
X+ @group
X+ @example
X+ 3: 10 3: 21 3: 21 3: 30 3: 11
X+ 2: 21 2: 30 2: 30 2: 11 2: 21
X+ 1: 30 1: 10 1: 11 1: 21 1: 30
X+ . . . . .
X+
X+ M-TAB 1 + M-TAB M-TAB
X+ @end example
X+ @end group
X+
X+ @node RPN Answer 4, Algebraic Answer 1, RPN Answer 3, Answers to Exercises
X+ @subsection RPN Tutorial Exercise 4
X+
X+ Either @kbd{( 2 , 3 )} or @kbd{( 2 @key{SPC} 3 )} would have worked,
X+ but using both the comma and the space at once yields:
X+
X+ @group
X+ @example
X+ 1: ( ... 2: ( ... 1: (2, ... 2: (2, ... 2: (2, ...
X+ . 1: 2 . 1: (2, ... 1: (2, 3)
X+ . . .
X+
X+ ( 2 , SPC 3 )
X+ @end example
X+ @end group
X+
X+ Joe probably tried to type @kbd{@key{TAB} @key{DEL}} to swap the
X+ extra incomplete object to the top of the stack and delete it.
X+ But a feature of Calc is that @key{DEL} on an incomplete object
X+ deletes just one component out of that object, so he had to press
X+ @key{DEL} twice to finish the job.
X+
X+ @group
X+ @example
X+ 2: (2, ... 2: (2, 3) 2: (2, 3) 1: (2, 3)
X+ 1: (2, 3) 1: (2, ... 1: ( ... .
X+ . . .
X+
X+ TAB DEL DEL
X+ @end example
X+ @end group
X+
X+ @node Algebraic Answer 1, Algebraic Answer 2, RPN Answer 4, Answers to Exercises
X+ @subsection Algebraic Entry Tutorial Exercise 1
X+
X+ Type @kbd{' sqrt($) @key{RET}}.
X+
X+ If the @kbd{Q} key is broken, you could use @kbd{' $^0.5 @key{RET}}.
X+ Or, RPN style, @kbd{0.5 ^}.
X+
X+ @node Algebraic Answer 2, Algebraic Answer 3, Algebraic Answer 1, Answers to Exercises
X+ @subsection Algebraic Entry Tutorial Exercise 2
X+
X+ In the formula @samp{2 x (1+y)}, @samp{x} was interpreted as a function
X+ name with @samp{1+y} as its argument. Assigning a value to a variable
X+ has no relation to a function by the same name. Joe needed to use an
X+ explicit @samp{*} symbol here: @samp{2 x*(1+y)}.
X+
X+ @node Algebraic Answer 3, Modes Answer 1, Algebraic Answer 2, Answers to Exercises
X+ @subsection Algebraic Entry Tutorial Exercise 3
X+
X+ The result from @kbd{1 @key{RET} 0 /} will be the formula @samp{1 / 0}.
X+ You can think of this as ``infinity,'' but Calc doesn't really understand
X+ the calculus of infinities. When you type @kbd{0 *}, the result will
X+ be zero because Calc ``knows'' that anything times zero is zero. The
X+ proper mathematical answer is to say that the result is ``indeterminate,''
X+ but Calc has no such concept.
X+
X+ @node Modes Answer 1, Modes Answer 2, Algebraic Answer 3, Answers to Exercises
X+ @subsection Modes Tutorial Exercise 1
X+
X+ The decimal number @samp{123.456} is just another way of writing
X+ @samp{123456 * 10^-3}. Similarly, @samp{16#3A.7F} is the same as
X+ @samp{16#3A7F * 16^-2}. Entering this we get the equivalent decimal
X+ number 58.49609375 on the stack. We can then press @kbd{Q} to take the
X+ square root, 7.64827390658. Now we reverse the process to convert
X+ this number back to hexadecimal form: We want four digits after
X+ the point, i.e., we want a number which is a hexadecimal integer
X+ multiplied by @samp{16^-4}. So, divide by @samp{16^-4}, convert
X+ to an integer with @kbd{F} or @kbd{R}, then use @kbd{d r 16} to
X+ view the result in hexadecimal: @samp{16#7A5F5}. Thus our
X+ floating-point hexadecimal square root is @samp{16#7.A5F5}.
X+
X+ @node Modes Answer 2, Modes Answer 3, Modes Answer 1, Answers to Exercises
X+ @subsection Modes Tutorial Exercise 2
X+
X+ The answer he got was @samp{0.5000000000006399}.
X+
X+ The problem is not that the square operation is inexact, but that the
X+ sine of 45 that was already on the stack was accurate to only 12 places.
X+ Arbitrary-precision calculations still only give answers as good as
X+ their inputs.
X+
X+ The real problem is that there is no 12-digit number which, when
X+ squared, comes out to 0.5 exactly. The @kbd{f [} and @kbd{f ]}
X+ commands decrease or increase a number by one unit in the last
X+ place (according to the current precision). They are useful for
X+ determining facts like this.
X+
X+ @group
X+ @example
X+ 1: 0.707106781187 1: 0.500000000001
X+ . .
X+
X+ 45 S 2 ^
X+ @end example
X+ @end group
X+
X+ @group
X+ @example
X+ 1: 0.707106781187 1: 0.707106781186 1: 0.499999999999
X+ . . .
X+
X+ U f [ 2 ^
X+ @end example
X+ @end group
X+
X+ A high-precision calculation must be carried out in high precision
X+ all the way. The only number in the original problem which was known
X+ exactly was the quantity 45 degrees, so the precision must be raised
X+ before anything is done after the number 45 has been entered in order
X+ for the higher precision to be meaningful.
X+
X+ @node Modes Answer 3, Arithmetic Answer 1, Modes Answer 2, Answers to Exercises
X+ @subsection Modes Tutorial Exercise 3
X+
X+ Many calculations involve real-world quantities, like the width and
X+ height of a piece of wood or the volume of a jar. Such quantities
X+ can't be measured exactly anyway, and if the data that is input to
X+ a calculation is inexact, doing exact arithmetic on it is a waste
X+ of time.
X+
X+ Fractions become unwieldy after too many calculations have been
X+ done with them. For example, the sum of the reciprocals of the
X+ integers from 1 to 10 is 7381:2520. The sum from 1 to 30 is
X+ 9304682830147:2329089562800. After a point it will take a long
X+ time to add even one more term to this sum, but a floating-point
X+ calculation of the sum will not have this problem.
X+
X+ Also, rational numbers cannot express the results of all calculations.
X+ There is no fractional form for the square root of two, so if you type
X+ @kbd{2 Q}, Calc has no choice but to give you a floating-point answer.
X+
X+ @node Arithmetic Answer 1, Arithmetic Answer 2, Modes Answer 3, Answers to Exercises
X+ @subsection Arithmetic Tutorial Exercise 1
X+
X+ Dividing two integers that are larger than the current precision may
X+ give a floating-point result that is inaccurate even when rounded
X+ down to an integer. Consider @samp{123456789 / 2} when the current
X+ precision is 6 digits. The true answer is @samp{61728394.5}, but
X+ with a precision of 6 this will be rounded to @samp{61728300.}.
X+ The result, when converted to an integer, will be off by 94.
X+
X+ Here are two solutions: Raise the precision enough that the
X+ floating-point round-off error is strictly to the right of the
X+ decimal point. Or, convert to fraction mode so that @samp{123456789 / 2}
X+ produces the exact fraction @samp{123456789:2}, which can be rounded
X+ down by the @kbd{F} command without ever switching to floating-point
X+ format.
X+
X+ @node Arithmetic Answer 2, Vector Answer 1, Arithmetic Answer 1, Answers to Exercises
X+ @subsection Arithmetic Tutorial Exercise 2
X+
X+ @kbd{27 @key{RET} 9 B} could give the exact result @samp{3:2}, but it
X+ does a floating-point calculation instead and produces @samp{1.5}.
X+
X+ Calc will find an exact result for a logarithm if the result is an integer
X+ or the reciprocal of an integer. But there is no efficient way to search
X+ the space of all possible rational numbers for an exact answer, so Calc
X+ doesn't try.
X+
X+ @node Vector Answer 1, Vector Answer 2, Arithmetic Answer 2, Answers to Exercises
X+ @subsection Vector Tutorial Exercise 1
X+
X+ Duplicate the vector, compute its length, then divide the vector
X+ by its length: @kbd{@key{RET} A /}.
X+
X+ @node Vector Answer 2, Matrix Answer 1, Vector Answer 1, Answers to Exercises
X+ @subsection Vector Tutorial Exercise 2
X+
X+ The average position is equal to the sum of the products of the
X+ positions times their corresponding probabilities. This is the
X+ definition of the dot product operation. So all you need to do
X+ is to put the two vectors on the stack and press @kbd{*}.
X+
X+ @node Matrix Answer 1, Matrix Answer 2, Vector Answer 2, Answers to Exercises
X+ @subsection Matrix Tutorial Exercise 1
X+
X+ The trick is to multiply by a vector of ones. Use @kbd{r 4 [1 1 1] *} to
X+ get the row sum. Similarly, use @kbd{[1 1] r 4 *} to get the column sum.
X+
X+ @node Matrix Answer 2, Matrix Answer 3, Matrix Answer 1, Answers to Exercises
X+ @subsection Matrix Tutorial Exercise 2
X+
X+ @group
X+ @example
X+ x + a y = 6
X+ x + b y = 10
X+ @end example
X+ @end group
X+
X+ Just enter the righthand side vector, then divide by the lefthand side
X+ matrix as usual.
X+
X+ @group
X+ @example
X+ 1: [6, 10] 2: [6, 10] 1: [6 - 4 a / (b - a), 4 / (b - a) ]
X+ . 1: [ [ 1, a ], .
X+ [ 1, b ] ]
X+ .
X+
X+ ' [6 10] RET ' [1 a; 1 b] RET /
X+ @end example
X+ @end group
X+
X+ This can be made more readable using @kbd{d B} to enable ``big'' display
X+ mode:
X+
X+ @group
X+ @example
X+ 4 a 4
X+ 1: [6 - -----, -----]
X+ b - a b - a
X+ @end example
X+ @end group
X+
X+ Type @kbd{d N} to return to ``normal'' display mode afterwards.
X+
X+ @node Matrix Answer 3, List Answer 1, Matrix Answer 2, Answers to Exercises
X+ @subsection Matrix Tutorial Exercise 3
X+
X+ To solve @samp{trn(A) * A * X = trn(A) * B}, first we compute
X+ @samp{A' = trn(A) * A} and @samp{B' = trn(A) * B}; now, we have a
X+ system @samp{A' * X = B'} which we can solve using Calc's @samp{/}
X+ command.
X+
X+ @group
X+ @example
X+ a + 2b + 3c = 6
X+ 4a + 5b + 6c = 2
X+ 7a + 6b = 3
X+ 2a + 4b + 6c = 11
X+ @end example
X+ @end group
X+
X+ The first step is to enter the coefficient matrix. We'll store it in
X+ quick variable number 7 for later reference. Next, we compute the
X+ @samp{B'} vector.
X+
X+ @group
X+ @example
X+ 1: [ [ 1, 2, 3 ], 2: [ [ 1, 4, 7, 2 ], 1: [57, 84, 96]
X+ [ 4, 5, 6 ], [ 2, 5, 6, 4 ], .
X+ [ 7, 6, 0 ], [ 3, 6, 0, 6 ] ]
X+ [ 2, 4, 6 ] ] 1: [6, 2, 3, 11]
X+ . .
X+
X+ ' [1 2 3; 4 5 6; 7 6 0; 2 4 6] RET s 7 v t [6 2 3 11] *
X+ @end example
X+ @end group
X+
X+ Now we compute the matrix @samp{A'} and divide.
X+
X+ @group
X+ @example
X+ 2: [57, 84, 96] 1: [-11.64, 14.08, -3.64]
X+ 1: [ [ 70, 72, 39 ], .
X+ [ 72, 81, 60 ],
X+ [ 39, 60, 81 ] ]
X+ .
X+
X+ r 7 v t r 7 * /
X+ @end example
X+ @end group
X+
X+ (The actual computed answer will be slightly inexact due to
X+ round-off error.)
X+
X+ Notice that the answers are similar to those for the 3x3 system
X+ solved in the text. That's because the fourth equation that was
X+ added to the system is almost identical to the first one multiplied
X+ by two. (If it were identical, we would have gotten the exact same
X+ answer since the 4x3 system would be equivalent to the original 3x3
X+ system.)
X+
X+ Since the first and fourth equations aren't quite equivalent, they
X+ can't both be satisfied at once. Let's plug our answers back into
X+ the original system of equations to see how well they match.
X+
X+ @group
X+ @example
X+ 2: [-11.64, 14.08, -3.64] 1: [5.6, 2., 3., 11.2]
X+ 1: [ [ 1, 2, 3 ], .
X+ [ 4, 5, 6 ],
X+ [ 7, 6, 0 ],
X+ [ 2, 4, 6 ] ]
X+ .
X+
X+ r 7 TAB *
X+ @end example
X+ @end group
X+
X+ This is reasonably close to our original @samp{B} vector,
X+ @samp{[6, 2, 3, 11]}.
X+
X+ @node List Answer 1, List Answer 2, Matrix Answer 3, Answers to Exercises
X+ @subsection List Tutorial Exercise 1
X+
X+ We can use @kbd{v x} to build a vector of integers. This needs to be
X+ adjusted to get the range of integers we desire. Mapping @samp{-}
X+ across the vector will accomplish this, although it turns out the
X+ plain @samp{-} key will work just as well.
X+
X+ @group
X+ @example
X+ 2: 2 2: 2
X+ 1: [1, 2, 3, 4, 5, 6, 7, 8, 9] 1: [-4, -3, -2, -1, 0, 1, 2, 3, 4]
X+ . .
X+
X+ 2 v x 9 RET 5 V M - or 5 -
X+ @end example
X+ @end group
X+
X+ Now we use @kbd{V M ^} to map the exponentiation operator across the
X+ vector.
X+
X+ @group
X+ @example
X+ 1: [0.0625, 0.125, 0.25, 0.5, 1, 2, 4, 8, 16]
X+ .
X+
X+ V M ^
X+ @end example
X+ @end group
X+
X+ @node List Answer 2, List Answer 3, List Answer 1, Answers to Exercises
X+ @subsection List Tutorial Exercise 2
X+
X+ Given @var{x} and @var{y} vectors in quick variables 1 and 2 as before,
X+ the first job is to form the matrix that describes the problem.
X+
X+ @example
X+ m*x + b*1 = y
X+ @end example
X+
X+ Thus we want a 19x2 matrix with our @var{x} vector as one column and
X+ ones as the other column. So, first we build the column of ones, then
X+ we combine the two columns to form our @samp{A} matrix.
X+
X+ @group
X+ @example
X+ 2: [1.34, 1.41, 1.49, ... ] 1: [ [ 1.34, 1 ],
X+ 1: [1, 1, 1, ...] [ 1.41, 1 ],
X+ . @dots{}
X+
X+ r 1 1 v b 19 RET M-2 v p v t s 3
X+ @end example
X+ @end group
X+
X+ Now we compute @samp{trn(A) * y} and @samp{trn(A) * A} and divide.
X+
X+ @group
X+ @example
X+ 1: [33.36554, 13.613] 2: [33.36554, 13.613]
X+ . 1: [ [ 98.0003, 41.63 ],
X+ [ 41.63, 19 ] ]
X+ .
X+
X+ v t r 2 * r 2 v t r 2 *
X+ @end example
X+ @end group
X+
X+ (Hey, those numbers look familiar!)
X+
X+ @group
X+ @example
X+ 1: [0.52141679, -0.425978]
X+ .
X+
X+ /
X+ @end example
X+ @end group
X+
X+ Since we were solving equations of the form @samp{m*x + b*1 = y}, these
X+ numbers should be @var{m} and @var{b}, respectively. Sure enough, they
X+ agree exactly with the result computed using @kbd{V M} and @kbd{V R}!
X+
X+ The moral of this story: @kbd{V M} and @kbd{V R} will probably solve
X+ your problem, but there is often an easier way using the higher-level
X+ arithmetic functions!
X+
X+ @node List Answer 3, List Answer 4, List Answer 2, Answers to Exercises
X+ @subsection List Tutorial Exercise 3
X+
X+ Move to one end of the list and press @kbd{C-@@} (or @kbd{C-SPC} or
X+ whatever) to set the mark, then move to the other end of the list
X+ and type @kbd{C-u M-x calc-grab-region}.
X+
X+ @group
X+ @example
X+ 1: [2.3, 6, 22, 15.1, 7, 15, 14, 7.5, 2.5]
X+ .
X+ @end example
X+ @end group
X+
X+ To make things interesting, let's assume we don't know at a glance
X+ how many numbers are in this list. Then we could type:
X+
X+ @group
X+ @example
X+ 2: [2.3, 6, 22, ... ] 2: [2.3, 6, 22, ... ]
X+ 1: [2.3, 6, 22, ... ] 1: 126356422.5
X+ . .
X+
X+ RET V R *
X+ @end example
X+ @end group
X+
X+ @group
X+ @example
X+ 2: 126356422.5 2: 126356422.5 1: 7.94652913734
X+ 1: [2.3, 6, 22, ... ] 1: 9 .
X+ . .
X+
X+ TAB v l I ^
X+ @end example
X+ @end group
X+
X+ (The @kbd{I ^} command computes the @var{n}th root of a number.
X+ You could also type @samp{& ^} to take the reciprocal of 9 and
X+ then raise the number to that power.)
X+
X+ @node List Answer 4, List Answer 5, List Answer 3, Answers to Exercises
X+ @subsection List Tutorial Exercise 4
X+
X+ A number @var{j} is a divisor of @var{n} if @samp{n % j = 0}. The first
X+ step is to get a vector that identifies the divisors.
X+
X+ @group
X+ @example
X+ 2: 30 2: [0, 0, 0, 2, ...] 1: [1, 1, 1, 0, ...]
X+ 1: [1, 2, 3, 4, ...] 1: 0 .
X+ . .
X+
X+ 30 RET v x 30 RET s 1 V M % 0 V M a = s 2
X+ @end example
X+ @end group
X+
X+ The zeroth divisor function is just the total number of divisors.
X+ The first divisor function is the sum of the divisors.
X+
X+ @group
X+ @example
X+ 1: 8 3: 8 2: 8 2: 8
X+ 2: [1, 2, 3, 4, ...] 1: [1, 2, 3, 0, ...] 1: 72
X+ 1: [1, 1, 1, 0, ...] . .
X+ .
X+
X+ V R + r 1 r 2 V M * V R +
X+ @end example
X+ @end group
X+
X+ Once again, the last two steps just compute a dot product for which
X+ a simple @kbd{*} would have worked equally well.
X+
X+ @node List Answer 5, List Answer 6, List Answer 4, Answers to Exercises
X+ @subsection List Tutorial Exercise 5
X+
X+ The obvious first step is to obtain the list of factors with @kbd{k f}.
X+ This list will always be in sorted order, so if there are duplicates
X+ they will be right next to each other. A suitable method is to compare
X+ the list with a copy of itself shifted over by one.
X+
X+ @group
X+ @example
X+ 1: [3, 7, 7, 7, 19] 2: [3, 7, 7, 7, 19] 2: [3, 7, 7, 7, 19, 0]
X+ . 1: [3, 7, 7, 7, 19, 0] 1: [0, 3, 7, 7, 7, 19]
X+ . .
X+
X+ 19551 k f RET 0 | TAB 0 TAB |
X+ @end example
X+ @end group
X+
X+ @group
X+ @example
X+ 1: [0, 0, 1, 1, 0, 0] 1: 2 1: 0
X+ . . .
X+
X+ V M a = V R + 0 a =
X+ @end example
X+ @end group
X+
X+ Note that we have to arrange for both vectors to have the same length
X+ so that the mapping operation works; no prime factor will ever be
X+ zero, so adding zeros on the left and right is safe. From then on
X+ the job is pretty straightforward.
X+
X+ Incidentally, Calc provides the @dfn{Moebius mu} function which is
X+ zero if and only if its argument is square-free. It would be a much
X+ more convenient way to do the above test in practice.
X+
X+ @node List Answer 6, List Answer 7, List Answer 5, Answers to Exercises
X+ @subsection List Tutorial Exercise 6
X+
X+ First use @kbd{v x 6 RET} to get a list of integers, then @kbd{V M v x}
X+ to get a list of lists of integers!
X+
X+ @node List Answer 7, List Answer 8, List Answer 6, Answers to Exercises
X+ @subsection List Tutorial Exercise 7
X+
X+ Here's one solution. First, compute the triangular list from the previous
X+ exercise and type @kbd{1 -} to subtract one from all the elements.
X+
X+ @group
X+ @example
X+ 1: [ [0],
X+ [0, 1],
X+ [0, 1, 2],
X+ @dots{}
X+
X+ 1 -
X+ @end example
X+ @end group
X+
X+ The numbers down the lefthand edge of the list we desire are called
X+ the ``triangular numbers'' (now you know why!). The @var{n}th
X+ triangular number is the sum of the integers from 1 to @var{n}, and
X+ can be computed directly by the formula @samp{n * (n+1) / 2}.
X+
X+ @group
X+ @example
X+ 2: [ [0], [0, 1], ... ] 2: [ [0], [0, 1], ... ]
X+ 1: [0, 1, 2, 3, 4, 5] 1: [0, 1, 3, 6, 10, 15]
X+ . .
X+
X+ v x 6 RET 1 - V M ' n*(n+1)/2 RET RET
X+ @end example
X+ @end group
X+
X+ Adding this list to the above list of lists produces the desired
X+ result:
X+
X+ @group
X+ @example
X+ 1: [ [0],
X+ [1, 2],
X+ [3, 4, 5],
X+ [6, 7, 8, 9],
X+ [10, 11, 12, 13, 14],
X+ [15, 16, 17, 18, 19, 20] ]
X+ .
X+
X+ V M +
X+ @end example
X+ @end group
X+
X+ If we did not know the formula for triangular numbers, we could have
X+ computed them using an @kbd{H V R +} command. We could also have
X+ gotten them the hard way by mapping a reduction across the original
X+ triangular list.
X+
X+ @group
X+ @example
X+ 2: [ [0], [0, 1], ... ] 2: [ [0], [0, 1], ... ]
X+ 1: [ [0], [0, 1], ... ] 1: [0, 1, 3, 6, 10, 15]
X+ . .
X+
X+ RET V M ' reduce(add,x) RET C-a C-k (x) RET
X+ @end example
X+ @end group
X+
X+ The @kbd{C-a C-k (x)} represents changing the default argument list,
X+ @samp{(add x)}, to our desired argument list, @samp{(x)}. Calc did not
X+ understand that the @code{add} was a function name and not a variable
X+ in the @code{reduce} expression.
X+
X+ Incidentally, the algebraic notation for mappings and reductions was
X+ not covered in the Tutorial; you could discover it for yourself like
X+ this:
X+
X+ @group
X+ @example
X+ 1: a 1: reduce(mul, a)
X+ . .
X+
X+ ' a RET V R *
X+ @end example
X+ @end group
X+
X+ As usual, since @samp{a} is not an acceptable type of argument for
X+ a @kbd{V R} command, the operation is left in symbolic form on the
X+ stack.
X+
X+ @node List Answer 8, List Answer 9, List Answer 7, Answers to Exercises
X+ @subsection List Tutorial Exercise 8
X+
X+ The first step is to build a list of values of @samp{x}.
X+
X+ @group
X+ @example
X+ 1: [1, 2, 3, ..., 21] 1: [0, 1, 2, ..., 20] 1: [0, 0.25, 0.5, ..., 5]
X+ . . .
X+
X+ v x 21 RET 1 - 4 / s 1
X+ @end example
X+ @end group
X+
X+ Next, we compute the Bessel function values.
X+
X+ @group
X+ @example
X+ 1: [0., 0.124, 0.242, ..., -0.328]
X+ .
X+
X+ V M ' besJ(1,x) RET RET
X+ @end example
X+ @end group
X+
X+ A way to isolate the maximum value is to compute the maximum using
X+ @kbd{V R X}, then compare all the Bessel values with that maximum.
X+
X+ @group
X+ @example
X+ 2: [0., 0.124, 0.242, ... ] 1: [0, 0, 0, ... ] 2: [0, 0, 0, ... ]
X+ 1: 0.5801562 . 1: 1
X+ . .
X+
X+ V R X V M a = RET V R + DEL
X+ @end example
X+ @end group
X+
X+ It's a good idea to verify, as in the last step above, that only
X+ one value is equal to the maximum. (After all, a plot of @samp{sin(x)}
X+ might have many points all equal to the maximum value, 1.)
X+
X+ The vector we have now has a single 1 in the position that indicates
X+ the maximum value of @samp{x}. Now it is a simple matter to convert
X+ this back into the corresponding value itself.
X+
X+ @group
X+ @example
X+ 2: [0, 0, 0, ... ] 1: [0, 0., 0., ... ] 1: 1.75
X+ 1: [0, 0.25, 0.5, ... ] . .
X+ .
X+
X+ r 1 V M * V R +
X+ @end example
X+ @end group
X+
X+ If @samp{a =} had produced more than one @samp{1} value, this method
X+ would have given the sum of all maximum @samp{x} values; not very
X+ useful! In this case we could have used @kbd{v m} (@code{calc-mask-vector})
X+ instead. This command deletes all elements of a ``data'' vector that
X+ correspond to zeros in a ``mask'' vector, leaving us with, in this
X+ example, a vector of maximum @samp{x} values.
X+
X+ @node List Answer 9, List Answer 10, List Answer 8, Answers to Exercises
X+ @subsection List Tutorial Exercise 9
X+
X+ Step one is to convert our integer into vector notation.
X+
X+ @group
X+ @example
X+ 1: 25129925999 3: 25129925999
X+ . 2: 10
X+ 1: [11, 10, 9, ..., 1, 0]
X+ .
X+
X+ 25129925999 RET 10 RET 12 RET v x 12 RET -
X+ @end example
X+ @end group
X+
X+ @group
X+ @example
X+ 1: 25129925999 1: [0, 2, 25, 251, 2512, ... ]
X+ 2: [100000000000, ... ] .
X+ .
X+
X+ V M ^ s 1 V M \
X+ @end example
X+ @end group
X+
X+ (Recall, the @kbd{\} command computes an integer quotient.)
X+
X+ @group
X+ @example
X+ 1: [0, 2, 5, 1, 2, 9, 9, 2, 5, 9, 9, 9]
X+ .
X+
X+ 10 V M % s 2
X+ @end example
X+ @end group
X+
X+ Next we must increment this number. This involves adding one to
X+ the last digit, plus handling carries. There is a carry out of a
X+ digit to the left if that digit is a nine and all the digits to
X+ the right of it are nines.
X+
X+ @group
X+ @example
X+ 1: [0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1] 1: [1, 1, 1, 0, 0, 1, ... ]
X+ . .
X+
X+ 9 V M a = v v
X+ @end example
X+ @end group
X+
X+ @group
X+ @example
X+ 1: [1, 1, 1, 0, 0, 0, ... ] 1: [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1]
X+ . .
X+
X+ H V R * v v 1 |
X+ @end example
X+ @end group
X+
X+ Accumulating @kbd{*} across a vector of ones and zeros will preserve
X+ only the leftmost run of ones. These are the carries into all digits
X+ except the rightmost digit. Concatenating a one on the right takes
X+ care of aligning the carries properly, and also adding one to the
X+ rightmost digit.
X+
X+ @group
X+ @example
X+ 2: [0, 0, 0, 0, ... ] 1: [0, 0, 2, 5, 1, 2, 9, 9, 2, 6, 0, 0, 0]
X+ 1: [0, 0, 2, 5, ... ] .
X+ .
X+
X+ 0 r 2 | V M + 10 V M %
X+ @end example
X+ @end group
X+
X+ Here we have concatenated 0 to the @emph{left} of the original number;
X+ this takes care of shifting the carries by one with respect to the
X+ digits that generated them.
X+
X+ Finally, we must convert this list back into an integer.
X+
X+ @group
X+ @example
X+ 3: [0, 0, 2, 5, ... ] 2: [0, 0, 2, 5, ... ]
X+ 2: 1000000000000 1: [1000000000000, 100000000000, ... ]
X+ 1: [100000000000, ... ] .
X+ .
X+
X+ 10 RET 12 ^ r 1 |
X+ @end example
X+ @end group
X+
X+ @group
X+ @example
X+ 1: [0, 0, 20000000000, 5000000000, ... ] 1: 25129926000
X+ . .
X+
X+ V M * V R +
X+ @end example
X+ @end group
X+
X+ @node List Answer 10, Types Answer 1, List Answer 9, Answers to Exercises
X+ @subsection List Tutorial Exercise 10
X+
X+ For the list @samp{[a, b, c, d]}, the result is @samp{((a = b) = c) = d},
X+ which will compare @samp{a} and @samp{b} to produce a 1 or 0, which is
X+ then compared with @samp{c} to produce another 1 or 0, which is then
X+ compared with @samp{d}. This is not at all what Joe wanted.
X+
X+ Here's a more correct method:
X+
X+ @group
X+ @example
X+ 1: [7, 7, 7, 8, 7] 2: [7, 7, 7, 8, 7]
X+ . 1: 7
X+ .
X+
X+ ' [7,7,7,8,7] RET RET v r 1 RET
X+ @end example
X+ @end group
X+
X+ @group
X+ @example
X+ 1: [1, 1, 1, 0, 1] 1: 0
X+ . .
X+
X+ V M a = V R *
X+ @end example
X+ @end group
X+
X+ @node Types Answer 1, Types Answer 2, List Answer 10, Answers to Exercises
X+ @subsection Types Tutorial Exercise 1
X+
X+ If the number is the square root of @samp{pi} times a rational number,
X+ then its square, divided by @samp{pi}, should be a rational number.
X+
X+ @group
X+ @example
X+ 1: 1.26508260337 1: 0.509433962268 1: 2486645810:4881193627
X+ . . .
X+
X+ 2 ^ P / c F
X+ @end example
X+ @end group
X+
X+ Technically speaking this is a rational number, but not one that is
X+ likely to have arisen in the original problem. More likely, it just
X+ happens to be the fraction which most closely represents some
X+ irrational number to within 12 digits.
X+
X+ But perhaps our result was not quite exact. Let's reduce the
X+ precision slightly and try again:
X+
X+ @group
X+ @example
X+ 1: 0.509433962268 1: 27:53
X+ . .
X+
X+ U p 10 RET c F
X+ @end example
X+ @end group
X+
X+ Aha! It's unlikely that an irrational number would equal a fraction
X+ this simple to within ten digits, so our original number was probably
X+ @samp{sqrt(27 pi / 53)}.
X+
X+ Notice that we didn't need to re-round the number when we reduced the
X+ precision. Remember, arithmetic operations always round their inputs
X+ to the current precision before starting.
X+
X+ @node Types Answer 2, Types Answer 3, Types Answer 1, Answers to Exercises
X+ @subsection Types Tutorial Exercise 2
X+
X+ @group
X+ @example
X+ 2: 0@@ 47' 26" 1: 0@@ 2' 47.411765"
X+ 1: 17 .
X+ .
X+
X+ 0@@ 47' 26" RET 17 /
X+ @end example
X+ @end group
X+
X+ The average song length is two minutes and 47.4 seconds.
X+
X+ @group
X+ @example
X+ 2: 0@@ 2' 47.411765" 1: 0@@ 3' 7.411765" 1: 0@@ 53' 6.000005"
X+ 1: 0@@ 0' 20" . .
X+ .
X+
X+ 20" + 17 *
X+ @end example
X+ @end group
X+
X+ The album would be 53 minutes and 6 seconds long.
X+
X+ @node Types Answer 3, Types Answer 4, Types Answer 2, Answers to Exercises
X+ @subsection Types Tutorial Exercise 3
X+
X+ The relative errors must be converted to absolute errors so that
X+ @samp{+/-} notation may be used.
X+
X+ @group
X+ @example
X+ 1: 1. 2: 1.
X+ . 1: 0.2
X+ .
X+
X+ 20 RET .05 * 4 RET .05 *
X+ @end example
X+ @end group
X+
X+ Now we simply chug through the formula.
X+
X+ @group
X+ @example
X+ 1: 39.4784176044 1: 789.56 +/- 39.478 1: 12633. +/- 1412.4
X+ . . .
X+
X+ 4 P 2 ^ * 20 p 1 * 4 p .2 RET 2 ^ *
X+ @end example
X+ @end group
X+
X+ It turns out the @kbd{v u} command will unpack an error form as
X+ well as a vector. This saves us some retyping of numbers.
X+
X+ @group
X+ @example
X+ 3: 12633. +/- 1412.4 2: 12633 +/- 1412.4
X+ 2: 12633. 1: 0.1118
X+ 1: 1412.4 .
X+ .
X+
X+ RET v u TAB /
X+ @end example
X+ @end group
X+
X+ Thus the volume is 12633 cubic centimeters, within about 11 percent.
X+
X+ @node Types Answer 4, Types Answer 5, Types Answer 3, Answers to Exercises
X+ @subsection Types Tutorial Exercise 4
X+
X+ It is an error to divide by an interval that contains zero, or that has
X+ zero as an open limit. Note that the interval @samp{(0, 10]} doesn't
X+ actually contain zero as a possible value, but dividing by it is still
X+ an error. If Calc had a symbol for infinity, it could write
X+ @samp{[0.1, infinity)} for the reciprocal of @samp{(0, 10]}. But it
X+ doesn't, so there's no way to express an interval that is unbounded
X+ on one end.
X+
X+ @node Types Answer 5, Types Answer 6, Types Answer 4, Answers to Exercises
X+ @subsection Types Tutorial Exercise 5
X+
X+ @group
X+ @example
X+ 1: [-3 .. 3] 2: [-3 .. 3] 2: [0 .. 9]
X+ . 1: [0 .. 9] 1: [-9 .. 9]
X+ . .
X+
X+ [ 3 n .. 3 ] RET 2 ^ TAB RET *
X+ @end example
X+ @end group
X+
X+ In the first case the result says, ``if a number is between -3 and 3,
X+ its square is between 0 and 9.'' The second case says, ``the product
X+ of two numbers each between -3 and 3 is between -9 and 9.''
X+
X+ An interval form is not a number; it is a symbol that can stand for
X+ many different numbers. Two identical-looking error forms can stand
X+ for different numbers.
X+
X+ The same issue arises when you try to square an error form.
X+
X+ @node Types Answer 6, Types Answer 7, Types Answer 5, Answers to Exercises
X+ @subsection Types Tutorial Exercise 6
X+
X+ Testing the first number, we might arbitrarily choose 17 for @samp{x}.
X+
X+ @group
X+ @example
X+ 1: 17 mod 811749613 2: 17 mod 811749613 1: 533694123 mod 811749613
X+ . 811749612 .
X+ .
X+
X+ 17 M 811749613 RET 811749612 ^
X+ @end example
X+ @end group
X+
X+ Since 533694123 is (considerably) different from 1, the number must
X+ not be prime.
X+
X+ It's awkward to type the number in twice as we did above. There are
X+ various ways to avoid this, and algebraic entry is one. In fact, using
X+ a vector mapping operation we can perform several tests at once. Let's
X+ use this method to test the second number.
X+
X+ @group
X+ @example
X+ 2: [17, 42, 100000] 1: [1 mod 15485863, 1 mod ... ]
X+ 1: 15485863 .
X+ .
X+
X+ [17 42 100000] 15485863 RET V M ' (a mod n)^(n-1) RET RET
X+ @end example
X+ @end group
X+
X+ We use @samp{a} instead of @samp{x} here so that the argument list
X+ will come out right without editing. Calc sorts the variable names
X+ into alphabetical order to form the default argument list, @samp{(a n)}
X+ in this case. The result is three ones (modulo @samp{n}), so it's
X+ very probable that 15485863 is prime. (In fact, this number is the
X+ millionth prime.)
X+
X+ Note that the formulas @samp{a^(n-1) mod n} or @samp{a^(n-1) % n} would
X+ have been vastly less efficient, since they would have calculated
X+ the power using full integer arithmetic.
X+
X+ Calc has a @kbd{k p} command that does primality testing. For small
X+ numbers it does an exact test; for large numbers it uses a variant
X+ of the Fermat test we used here. You can use @kbd{k p} repeatedly
X+ to prove that a large integer is prime with any desired probability.
X+
X+ @node Types Answer 7, Types Answer 8, Types Answer 6, Answers to Exercises
X+ @subsection Types Tutorial Exercise 7
X+
X+ It's not easy to insert a calculated number into an HMS form, so
X+ instead we'll express the number in hours.
X+
X+ @group
X+ @example
X+ 1: 31415926.5359 1: 8726.64625997
X+ . .
X+
X+ P 1e7 * 3600 /
X+ @end example
X+ @end group
X+
X+ It's important to use degrees mode when combining real numbers with
X+ HMS forms. If you were in radians mode, Calc would convert 8726
X+ ``radians'' to degrees.
X+
X+ @group
X+ @example
X+ 2: 8726.64625997 1: 6@@ 6' 2.5359" mod 24@@ 0' 0"
X+ 1: 15@@ 27' 16" mod 24@@ 0' 0" .
X+ .
X+
X+ x time RET m d +
X+ @end example
X+ @end group
X+
X+ It will be just after six in the morning.
X+
X+ @node Types Answer 8, Types Answer 9, Types Answer 7, Answers to Exercises
X+ @subsection Types Tutorial Exercise 8
X+
X+ As we recall, there are 17 songs of about 2 minutes and 47 seconds
X+ each.
X+
X+ @group
X+ @example
X+ 2: 0@@ 2' 47" 1: [0@@ 3' 7" .. 0@@ 3' 47"]
X+ 1: [0@@ 0' 20" .. 0@@ 1' 0"] .
X+
X+ +
X+ @end example
X+ @end group
X+
X+ @group
X+ @example
X+ 1: [0@@ 52' 59." .. 1@@ 4' 19."]
X+ .
X+
X+ 17 *
X+ @end example
X+ @end group
X+
X+ No matter how long it is, the album will fit nicely on one CD.
X+
X+ @node Types Answer 9, Types Answer 10, Types Answer 8, Answers to Exercises
X+ @subsection Types Tutorial Exercise 9
X+
X+ Type @kbd{' 1 yr RET u c s RET}. The answer is 31557600 seconds.
X+
X+ @node Types Answer 10, Types Answer 11, Types Answer 9, Answers to Exercises
X+ @subsection Types Tutorial Exercise 10
X+
X+ How long will it take for a signal to get from one end of the computer
X+ to the other?
X+
X+ @group
X+ @example
X+ 1: m / c 1: 3.3356 ns
X+ . .
X+
X+ ' 1 m / c RET u c ns RET
X+ @end example
X+ @end group
X+
X+ (Recall, @samp{c} is a ``unit'' corresponding to the speed of light.)
X+
X+ @group
X+ @example
X+ 1: 3.3356 ns 1: 0.81356 ns / ns 1: 0.81356
X+ 2: 4.1 ns . .
X+ .
X+
X+ ' 4.1 ns RET / u s
X+ @end example
X+ @end group
X+
X+ Thus a signal could take up to 81 percent of a clock cycle just to
X+ go from one place to another inside the computer, assuming the signal
X+ could actually attain the full speed of light. Pretty tight!
X+
X+ @node Types Answer 11, Algebra Answer 1, Types Answer 10, Answers to Exercises
X+ @subsection Types Tutorial Exercise 11
X+
X+ The speed limit is 55 miles per hour on most highways. We want to
X+ find the ratio of Sam's speed to the US speed limit.
X+
X+ @group
X+ @example
X+ 1: 55 mph 2: 55 mph 3: 11 hr mph / yd
X+ . 1: 5 yd / hr .
X+ .
X+
X+ ' 55 mph RET ' 5 yd/hr RET /
X+ @end example
X+ @end group
X+
X+ The @kbd{u s} command doesn't figure out how to simplify this,
X+ but @kbd{u b} (convert to base units) does. Now we take the
X+ logarithm base two to find the final answer, assuming that each
X+ successive pill doubles his speed.
X+
X+ @group
X+ @example
X+ 1: 19360. 2: 19360. 1: 14.24
X+ . 1: 2 .
X+ .
X+
X+ u b 2 B
X+ @end example
X+ @end group
X+
X+ Thus Sam can take up to 14 pills without a worry.
X+
X+ @node Algebra Answer 1, Algebra Answer 2, Types Answer 11, Answers to Exercises
X+ @subsection Algebra Tutorial Exercise 1
X+
X+ The result @samp{sqrt(x)^2} is simplified back to @samp{x} by the
X+ Calculator, but @samp{sqrt(x^2)} is not. (Consider what happens
X+ if @samp{x = -3}.) If @samp{x} is real, this formula could be
X+ simplified to @samp{abs(x)}, but for general complex arguments even
X+ that is not safe.
X+
X+ @node Algebra Answer 2, Algebra Answer 3, Algebra Answer 1, Answers to Exercises
X+ @subsection Algebra Tutorial Exercise 2
X+
X+ Using @kbd{a S x RET} on the formula @samp{17 x^2 - 6 x^4 + 3 = 0} shows
X+ that Calc isn't smart enough to solve this directly. But this is a
X+ quadratic equation in @samp{x^2}, so let's rewrite it in terms of a
X+ new variable @samp{y = x^2}. We can do this by replacing @samp{x}
X+ with @samp{sqrt(y)}.
X+
X+ @group
X+ @example
X+ 1: 17 x^2 - 6 x^4 + 3 = 0 1: 17 y - 6 y^2 + 3 = 0
X+ 2: sqrt(y) .
X+
X+ ' sqrt(y) RET l x RET
X+ @end example
X+ @end group
X+
X+ Now we can solve for the values of @samp{y} that satisfy the equation.
X+
X+ @group
X+ @example
X+ 1: y = 1.41667 + 1.58333 s1 1: y = [-0.16667, 3.]
X+ . .
X+
X+ H a S y RET [ 1 RET 1 n ] l s1 RET
X+ @end example
X+ @end group
X+
X+ We now rewrite in terms of @samp{x = sqrt(y)}.
X+
X+ @group
X+ @example
X+ 2: y 2: y
X+ 1: [-0.16667, 3.] 1: [(0., 0.408248), 1.73205]
X+ . .
X+
X+ v u V M Q
X+ @end example
X+ @end group
X+
X+ These are two solutions to the original equation. We can find the
X+ other two by noticing that @samp{x = -sqrt(y)} is another solution
X+ to @samp{y = x^2}.
X+
X+ @group
X+ @example
X+ 1: [(0., 0.408248), 1.73205, (0., -0.408248), -1.73205]
X+ .
X+
X+ RET n |
X+ @end example
X+ @end group
X+
X+ @node Algebra Answer 3, Algebra Answer 4, Algebra Answer 2, Answers to Exercises
X+ @subsection Algebra Tutorial Exercise 3
X+
X+ @group
X+ @example
X+ 1: x sin(pi x) 1: (sin(x pi) - x pi cos(x pi)) / pi^2
X+ . .
X+
X+ ' x sin(pi x) RET a i x RET
X+ @end example
X+ @end group
X+
X+ @group
X+ @example
X+ 1: [y, 1]
X+ 2: (sin(x pi) - x pi cos(x pi)) / pi^2
X+ .
X+
X+ ' [y,1] RET TAB
X+ @end example
X+ @end group
X+
X+ @group
X+ @example
X+ 1: [(sin(y pi) - y pi cos(y pi)) / pi^2, (sin(pi) - pi cos(pi)) / pi^2]
X+ .
X+
X+ V M $ RET
X+ @end example
X+ @end group
X+
X+ @group
X+ @example
X+ 1: (sin(y pi) - y pi cos(y pi)) / pi^2 - (sin(pi) - pi cos(pi)) / pi^2
X+ .
X+
X+ V R -
X+ @end example
X+ @end group
X+
X+ @group
X+ @example
X+ 1: (sin(3.14159 y) - 3.14159 y cos(3.14159 y)) / 9.8696 - 0.3183
X+ .
X+
X+ =
X+ @end example
X+ @end group
X+
X+ @group
X+ @example
X+ 1: [0., -0.95493, 0.63662, -1.5915, 1.2732]
X+ .
X+
X+ v x 5 RET TAB V M $ RET
X+ @end example
X+ @end group
X+
X+ @node Algebra Answer 4, Programming Answer 1, Algebra Answer 3, Answers to Exercises
X+ @subsection Algebra Tutorial Exercise 4
X+
X+ The hard part is that @kbd{V R +} is no longer sufficient to add up all
X+ the contributions from the slices, since the slices have varying
X+ coefficients. So first we must come up with a vector of these
X+ coefficients. Here's one way:
X+
X+ @group
X+ @example
X+ 2: -1 2: 3 1: [4, 2, ..., 4]
X+ 1: [1, 2, ..., 9] 1: [-1, 1, ..., -1] .
X+ . .
X+
X+ 1 n v x 9 RET V M ^ 3 TAB -
X+ @end example
X+ @end group
X+
X+ @group
X+ @example
X+ 1: [4, 2, ..., 4, 1] 1: [1, 4, 2, ..., 4, 1]
X+ . .
X+
X+ 1 | 1 TAB |
X+ @end example
X+ @end group
X+
X+ Now we compute the function values. Note that for this method we need
X+ eleven values, including both endpoints of the desired interval.
X+
X+ @group
X+ @example
X+ 2: [1, 4, 2, ..., 4, 1]
X+ 1: [1, 1.1, 1.2, ... , 1.8, 1.9, 2.]
X+ .
X+
X+ 11 RET 1 RET .1 RET C-u v x
X+ @end example
X+ @end group
X+
X+ @group
X+ @example
X+ 2: [1, 4, 2, ..., 4, 1]
X+ 1: [0., 0.084941, 0.16993, ... ]
X+ .
X+
X+ ' sin(x) ln(x) RET m r p 5 RET V M $ RET
X+ @end example
X+ @end group
X+
X+ Once again this calls for @kbd{V M * V R +}; a simple @kbd{*} does the
X+ same thing.
X+
X+ @group
X+ @example
X+ 1: 11.22 1: 1.112 1: 0.374
X+ . . .
X+
X+ * .1 * 3 /
X+ @end example
X+ @end group
X+
X+ Wow! That's even better than the result from the Taylor series method.
X+
X+ @node Programming Answer 1, Programming Answer 2, Algebra Answer 4, Answers to Exercises
X+ @subsection Programming Tutorial Exercise 1
X+
X+ Here's one way: @kbd{C-x ( M-TAB n M-TAB M-TAB C-x )}.
X+
X+ Here's another: @kbd{C-x ( M-3 n M-2 n C-x )}.
X+
X+ @node Programming Answer 2, Programming Answer 3, Programming Answer 1, Answers to Exercises
X+ @subsection Programming Tutorial Exercise 2
X+
X+ Using the stack: @kbd{C-x ( RET S TAB / C-x )}.
X+
X+ Using algebraic entry: @kbd{C-x ( ' sin($)/$ RET C-x )}.
X+
X+ @node Programming Answer 3, Programming Answer 4, Programming Answer 2, Answers to Exercises
X+ @subsection Programming Tutorial Exercise 3
X+
X+ Here's one way: @kbd{C-x ( RET V R + TAB v l / C-x )}.
X+
X+ @node Programming Answer 4, Programming Answer 5, Programming Answer 3, Answers to Exercises
X+ @subsection Programming Tutorial Exercise 4
X+
X+ @group
X+ @example
X+ 2: 1 1: 1.61803398502 2: 1.61803398502
X+ 1: 20 . 1: 1.61803398875
X+ . .
X+
X+ 1 RET 20 Z < & 1 + Z > 5 Q 1 + 2 /
X+ @end example
X+ @end group
X+
X+ This answer is quite accurate.
X+
X+ @node Programming Answer 5, Programming Answer 6, Programming Answer 4, Answers to Exercises
X+ @subsection Programming Tutorial Exercise 5
X+
X+ Here is the matrix:
X+
X+ @example
X+ [ [ 0, 1 ], * [a, b] = [b, a + b]
X+ [ 1, 1 ] ]
X+ @end example
X+
X+ Thus @samp{[0, 1; 1, 1]^n * [1, 1]} computes Fibonacci numbers @samp{n+1}
X+ and @samp{n+2}. Here's one program that does the job:
X+
X+ @example
X+ C-x ( ' [0, 1; 1, 1] ^ ($-1) * [1, 1] RET v u DEL C-x )
X+ @end example
X+
X+ @node Programming Answer 6, Programming Answer 7, Programming Answer 5, Answers to Exercises
X+ @subsection Programming Tutorial Exercise 6
X+
X+ The trick here is to compute the harmonic numbers differently, so that
X+ the loop counter itself accumulates the sum of reciprocals. We use
X+ a separate variable to hold the integer counter.
X+
X+ @group
X+ @example
X+ 1: 1 2: 1 1: .
X+ . 1: 4
X+ .
X+
X+ 1 s 1 DEL 1 RET 4 Z ( s 2 DEL r 1 1 + s 1 & Z )
X+ @end example
X+ @end group
X+
X+ The body of the loop goes as follows: First save the harmonic sum
X+ so far in variable 2. Then delete it from the stack; the for loop
X+ itself will take care of remembering it for us. Next, recall the
X+ count from variable 1, add one to it, and feed its reciprocal to
X+ the for loop to use as the step value. The for loop will increase
X+ the ``loop counter'' by that amount and keep going until the
X+ loop counter exceeds 4.
X+
X+ @group
X+ @example
X+ 2: 31 3: 31
X+ 1: 3.99498713092 2: 3.99498713092
X+ . 1: 4.02724519544
X+ .
X+
X+ r 1 r 2 RET 31 & +
X+ @end example
X+ @end group
X+
X+ Thus we find that the 30th harmonic number is 3.99, and the 31st
X+ harmonic number is 4.02.
X+
X+ @node Programming Answer 7, Programming Answer 8, Programming Answer 6, Answers to Exercises
X+ @subsection Programming Tutorial Exercise 7
X+
X+ The first step is to compute the derivative @samp{f'(x)} and thus
X+ the formula @samp{x - f(x)/f'(x)}.
X+
X+ (Because this definition is long, it will be repeated in concise form
X+ below. You can use @code{read-kbd-macro} to load it from there.
X+ While you are entering a @samp{Z ` Z '} body in a macro, Calc simply
X+ collects keystrokes without executing them. In the following diagrams
X+ we'll pretend Calc actually executed the keystrokes as you typed them,
X+ just for purposes of illustration.)
X+
X+ @group
X+ @example
X+ 2: sin(cos(x)) - 0.5 3: 4.5
X+ 1: 4.5 2: sin(cos(x)) - 0.5
X+ . 1: -(sin(x) cos(cos(x)))
X+ .
X+
X+ ' sin(cos(x))-0.5 RET 4.5 m r C-x ( Z ` TAB RET a d x RET
X+ @end example
X+ @end group
X+
X+ @group
X+ @example
X+ 2: 4.5
X+ 1: x + (sin(cos(x)) - 0.5) / sin(x) cos(cos(x))
X+ .
X+
X+ / ' x RET TAB - s 1
X+ @end example
X+ @end group
X+
X+ Now, we enter the loop. We'll use a repeat loop with a 20 repetition
X+ limit just in case the method fails to converge for some reason.
X+ (Normally, the @kbd{Z /} command will stop the loop before all 20
X+ repetitions are done.)
X+
X+ @group
X+ @example
X+ 1: 4.5 3: 4.5 2: 4.5
X+ . 2: x + (sin(cos(x)) ... 1: 5.24196456928
X+ 1: 4.5 .
X+ .
X+
X+ DEL 20 Z < RET r 1 TAB l x RET
X+ @end example
X+ @end group
X+
X+ This is the new guess for @samp{x}. Now we compare it with the
X+ old one to see if we've converged.
X+
X+ @group
X+ @example
X+ 3: 5.24196 2: 5.24196 1: 5.24196 1: 5.26345856348
X+ 2: 5.24196 1: 0 . .
X+ 1: 4.5 .
X+ .
X+
X+ RET M-TAB a = Z / Z > Z ' C-x )
X+ @end example
X+ @end group
X+
X+ The loop converges in just a few steps to this value. To check
X+ the result, we can simply substitute it back into the equation.
X+
X+ @group
X+ @example
X+ 3: 5.26345856348 2: 5.26345856348
X+ 2: 5.26345856348 1: 0.499999999997
X+ 1: sin(cos(x)) .
X+ .
X+
X+ RET ' sin(cos(x)) RET TAB l x RET
X+ @end example
X+ @end group
X+
X+ Let's test the new definition again:
X+
X+ @group
X+ @example
X+ 2: x^2 - 9 1: 3.
X+ 1: 1 .
X+ .
X+
X+ ' x^2-9 RET 1 K
X+ @end example
X+ @end group
X+
X+ Once again, here's the full Newton's Method definition:
X+
X+ @group
X+ @example
X+ C-x ( Z ` TAB RET a d x RET / ' x RET TAB - s 1 DEL
X+ 20 Z < RET r 1 TAB l x RET
X+ RET M-TAB a = Z /
X+ Z >
X+ Z '
X+ C-x )
X+ @end example
X+ @end group
X+
X+ @node Programming Answer 8, Programming Answer 9, Programming Answer 7, Answers to Exercises
X+ @subsection Programming Tutorial Exercise 8
X+
X+ The first step is to adjust @samp{z} to be greater than 5. A simple
X+ ``for'' loop will do the job here. If @samp{z} is less than 5, we
X+ reduce the problem using @samp{psi(z) = psi(z+1) - 1/z}. We go
X+ on to compute @samp{psi(z+1)}, and remember to add back a factor of
X+ @samp{-1/z} when we're done. This step is repeated until @samp{z > 5}.
X+
X+ (Because this definition is long, it will be repeated in concise form
X+ below. You can use @code{read-kbd-macro} to load it from there.
X+ While you are entering a @samp{Z ` Z '} body in a macro, Calc simply
X+ collects keystrokes without executing them. In the following diagrams
X+ we'll pretend Calc actually executed the keystrokes as you typed them,
X+ just for purposes of illustration.)
X+
X+ @group
X+ @example
X+ 1: 1. 1: 1.
X+ . .
X+
X+ 1.0 RET C-x ( Z ` s 1 0 s 2 DEL
X+ @end example
X+ @end group
X+
X+ Here, variable 1 holds @code{z} and variable 2 holds the adjustment
X+ factor. If @samp{z < 5}, we use a loop to increase it.
X+
X+ @group
X+ @example
X+ 3: 1. 2: 1. 1: 6.
X+ 2: 1. 1: 1 .
X+ 1: 5 .
X+ .
X+
X+ RET 5 a < Z [ 5 Z ( & s + 2 DEL 1 s + 1 Z ) r 1 Z ]
X+ @end example
X+ @end group
X+
X+ Now we compute the initial part of the sum: @samp{ln(z) - 1/2z}
X+ minus the adjustment factor.
X+
X+ @group
X+ @example
X+ 2: 1.79175946923 2: 1.7084261359 1: -0.57490719743
X+ 1: 0.0833333333333 1: 2.28333333333 .
X+ . .
X+
X+ L r 1 2 * & - r 2 -
X+ @end example
X+ @end group
X+
X+ Now we evaluate the series. We'll use another ``for'' loop counting
X+ up the value of @samp{2 n}.
X+
X+ @group
X+ @example
X+ 3: -0.5749 3: -0.5749 4: -0.5749 2: -0.5749
SHAR_EOF
echo "End of part 14, continue with part 15"
echo "15" > s2_seq_.tmp
exit 0
More information about the Comp.sources.misc
mailing list