home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD1.bin
/
gnu
/
info
/
calc.info-5
(
.txt
)
< prev
next >
Wrap
GNU Info File
|
1994-12-22
|
31KB
|
569 lines
This is Info file calc.info, produced by Makeinfo-1.55 from the input
file calc.texinfo.
This file documents Calc, the GNU Emacs calculator.
Copyright (C) 1990, 1991 Free Software Foundation, Inc.
Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.
Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided also
that the section entitled "GNU General Public License" is included
exactly as in the original, and provided that the entire resulting
derived work is distributed under the terms of a permission notice
identical to this one.
Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions, except that the section entitled "GNU General Public License"
may be included in a translation approved by the author instead of in
the original English.
File: calc.info, Node: Basic Algebra Tutorial, Next: Rewrites Tutorial, Prev: Algebra Tutorial, Up: Algebra Tutorial
Basic Algebra
-------------
If you enter a formula in algebraic mode that refers to variables, the
formula itself is pushed onto the stack. You can manipulate formulas
as regular data objects.
1: 2 x^2 - 6 1: 6 - 2 x^2 1: (6 - 2 x^2) (3 x^2 + y)
. . .
' 2x^2-6 RET n ' 3x^2+y RET *
(*) *Exercise 1.* Do `' x RET Q 2 ^' and `' x RET 2 ^ Q' both wind
up with the same result (`x')? Why or why not? *Note 1: Algebra
Answer 1. (*)
There are also commands for doing common algebraic operations on
formulas. Continuing with the formula from the last example,
1: 18 x^2 + 6 y - 6 x^4 - 2 x^2 y 1: (18 - 2 y) x^2 - 6 x^4 + 6 y
. .
a x a c x RET
First we "expand" using the distributive law, then we "collect" terms
involving like powers of `x'.
Let's find the value of this expression when `x' is 2 and `y' is
one-half.
1: 17 x^2 - 6 x^4 + 3 1: -25
. .
1:2 s l y RET 2 s l x RET
The `s l' command means "let"; it takes a number from the top of the
stack and temporarily assigns it as the value of the variable you
specify. It then evaluates (as if by the `=' key) the next expression
on the stack. After this command, the variable goes back to its
original value, if any.
(An earlier exercise in this tutorial involved storing a value in the
variable `x'; if this value is still there, you will have to unstore it
with `s u x RET' before the above example will work properly.)
Let's find the maximum value of our original expression when `y' is
one-half and `x' ranges over all possible values. We can do this by
taking the derivative with respect to `x' and examining values of `x'
for which the derivative is zero. If the second derivative of the
function at that value of `x' is negative, the function has a local
maximum there.
1: 17 x^2 - 6 x^4 + 3 1: 34 x - 24 x^3
. .
U DEL s 1 a d x RET s 2
Well, the derivative is clearly zero when `x' is zero. To find the
other root(s), let's divide through by `x' and then solve:
1: (34 x - 24 x^3) / x 1: 34 x / x - 24 x^3 / x 1: 34 - 24 x^2
. . .
' x RET / a x a s
1: 34 - 24 x^2 = 0 1: x = 1.19023
. .
0 a = s 3 a S x RET
Notice the use of `a s' to "simplify" the formula. When the default
algebraic simplifications don't do enough, you can use `a s' to tell
Calc to spend more time on the job.
Now we compute the second derivative and plug in our values of `x':
1: 1.19023 2: 1.19023 2: 1.19023
. 1: 34 x - 24 x^3 1: 34 - 72 x^2
. .
a . r 2 a d x RET s 4
(The `a .' command extracts just the righthand side of an equation.
Another method would have been to use `v u' to unpack the equation
`x = 1.19' to `x' and `1.19', then use `M-- M-2 DEL' to delete the `x'.)
2: 34 - 72 x^2 1: -68. 2: 34 - 72 x^2 1: 34
1: 1.19023 . 1: 0 .
. .
TAB s l x RET U DEL 0 s l x RET
The first of these second derivatives is negative, so we know the
function has a maximum value at `x = 1.19023'. (The function also has a
local *minimum* at `x = 0'.)
When we solved for `x', we got only one value even though `34 - 24
x^2 = 0' is a quadratic equation that ought to have two solutions. The
reason is that `a S' normally returns a single "principal" solution.
If it needs to come up with an arbitrary sign (as occurs in the
quadratic formula) it picks `+'. If it needs an arbitrary integer, it
picks zero. We can get a full solution by pressing `H' (the Hyperbolic
flag) before `a S'.
1: 34 - 24 x^2 = 0 1: x = 1.19023 s1 1: x = -1.19023
. . .
r 3 H a S x RET s 5 1 n s l s1 RET
Calc has invented the variable `s1' to represent an unknown sign; it is
supposed to be either +1 or -1. Here we have used the "let" command to
evaluate the expression when the sign is negative. If we plugged this
into our second derivative we would get the same, negative, answer, so
`x = -1.19023' is also a maximum.
To find the actual maximum value, we must plug our two values of `x'
into the original formula.
2: 17 x^2 - 6 x^4 + 3 1: 24.08333 s1^2 - 12.04166 s1^4 + 3
1: x = 1.19023 s1 .
.
r 1 r 5 s l RET
(Here we see another way to use `s l'; if its input is an equation with
a variable on the lefthand side, then `s l' treats the equation like an
assignment to that variable if you don't give a variable name.)
It's clear that this will have the same value for either sign of
`s1', but let's work it out anyway, just for the exercise:
2: [-1, 1] 1: [15.04166, 15.04166]
1: 24.08333 s1^2 ... .
.
[ 1 n , 1 ] TAB V M $ RET
Here we have used a vector mapping operation to evaluate the function
at several values of `s1' at once. `V M $' is like `V M '' except that
it takes the formula from the top of the stack. The formula is
interpreted as a function to apply across the vector at the next-to-top
stack level. Since a formula on the stack can't contain `$' signs,
Calc assumes the variables in the formula stand for different
arguments. It prompts you for an "argument list", giving the list of
all variables in the formula in alphabetical order as the default list.
In this case the default is `(s1)', which is just what we want so we
simply press RET at the prompt.
If there had been several different values, we could have used
`V R X' to find the global maximum.
Calc has a built-in `a P' command that solves an equation using
`H a S' and returns a vector of all the solutions. It simply automates
the job we just did by hand. Applied to our original cubic polynomial,
it would produce the vector of solutions `[1.19023, -1.19023, 0]'.
(There is also an `a X' command which finds a local maximum of a
function. It uses a numerical search method rather than examining the
derivatives, and thus requires you to provide some kind of initial
guess to show it where to look.)
(*) *Exercise 2.* Given a vector of the roots of a polynomial (such
as the output of an `a P' command), what sequence of commands would you
use to reconstruct the original polynomial? (The answer will be unique
to within a constant multiple; choose the solution where the