home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-11-01 | 55.5 KB | 1,209 lines |
- Newsgroups: comp.sources.misc
- From: daveg@synaptics.com (David Gillespie)
- Subject: v24i094: gnucalc - GNU Emacs Calculator, v2.00, Part46/56
- Message-ID: <1991Nov1.183840.21155@sparky.imd.sterling.com>
- X-Md4-Signature: 978bcd10a1385fa395f7fac5ddec7ca2
- Date: Fri, 1 Nov 1991 18:38:40 GMT
- Approved: kent@sparky.imd.sterling.com
-
- Submitted-by: daveg@synaptics.com (David Gillespie)
- Posting-number: Volume 24, Issue 94
- Archive-name: gnucalc/part46
- Environment: Emacs
- Supersedes: gmcalc: Volume 13, Issue 27-45
-
- ---- Cut Here and unpack ----
- #!/bin/sh
- # do not concatenate these parts, unpack them in order with /bin/sh
- # file calc.texinfo continued
- #
- if test ! -r _shar_seq_.tmp; then
- echo 'Please unpack part 1 first!'
- exit 1
- fi
- (read Scheck
- if test "$Scheck" != 46; then
- echo Please unpack part "$Scheck" next!
- exit 1
- else
- exit 0
- fi
- ) < _shar_seq_.tmp || exit 1
- if test ! -f _shar_wnt_.tmp; then
- echo 'x - still skipping calc.texinfo'
- else
- echo 'x - continuing file calc.texinfo'
- sed 's/^X//' << 'SHAR_EOF' >> 'calc.texinfo' &&
- the function names @code{thecoefs} and @code{thefactors} are used only
- as placeholders; there are no actual Calc functions by those names.)
- X
- @kindex H a f
- @tindex factors
- The @kbd{H a f} [@code{factors}] command also factors a polynomial,
- but it returns a list of factors instead of an expression which is the
- product of the factors. Each factor is represented by a sub-vector
- of the factor, and the power with which it appears. For example,
- @cite{x^5 + x^4 - 33 x^3 + 63 x^2} factors to @cite{(x + 7) x^2 (x - 3)^2}
- in @kbd{a f}, or to @cite{[ [x, 2], [x+7, 1], [x-3, 2] ]} in @kbd{H a f}.
- If there is an overall numeric factor, it always comes first in the list.
- The functions @code{factor} and @code{factors} allow a second argument
- when written in algebraic form; @samp{factor(x,v)} factors @cite{x} with
- respect to the specific variable @cite{v}. The default is to factor with
- respect to all the variables that appear in @cite{x}.
- X
- @kindex a c
- @pindex calc-collect
- @tindex collect
- The @kbd{a c} (@code{calc-collect}) [@code{collect}] command rearranges a
- formula as a
- polynomial in a given variable, ordered in decreasing powers of that
- variable. For example, given @cite{1 + 2 x + 3 y + 4 x y^2} on
- the stack, @kbd{a c x} would produce @cite{(2 + 4 y^2) x + (1 + 3 y)},
- and @kbd{a c y} would produce @cite{(4 x) y^2 + 3 y + (1 + 2 x)}.
- The polynomial will be expanded out using the distributive law as
- necessary: Collecting @cite{x} in @cite{(x - 1)^3} produces
- @cite{x^3 - 3 x^2 + 3 x - 1}. Terms not involving @cite{x} will
- not be expanded.
- X
- The ``variable'' you specify at the prompt can actually be any
- expression: @kbd{a c ln(x+1)} will collect together all terms multiplied
- by @samp{ln(x+1)} or integer powers thereof. If @samp{x} also appears
- in the formula in a context other than @samp{ln(x+1)}, @kbd{a c} will
- treat those occurrences as unrelated to @samp{ln(x+1)}, i.e., as constants.
- X
- @kindex a x
- @pindex calc-expand
- @tindex expand
- The @kbd{a x} (@code{calc-expand}) [@code{expand}] command expands an
- expression by applying the distributive law everywhere. It applies to
- products, quotients, and powers involving sums. By default, it fully
- distributes all parts of the expression. With a numeric prefix argument,
- the distributive law is applied only the specified number of times, then
- the partially expanded expression is left on the stack.
- X
- The @kbd{a x} and @kbd{j D} commands are somewhat redundant. Use
- @kbd{a x} if you want to expand all products of sums in your formula.
- Use @kbd{j D} if you want to expand a particular specified term of
- the formula. There is an exactly analogous correspondence between
- @kbd{a f} and @kbd{j M}. (The @kbd{j D} and @kbd{j M} commands
- also know many other kinds of expansions, such as
- @samp{exp(a + b) = exp(a) exp(b)}.)
- X
- Calc's automatic simplifications will sometimes reverse a partial
- expansion. For example, the first step in expanding @cite{(x+1)^3} is
- to write @cite{(x+1) (x+1)^2}. If @kbd{a x} stops there and tries
- to put this formula onto the stack, though, Calc will automatically
- simplify it back to @cite{(x+1)^3} form. The solution is to turn
- simplification off first (@pxref{Simplification Modes}), or to run
- @kbd{a x} without a numeric prefix argument so that it expands all
- the way in one step.
- X
- @kindex a a
- @pindex calc-apart
- @tindex apart
- The @kbd{a a} (@code{calc-apart}) [@code{apart}] command expands a
- rational function by partial fractions. A rational function is the
- quotient of two polynomials; @code{apart} pulls this apart into a
- sum of rational functions with simple denominators. In algebraic
- notation, the @code{apart} function allows a second argument that
- specifies which variable to use as the ``base''; by default, Calc
- chooses the base variable automatically.
- X
- @kindex a n
- @pindex calc-normalize-rat
- @tindex nrat
- The @kbd{a n} (@code{calc-normalize-rat}) [@code{nrat}] command
- attempts to arrange a formula into a quotient of two polynomials.
- For example, given @cite{1 + (a + b/c) / d}, the result would be
- @cite{(b + a c + c d) / c d}. The quotient is reduced, so that
- @kbd{a n} will simplify @cite{(x^2 + 2x + 1) / (x^2 - 1)} by dividing
- out the common factor @cite{x + 1}, yielding @cite{(x + 1) / (x - 1)}.
- X
- @kindex a \
- @pindex calc-poly-div
- @tindex pdiv
- The @kbd{a \} (@code{calc-poly-div}) [@code{pdiv}] command divides
- two polynomials @cite{u} and @cite{v}, yielding a new polynomial
- @cite{q}. If several variables occur in the inputs, the inputs are
- considered multivariate polynomials. (Calc divides by the variable
- with the largest power in @cite{u} first, or, in the case of equal
- powers, chooses the variables in alphabetical order.) For example,
- dividing @cite{x^2 + 3 x + 2} by @cite{x + 2} yields @cite{x + 1}.
- The remainder from the division, if any, is reported at the bottom
- of the screen and is also placed in the Trail along with the quotient.
- X
- Using @code{pdiv} in algebraic notation, you can specify the particular
- variable to be used as the base: `@t{pdiv(}@i{a}@t{,}@i{b}@t{,}@i{x}@t{)}'.
- If @code{pdiv} is given only two arguments (as is always the case with
- the @kbd{a \} command), then it does a multivariate division as outlined
- above.
- X
- @kindex a %
- @pindex calc-poly-rem
- @tindex prem
- The @kbd{a %} (@code{calc-poly-rem}) [@code{prem}] command divides
- two polynomials and keeps the remainder @cite{r}. The quotient
- @cite{q} is discarded. For any formulas @cite{a} and @cite{b}, the
- results of @kbd{a \} and @kbd{a %} satisfy @cite{a = q b + r}.
- (This is analogous to plain @kbd{\} and @kbd{%}, which compute the
- integer quotient and remainder from dividing two numbers.)
- X
- @kindex a /
- @kindex H a /
- @pindex calc-poly-div-rem
- @tindex pdivrem
- @tindex pdivide
- The @kbd{a /} (@code{calc-poly-div-rem}) [@code{pdivrem}] command
- divides two polynomials and reports both the quotient and the
- remainder as a vector @cite{[q, r]}. The @kbd{H a /} [@code{pdivide}]
- command divides two polynomials and constructs the formula
- @cite{q + r/b} on the stack. (Naturally if the remainder is zero,
- this will immediately simplify to @cite{q}.)
- X
- @kindex a g
- @pindex calc-poly-gcd
- @tindex pgcd
- The @kbd{a g} (@code{calc-poly-gcd}) [@code{pgcd}] command computes
- the greatest common divisor of two polynomials. (The GCD actually
- is unique only to within a constant multiplier; Calc attempts to
- choose a GCD which will be unsurprising.) For example, the @kbd{a n}
- command uses @kbd{a g} to take the GCD of the numerator and denominator
- of a quotient, then divides each by the result using @kbd{a \}. (The
- definition of GCD ensures that this division can take place without
- leaving a remainder.)
- X
- While the polynomials used in operations like @kbd{a /} and @kbd{a g}
- often have integer coefficients, this is not required. Calc can also
- deal with polynomials over the rationals or floating-point reals.
- Polynomials with modulo-form coefficients are also useful in many
- applications; if you enter @samp{(x^2 + 3 x - 1) mod 5}, Calc
- automatically transforms this into a polynomial over the field of
- integers mod 5: @samp{(1 mod 5) x^2 + (3 mod 5) x + (4 mod 5)}.
- X
- Congratulations and thanks go to Ove Ewerlid
- (@code{ewerlid@@mizar.DoCS.UU.SE}), who contributed many of the
- polynomial routines used in the above commands.
- X
- @xref{Decomposing Polynomials}, for several useful functions for
- extracting the individual coefficients of a polynomial.
- X
- @node Calculus, Solving Equations, Polynomials, Algebra
- @section Calculus
- X
- @noindent
- The following calculus commands do not automatically simplify their
- inputs or outputs using @code{calc-simplify}. You may find it helps
- to do this by hand by typing @kbd{a s} or @kbd{a e}. It may also help
- to use @kbd{a x} and/or @kbd{a c} to arrange a result in the most
- readable way.
- X
- @menu
- * Differentiation::
- * Integration::
- * Customizing the Integrator::
- * Numerical Integration::
- * Taylor Series::
- @end menu
- X
- @node Differentiation, Integration, Calculus, Calculus
- @subsection Differentiation
- X
- @kindex a d
- @kindex H a d
- @pindex calc-derivative
- @tindex deriv
- @tindex tderiv
- @noindent
- The @kbd{a d} (@code{calc-derivative}) [@code{deriv}] command computes
- the derivative of the expression on the top of the stack with respect to
- some variable, which it will prompt you to enter. Normally, variables
- in the formula other than the specified differentiation variable are
- considered constant, i.e., @samp{deriv(y,x)} is reduced to zero. With
- the Hyperbolic flag, the @code{tderiv} (total derivative) operation is used
- instead, in which derivatives of variables are not reduced to zero
- unless those variables are known to be ``constant,'' i.e., independent
- of any other variables. (The built-in special variables like @code{pi}
- are considered constant, as are variables that have been declared
- @code{const}; @pxref{Declarations}.)
- X
- With a numeric prefix argument @var{n}, this computes the @var{n}th
- derivative.
- X
- When working with trigonometric functions, it is best to switch to
- radians mode first (with @kbd{m r}). The derivative of @samp{sin(x)}
- in degrees is @samp{(pi/180) cos(x)}, probably not the expected
- answer!
- X
- If you use the @code{deriv} function directly in an algebraic formula,
- you can write @samp{deriv(f,x,x0)} which represents the derivative
- of @cite{f} with respect to @cite{x}, evaluated at the point @cite{x=x0}.
- X
- If the formula being differentiated contains functions which Calc does
- not know, the derivatives of those functions are produced by adding
- primes (apostrophe characters). For example, @samp{deriv(f(2x), x)}
- produces @samp{2 f'(2 x)}, where the function @code{f'} represents the
- derivative of @code{f}.
- X
- For functions you have defined with the @kbd{Z F} command, Calc expands
- the functions according to their defining formulas unless you have
- also defined @code{f'} suitably. For example, suppose we define
- @samp{sinc(x) = sin(x)/x} using @kbd{Z F}. If we then differentiate
- the formula @samp{sinc(2 x)}, the formula will be expanded to
- @samp{sin(2 x) / (2 x)} and differentiated. However, if we also
- define @samp{sinc'(x) = dsinc(x)}, say, then Calc will write the
- result as @samp{2 dsinc(x)}. @xref{Algebraic Definitions}.
- X
- For multi-argument functions @samp{f(x,y,z)}, the derivative with respect
- to the first argument is written @samp{f'(x,y,z)}; derivatives with
- respect to the other arguments are @samp{f'2(x,y,z)} and @samp{f'3(x,y,z)}.
- Various higher-order derivatives can be formed in the obvious way, e.g.,
- @samp{f'@var{}'(x)} (the second derivative of @code{f}) or
- @samp{f'@var{}'2'3(x,y,z)} (@code{f} differentiated with respect to each
- argument once).@refill
- X
- @node Integration, Customizing the Integrator, Differentiation, Calculus
- @subsection Integration
- X
- @kindex a i
- @pindex calc-integral
- @tindex integ
- @noindent
- The @kbd{a i} (@code{calc-integral}) [@code{integ}] command computes the
- indefinite integral of the expression on the top of the stack with
- respect to a variable. The integrator is not guaranteed to work for
- all integrable functions, but it is able to integrate several large
- classes of formulas. In particular, any polynomial or rational function
- (a polynomial divided by a polynomial) is acceptable. (Rational functions
- don't have to be in explicit quotient form, however; @c{$x/(1+x^{-2}$}
- @cite{x/(1+x^-2)}
- is not strictly a quotient of polynomials, but it is equivalent to
- @cite{x^3/(x^2+1)}, which is.) Also, square roots of terms involving
- @cite{x} and @cite{x^2} may appear in rational functions being
- integrated. Finally, rational functions involving trigonometric or
- hyperbolic functions can be integrated.
- X
- @ifinfo
- If you use the @code{integ} function directly in an algebraic formula,
- you can also write @samp{integ(f,x,v)} which expresses the resulting
- indefinite integral in terms of variable @code{v} instead of @code{x}.
- With four arguments, @samp{integ(f(x),x,a,b)} represents a definite
- integral from @code{a} to @code{b}.
- @end ifinfo
- @tex
- If you use the @code{integ} function directly in an algebraic formula,
- you can also write @samp{integ(f,x,v)} which expresses the resulting
- indefinite integral in terms of variable @code{v} instead of @code{x}.
- With four arguments, @samp{integ(f(x),x,a,b)} represents a definite
- integral $\int_a^b f(x) \, dx$.
- @end tex
- X
- Please note that the current implementation of Calc's integrator sometimes
- produces results that are significantly more complex than they need to
- be. For example, the integral Calc finds for @c{$1/(x+\sqrt{x^2+1})$}
- @cite{1/(x+sqrt(x^2+1))}
- is several times more complicated than the answer Mathematica
- returns for the same input, although the two forms are numerically
- equivalent. Also, any indefinite integral should be considered to have
- an arbitrary constant of integration added to it, although Calc does not
- write an explicit constant of integration in its result. For example,
- Calc's solution for @c{$1/(1+\tan x)$}
- @cite{1/(1+tan(x))} differs from the solution given
- in the @emph{CRC Math Tables} by a constant factor of @c{$\pi i / 2$}
- @cite{pi i / 2},
- due to a different choice of constant of integration.
- X
- The Calculator remembers all the integrals it has done. If conditions
- change in a way that would invalidate the old integrals, they will be
- thrown out. If you suspect this is not happening when it should,
- use the @code{calc-flush-caches} command; @pxref{Caches}.
- X
- @vindex IntegLimit
- Calc normally will pursue integration by substitution or integration by
- parts up to 3 nested times before abandoning an approach as fruitless.
- If the integrator is taking too long, you can lower this limit by storing
- a number (like 2) in the variable @code{IntegLimit}. (The @kbd{s I}
- command is a convenient way to edit @code{IntegLimit}.) If this variable
- has no stored value or does not contain a nonnegative integer, a limit
- of 3 is used. The lower this limit is, the greater the chance that Calc
- will be unable to integrate a function it could otherwise handle. Raising
- this limit allows the Calculator to solve more integrals, though the time
- it takes may grow exponentially. You can monitor the integrator's actions
- by creating an Emacs buffer called @code{*Trace*}. If such a buffer
- exists, the @kbd{a i} command will write a log of its actions there.
- X
- If you want to manipulate integrals in a purely symbolic way, you can
- set the integration nesting limit to 0 to prevent all but fast
- table-lookup solutions of integrals. You might then wish to define
- rewrite rules for integration by parts, various kinds of substitutions,
- and so on. @xref{Rewrite Rules}.
- X
- @node Customizing the Integrator, Numerical Integration, Integration, Calculus
- @subsection Customizing the Integrator
- X
- @vindex IntegRules
- @noindent
- Calc has two built-in rewrite rules called @code{IntegRules} and
- @code{IntegAfterRules} which you can edit to define new integration
- methods. @xref{Rewrite Rules}. At each step of the integration process,
- Calc wraps the current integrand in a call to the fictitious function
- @samp{integtry(@var{expr},@var{var})}, where @var{expr} is the
- integrand and @var{var} is the integration variable. If your rules
- rewrite this to be a plain formula (not a call to @code{integtry}, then
- Calc will use this formula as the integral of @var{expr}. For example,
- the rule @samp{integtry(mysin(x),x) := -mycos(x)} would define a rule to
- integrate a function @code{mysin} that acts like the sine function.
- Then, putting @samp{4 mysin(2y+1)} on the stack and typing @kbd{a i y}
- will produce the integral @samp{-2 mycos(2y+1)}. Note that Calc has
- automatically made various transformations on the integral to allow it
- to use your rule; integral tables generally give rules for
- @samp{mysin(a x + b)}, but you don't need to use this generality in
- your @code{IntegRules}.
- X
- Your rule may do further integration by calling @code{integ}. For
- example, @samp{integtry(twice(u),x) := twice(integ(u))} allows Calc
- to integrate @samp{twice(sin(x))} to get @samp{twice(-cos(x))}.
- Note that @code{integ} was called with only one argument. This notation
- is allowed only within @code{IntegRules}; it means ``integrate this
- with respect to the same integration variable.'' If Calc is unable
- to integrate @code{u}, the integration that invoked @code{IntegRules}
- also fails. Thus integrating @samp{twice(f(x))} fails, returning the
- unevaluated integral @samp{integ(twice(f(x)), x)}. It is still legal
- to call @code{integ} with two or more arguments, however; in this case,
- if @code{u} is not integrable, @code{twice} itself will still be
- integrated: If the above rule is changed to @samp{... := twice(integ(u,x))},
- then integrating @samp{twice(f(x))} will yield @samp{twice(integ(f(x),x))}.
- X
- If a rule instead produces the formula @samp{integsubst(@var{sexpr},
- @var{svar})}, either replacing the top-level @code{integtry} call or
- nested anywhere inside the expression, then Calc will apply the
- substitution @samp{@var{u} = @var{sexpr}(@var{svar})} to try to
- integrate the original @var{expr}. For example, the rule
- @samp{sqrt(a) := integsubst(sqrt(x),x)} says that if Calc ever finds
- a square root in the integrand, it should attempt the substitution
- @samp{u = sqrt(x)}. (This particular rule is unnecessary because
- Calc always tries ``obvious'' substitutions where @var{sexpr} actually
- appears in the integrand.) The variable @var{svar} may be the same
- as the @var{var} that appeared in the call to @code{integtry}, but
- it need not be.
- X
- When integrating according to an @code{integsubst}, Calc uses the
- equation solver to find the inverse of @var{sexpr} (if the integrand
- refers to @var{var} anywhere except in subexpressions that exactly
- match @var{sexpr}). It uses the differentiator to find the derivative
- of @var{sexpr} and/or its inverse (it has two methods that use one
- derivative or the other). You can also specify these items by adding
- extra arguments to the @code{integsubst} your rules construct; the
- general form is @samp{integsubst(@var{sexpr}, @var{svar}, @var{sinv},
- @var{sprime})}, where @var{sinv} is the inverse of @var{sexpr} (still
- written as a function of @var{svar}), and @var{sprime} is the
- derivative of @var{sexpr} with respect to @var{svar}. If you don't
- specify these things, and Calc is not able to work them out on its
- own with the information it knows, then your substitution rule will
- work only in very specific, simple cases.
- X
- Calc applies @code{IntegRules} as if by @kbd{C-u 1 a r IntegRules};
- in other words, Calc stops rewriting as soon as any rule in your rule
- set succeeds. (If it weren't for this, the @samp{integsubst(sqrt(x),x)}
- example above would keep on adding layers of @code{integsubst} calls
- forever!)
- X
- @vindex IntegSimpRules
- Another set of rules, stored in @code{IntegSimpRules}, are applied
- every time the integrator uses @kbd{a s} to simplify an intermediate
- result. For example, putting the rule @samp{twice(x) := 2 x} into
- @code{IntegSimpRules} would tell Calc to convert the @code{twice}
- function into a form it knows whenever integration is attempted.
- X
- One more way to influence the integrator is to define a function with
- the @kbd{Z F} command (@pxref{Algebraic Definitions}). Calc's
- integrator automatically expands such functions according to their
- defining formulas, even if you originally asked for the function to
- be left unevaluated for symbolic arguments. (Certain other Calc
- systems, such as the differentiator and the equation solver, also
- do this.)
- X
- @vindex IntegAfterRules
- Sometimes Calc is able to find a solution to your integral, but it
- expresses the result in a way that is unnecessarily complicated. If
- this happens, you can either use @code{integsubst} hints as described
- above to try to hint at a more direct path to the desired result, or
- you can use @code{IntegAfterRules}. This is an extra rule set that
- runs after the main integrator returns its result; basically, Calc does
- an @kbd{a r IntegAfterRules} on the result before showing it to you.
- (It also does an @kbd{a s}, without @code{IntegSimpRules}, after that
- to further simplify the result.) For example, Calc's integrator
- sometimes produces expressions of the form @samp{ln(1+x) - ln(1-x)};
- the default @code{IntegAfterRules} rewrite this into the more readable
- form @samp{2 arctanh(x)}. Note that, unlike @code{IntegRules},
- @code{IntegSimpRules} and @code{IntegAfterRules} are applied any number
- of times until no further changes are possible. Rewriting by
- @code{IntegAfterRules} occurs only after the main integrator has
- finished, not at every step as for @code{IntegRules} and
- @code{IntegSimpRules}.
- X
- @node Numerical Integration, Taylor Series, Customizing the Integrator, Calculus
- @subsection Numerical Integration
- X
- @kindex a I
- @pindex calc-num-integral
- @tindex ninteg
- @noindent
- If you want a purely numerical answer to an integration problem, you can
- use the @kbd{a I} (@code{calc-num-integral}) [@code{ninteg}] command. This
- command prompts for an integration variable, a lower limit, and an
- upper limit. Except for the integration variable, all other variables
- that appear in the integrand formula must have stored values. (A stored
- value, if any, for the integration variable itself is ignored.)
- X
- Numerical integration works by evaluating your formula at many points in
- the specified interval. Calc uses an ``open Romberg'' method; this means
- that it does not evaluate the formula actually at the endpoints (so that
- it is safe to integrate @samp{sin(x)/x} from zero, for example). Also,
- the Romberg method works especially well when the function being
- integrated is fairly smooth. If the function is not smooth, Calc will
- have to evaluate it at quite a few points before it can accurately
- determine the value of the integral.
- X
- Integration is much faster when the current precision is small. It is
- best to set the precision to the smallest acceptable number of digits
- before you use @kbd{a I}. If Calc appears to be taking too long, press
- @kbd{C-g} to halt it and try a lower precision. If Calc still appears
- to need hundreds of evaluations, check to make sure your function is
- well-behaved in the specified interval.
- X
- It is possible for the lower integration limit to be @samp{-inf} (minus
- infinity). Likewise, the upper limit may be plus infinity. Calc
- internally transforms the integral into an equivalent one with finite
- limits.
- X
- @node Taylor Series, , Numerical Integration, Calculus
- @subsection Taylor Series
- X
- @kindex a t
- @pindex calc-taylor
- @tindex taylor
- @noindent
- The @kbd{a t} (@code{calc-taylor}) [@code{taylor}] command computes a
- power series expansion or Taylor series of a function. You specify the
- variable and the desired number of terms. You may give an expression of
- the form @samp{@var{var} = @var{a}} or @samp{@var{var} - @var{a}} instead
- of just a variable to produce a Taylor expansion about the point @var{a}.
- You may specify the number of terms with a numeric prefix argument;
- otherwise the command will prompt you for the number of terms. Note that
- many series expansions have coefficients of zero for some terms, so you
- may appear to get fewer terms than you asked for.@refill
- X
- If the @kbd{a i} command is unable to find a symbolic integral for a
- function, you can get an approximation by integrating the function's
- Taylor series.
- X
- @node Solving Equations, Numerical Solutions, Calculus, Algebra
- @section Solving Equations
- X
- @noindent
- @kindex a S
- @pindex calc-solve-for
- @tindex solve
- @cindex Equations, solving
- @cindex Solving equations
- The @kbd{a S} (@code{calc-solve-for}) [@code{solve}] command rearranges
- an equation to solve for a specific variable. An equation is an
- expression of the form @cite{L = R}. For example, the command @kbd{a S x}
- will rearrange @cite{y = 3x + 6} to the form, @cite{x = y/3 - 2}. If the
- input is not an equation, it is treated like an equation of the
- form @cite{X = 0}.
- X
- This command also works for inequalities, as in @cite{y < 3x + 6}.
- Some inequalities cannot be solved where the analogous equation could
- be; for example, solving @cite{a < b c} for @cite{b} is impossible
- without knowing the sign of @cite{c}. In this case, @kbd{a S} will
- produce the result @c{$b \mathbin{\hbox{\code{!=}}} a/c$}
- @cite{b != a/c} (using the not-equal-to operator)
- to signify that the direction of the inequality was unknown. The
- inequality @c{$a \le b \, c$}
- @cite{a <= b c} is not even partially solved.
- @xref{Declarations}, for a way to tell Calc that the signs of the
- variables in a formula are in fact known.
- X
- Two useful commands for working with the result of @kbd{a S} are
- @kbd{a .} (@pxref{Logical Operations}), which converts @samp{x = y/3 - 2}
- to @samp{y/3 - 2}, and @kbd{s l} (@pxref{Let Command}) which evaluates
- a formula with @samp{x} set equal to @samp{y/3 - 2}.
- X
- @menu
- * Multiple Solutions::
- * Solving Systems of Equations::
- * Decomposing Polynomials::
- @end menu
- X
- @node Multiple Solutions, Solving Systems of Equations, Solving Equations, Solving Equations
- @subsection Multiple Solutions
- X
- @noindent
- @kindex H a S
- @tindex fsolve
- Some equations have more than one solution. The Hyperbolic flag
- (@code{H a S}) [@code{fsolve}] tells the solver to report the fully
- general family of solutions. It will invent variables @code{n1},
- @code{n2}, @dots{}, which represent independent arbitrary integers, and
- @code{s1}, @code{s2}, @dots{}, which represent independent arbitrary
- signs (either @i{+1} or @i{-1}). If you don't use the Hyperbolic
- flag, Calc will use zero in place of all arbitrary integers, and plus
- one in place of all arbitrary signs. Note that variables like @code{n1}
- and @code{s1} are not given any special interpretation in Calc except by
- @code{calc-solve-for} itself. As usual, you can use the @kbd{s l}
- (@code{calc-let}) command to obtain solutions for various actual values
- of these variables.
- X
- For example, @kbd{' x^2 = y @key{RET} H a S x @key{RET}} solves to
- get @samp{x = s1 sqrt(y)}, indicating that the two solutions to the
- equation are @samp{sqrt(y)} and @samp{-sqrt(y)}. Another way to
- think about it is that the square-root operation is really a
- two-valued function; since every Calc function must return a
- single result, @code{sqrt} chooses to return the positive result.
- Then @kbd{H a S} doctors this result using @code{s1} to indicate
- the full set of possible values of the mathematical square-root.
- X
- There is a similar phenomenon going the other direction: Suppose
- we solve @samp{sqrt(y) = x} for @code{y}. Calc squares both sides
- to get @samp{y = x^2}. This is correct, except that it introduces
- some dubious solutions. Consider solving @samp{sqrt(y) = -3}:
- Calc will report @cite{y = 9} as a valid solution, which is true
- in the mathematical sense of square-root, but false (there is no
- solution) for the actual Calc positive-valued @code{sqrt}. This
- happens for both @kbd{a S} and @kbd{H a S}.
- X
- @cindex @code{GenCount} variable
- @vindex GenCount
- @tindex an
- @tindex as
- If you store a positive integer in the Calc variable @code{GenCount},
- then Calc will generate formulas of the form @samp{as(@var{n})} for
- arbitrary signs, and @samp{an(@var{n})} for arbitrary integers,
- where @var{n} represents succeeding values taken by incrementing
- @code{GenCount} by one. While the normal arbitrary sign and
- integer symbols start over at @code{s1} and @code{n1} with each
- new Calc command, the @code{GenCount} approach will give each
- arbitrary value a name that is unique throughout the entire Calc
- session. Also, the arbitrary values are function calls instead
- of variables, which is advantageous in some cases. For example,
- you can make a rewrite rule that recognizes all arbitrary signs
- using a pattern like @samp{as(n)}. The @kbd{s l} command only works
- on variables, but you can use the @kbd{a b} (@code{calc-substitute})
- command to substitute actual values for function calls like @samp{as(3)}.
- X
- The @kbd{s G} (@code{calc-edit-GenCount}) command is a convenient
- way to create or edit this variable. Press @kbd{M-# M-#} to finish.
- X
- If you have not stored a value in @code{GenCount}, or if the value
- in that variable is not a positive integer, the regular
- @code{s1}/@code{n1} notation is used.
- X
- @kindex I a S
- @kindex H I a S
- @tindex finv
- @tindex ffinv
- With the Inverse flag, @kbd{I a S} [@code{finv}] treats the expression
- on top of the stack as a function of the specified variable and solves
- to find the inverse function, written in terms of the same variable.
- For example, @kbd{I a S x} inverts @cite{2x + 6} to @cite{x/2 - 3}.
- You can use both Inverse and Hyperbolic [@code{ffinv}] to obtain a
- fully general inverse, as described above.
- X
- @kindex a P
- @pindex calc-poly-roots
- @tindex roots
- Some equations, specifically polynomials, have a known, finite number
- of solutions. The @kbd{a P} (@code{calc-poly-roots}) [@code{roots}]
- command uses @kbd{H a S} to solve an equation in general form, then, for
- all arbitrary-sign variables like @code{s1}, and all arbitrary-integer
- variables like @code{n1} for which @code{n1} only usefully varies over
- a finite range, it expands these variables out to all their possible
- values. The results are collected into a vector, which is returned.
- For example, @samp{roots(x^4 = 1, x)} returns the four solutions
- @samp{[1, -1, (0, 1), (0, -1)]}. Generally an @var{n}th degree
- polynomial will always have @var{n} roots on the complex plane.
- (If you have given a @code{real} declaration for the solution
- variable, then only the real-valued solutions, if any, will be
- reported; @pxref{Declarations}.)
- X
- Note that because @kbd{a P} uses @kbd{H a S}, it is able to deliver
- symbolic solutions if the polynomial has symbolic coefficients. Also
- note that Calc's solver is not able to get exact symbolic solutions
- to all polynomials. Polynomials containing powers up to @cite{x^4}
- can always be solved exactly; polynomials of higher degree sometimes
- can be: @cite{x^6 + x^3 + 1} is converted to @cite{(x^3)^2 + (x^3) + 1},
- which can be solved for @cite{x^3} using the quadratic equation, and then
- for @cite{x} by taking cube roots. But in many cases, like
- @cite{x^6 + x + 1}, Calc does not know how to rewrite the polynomial
- into a form it can solve. The @kbd{a P} command can still deliver a
- list of numerical roots, however, provided that symbolic mode (@kbd{m s})
- is not turned on. (If you work with symbolic mode on, recall that the
- @kbd{N} (@code{calc-eval-num}) key is a handy way to reevaluate the
- formula on the stack with symbolic mode temporarily off.) Naturally,
- @kbd{a P} can only provide numerical roots if the polynomial coefficents
- are all numbers (real or complex).
- X
- @node Solving Systems of Equations, Decomposing Polynomials, Multiple Solutions, Solving Equations
- @subsection Solving Systems of Equations
- X
- @noindent
- @cindex Systems of equations, symbolic
- You can also use the commands described above to solve systems of
- simultaneous equations. Just create a vector of equations, then
- specify a vector of variables for which to solve. (You can omit
- the surrounding brackets when entering the vector of variables
- at the prompt.)
- X
- For example, putting @samp{[x + y = a, x - y = b]} on the stack
- and typing @kbd{a S x,y @key{RET}} produces the vector of solutions
- @samp{[x = a - (a-b)/2, y = (a-b)/2]}. The result vector will
- have the same length as the variables vector, and the variables
- will be listed in the same order there. Note that the solutions
- are not always simplified as far as possible; the solution for
- @cite{x} here could be improved by an application of @kbd{j M}
- (@code{calc-sel-merge}) on the @samp{/} sign, followed by
- @kbd{M-2 j v} also on the @samp{/} sign (which runs @kbd{a s},
- @code{calc-simplify}, to simplify the quotient).
- X
- Calc's algorithm works by trying to eliminate one variable at a
- time by solving one of the equations for that variable and then
- substituting into the other equations. Calc will try all the
- possiblities, but you can speed things up by noting that Calc
- first tries to eliminate the first variable with the first
- equation, then the second variable with the second equation,
- and so on. It also helps to put the simpler (e.g., more linear)
- equations toward the front of the list. Calc's algorithm will
- solve any system of linear equations, and also many kinds of
- nonlinear systems.
- X
- @tindex elim
- Normally there will be as many variables as equations. If you
- give fewer variables than equations (an ``over-determined'' system
- of equations), Calc will find a partial solution. For example,
- typing @kbd{a S y @key{RET}} with the above system of equations
- would produce @samp{[y = a - x]}. There are now several ways to
- express this solution in terms of the original variables; Calc uses
- the first one that it finds. You can control the choice by adding
- variable specifiers of the form @samp{elim(@var{v})} to the
- variables list. This says that @var{v} should be eliminated from
- the equations; the variable will not appear at all in the solution.
- For example, typing @kbd{a S y,elim(x)} would yield
- @samp{[y = a - (b+a)/2]}.
- X
- If the variables list contains only @code{elim} specifiers,
- Calc simply eliminates those variables from the equations
- and then returns the resulting equations. For example,
- @kbd{a S elim(x)} produces @samp{[a - 2 y = b]}. Every variable
- eliminated will reduce the number of equations in the system
- by one.
- X
- Again, @kbd{a S} gives you one solution to the system of
- equations. If there are several solutions, you can use @kbd{H a S}
- to get a general family of solutions, or, if there is a finite
- number of solutions, you can use @kbd{a P} to get a list. (In
- the latter case, the result will take the form of a matrix where
- the rows are different solutions and the columns correspond to the
- variables you requested.)
- X
- Another way to deal with certain kinds of overdetermined systems of
- equations is the @kbd{a F} command, which does least-squares fitting
- to satisfy the equations. @xref{Curve Fitting}.
- X
- @node Decomposing Polynomials, , Solving Systems of Equations, Solving Equations
- @subsection Decomposing Polynomials
- X
- @noindent
- @tindex poly
- The @code{poly} function takes a polynomial and a variable as
- arguments, and returns a vector of polynomial coefficients (constant
- coefficient first). For example, @samp{poly(x^3 + 2 x, x)} returns
- @cite{[0, 2, 0, 1]}. If the input is not a polynomial in @cite{x},
- the call to @code{poly} is left in symbolic form. If the input does
- not involve the variable @cite{x}, the input is returned in a list
- of length one, representing a polynomial with only a constant
- coefficient. The call @samp{poly(x, x)} returns the vector @cite{[0, 1]}.
- The last element of the returned vector is guaranteed to be nonzero;
- note that @samp{poly(0, x)} returns the empty vector @cite{[]}.
- Note that @cite{x} may actually be any formula; for example,
- @samp{poly(sin(x)^2 - sin(x) + 3, sin(x))} returns @cite{[3, -1, 1]}.
- X
- @cindex Coefficients of polynomial
- @cindex Degree of polynomial
- To get the @cite{x^k} coefficient of polynomial @cite{p}, use
- @samp{poly(p, x)_(k+1)}. To get the degree of polynomial @cite{p},
- use @samp{vlen(poly(p, x)) - 1}. For example, @samp{poly((x+1)^4, x)}
- returns @samp{[1, 4, 6, 4, 1]}, so @samp{poly((x+1)^4, x)_(2+1)}
- gives the @cite{x^2} coefficient of this polynomial, 6.
- X
- @tindex gpoly
- One important feature of the solver is its ability to recognize
- formulas which are ``essentially'' polynomials. This ability is
- made available to the user through the @code{gpoly} function, which
- is used just like @code{poly}: @samp{gpoly(@var{expr}, @var{var})}.
- If @var{expr} is a polynomial in some term which includes @var{var}, then
- this function will return a vector @samp{[@var{x}, @var{c}, @var{a}]}
- where @var{x} is the term that depends on @var{var}, @var{c} is a
- vector of polynomial coefficients (like the one returned by @code{poly}),
- and @var{a} is a multiplier which is usually 1. Basically,
- @samp{@var{expr} = @var{a}*(@var{c}_1 + @var{c}_2 @var{x} +
- @var{c}_3 @var{x}^2 + ...)}. The last element of @var{c} is
- guaranteed to be non-zero, and @var{c} will not equal @samp{[1]}
- (i.e., the trivial decomposition @var{expr} = @var{x} is not
- considered a polynomial). One side effect is that @samp{gpoly(x, x)}
- and @samp{gpoly(6, x)}, both of which might be expected to recognize
- their arguments as polynomials, will not because the decomposition
- is considered trivial.
- X
- For example, @samp{gpoly((x-2)^2, x)} returns @samp{[x, [4, -4, 1], 1]},
- since the expanded form of this polynomial is @cite{4 - 4 x + x^2}.
- X
- The term @var{x} may itself be a polynomial in @var{var}. This is
- done to reduce the size of the @var{c} vector. For example,
- @samp{gpoly(x^4 + x^2 - 1, x)} returns @samp{[x^2, [-1, 1, 1], 1]},
- since a quadratic polynomial in @cite{x^2} is easier to solve than
- a quartic polynomial in @cite{x}.
- X
- A few more examples of the kinds of polynomials @code{gpoly} can
- discover:
- X
- @smallexample
- sin(x) - 1 [sin(x), [-1, 1], 1]
- x + 1/x - 1 [x, [1, -1, 1], 1/x]
- x + 1/x [x^2, [1, 1], 1/x]
- x^3 + 2 x [x^2, [2, 1], x]
- x + x^2:3 + sqrt(x) [x^1:6, [1, 1, 0, 1], x^1:2]
- x^(2a) + 2 x^a + 5 [x^a, [5, 2, 1], 1]
- (exp(-x) + exp(x)) / 2 [e^(2 x), [0.5, 0.5], e^-x]
- @end smallexample
- X
- The @code{poly} and @code{gpoly} functions accept a third integer argument
- which specifies the largest degree of polynomial that is acceptable.
- If this is @cite{n}, then only @var{c} vectors of length @cite{n+1}
- or less will be returned. Otherwise, the @code{poly} or @code{gpoly}
- call will remain in symbolic form. For example, the equation solver
- can handle quartics and smaller polynomials, so it calls
- @samp{gpoly(@var{expr}, @var{var}, 4)} to discover whether @var{expr}
- can be treated by its linear, quadratic, cubic, or quartic formulas.
- X
- @tindex pdeg
- The @code{pdeg} function computes the degree of a polynomial;
- @samp{pdeg(p,x)} is the highest power of @code{x} that appears in
- @code{p}. This is the same as @samp{vlen(poly(p,x))-1}, but is
- much more efficient. If @code{p} is constant with respect to @code{x},
- then @samp{pdeg(p,x) = 0}. If @code{p} is not a polynomial in @code{x}
- (e.g., @samp{pdeg(2 cos(x), x)}, the function remains unevaluated.
- It is possible to omit the second argument @code{x}, in which case
- @samp{pdeg(p)} returns the highest total degree of any term of the
- polynomial, counting all variables that appear in @code{p}. Note
- that @code{pdeg(c) = pdeg(c,x) = 0} for any nonzero constant @code{c};
- the degree of the constant zero is considered to be @code{-inf}
- (minus infinity).
- X
- @tindex plead
- The @code{plead} function finds the leading term of a polynomial.
- Thus @samp{plead(p,x)} is equivalent to @samp{poly(p,x)_vlen(poly(p,x))},
- though again more efficient. In particular, @samp{plead((2x+1)^10, x)}
- returns 1024 without expanding out the list of coefficients. The
- value of @code{plead(p,x)} will be zero only if @cite{p = 0}.
- X
- @tindex pcont
- The @code{pcont} function finds the @dfn{content} of a polynomial. This
- is the greatest common divisor of all the coefficients of the polynomial.
- With two arguments, @code{pcont(p,x)} effectively uses @samp{poly(p,x)}
- to get a list of coefficients, then uses @code{pgcd} (the polynomial
- GCD function) to combine these into an answer. For example,
- @samp{pcont(4 x y^2 + 6 x^2 y, x)} is @samp{2 y}. The content is
- basically the ``biggest'' polynomial that can be divided into @code{p}
- exactly. The sign of the content is the same as the sign of the leading
- coefficient.
- X
- With only one argument, @samp{pcont(p)} computes the numerical
- content of the polynomial, i.e., the @code{gcd} of the numerical
- coefficients of all the terms in the formula. Note that @code{gcd}
- is defined on rational numbers as well as integers; it computes
- the @code{gcd} of the numerators and the @code{lcm} of the
- denominators. Thus @samp{pcont(4:3 x y^2 + 6 x^2 y)} returns 2:3.
- Dividing the polynomial by this number will clear all the
- denominators, as well as dividing by any common content in the
- numerators. The numerical content of a polynomial is negative only
- if all the coefficients in the polynomial are negative.
- X
- @tindex pprim
- The @code{pprim} function finds the @dfn{primitive part} of a
- polynomial, which is simply the polynomial divided (using @code{pdiv}
- if necessary) by its content. If the input polynomial has rational
- coefficients, the result will have integer coefficients in simplest
- terms.
- X
- @node Numerical Solutions, Curve Fitting, Solving Equations, Algebra
- @section Numerical Solutions
- X
- @noindent
- Not all equations can be solved symbolically. The commands in this
- section use numerical algorithms that can find a solution to a specific
- instance of an equation to any desired accuracy. Note that these
- commands are slower than their algebraic cousins; it is a good idea
- to try @kbd{a S} before resorting to @kbd{a R}.
- X
- (@xref{Curve Fitting}, for some other, more specialized, operations
- on numerical data.)
- X
- @menu
- * Root Finding::
- * Minimization::
- * Numerical Systems of Equations::
- @end menu
- X
- @node Root Finding, Minimization, Numerical Solutions, Numerical Solutions
- @subsection Root Finding
- X
- @noindent
- @kindex a R
- @pindex calc-find-root
- @tindex root
- @cindex Newton's method
- @cindex Roots of equations
- @cindex Numerical root-finding
- The @kbd{a R} (@code{calc-find-root}) [@code{root}] command finds a
- numerical solution (or @dfn{root}) of an equation. (This command treats
- inequalities the same as equations. If the input is any other kind
- of formula, it is interpreted as an equation of the form @cite{X = 0}.)
- X
- The @kbd{a R} command requires an initial guess on the top of the
- stack, and a formula in the second-to-top position. It prompts for a
- solution variable, which must appear in the formula. All other variables
- that appear in the formula must have assigned values, i.e., when
- a value is assigned to the solution variable and the formula is
- evaluated with @kbd{=}, it should evaluate to a number. Any assigned
- value for the solution variable itself is ignored and unaffected by
- this command.
- X
- When the command completes, the initial guess is replaced on the stack
- by a vector of two numbers: The value of the solution variable that
- solves the equation, and the difference between the lefthand and
- righthand sides of the equation at that value. Ordinarily, the second
- number will be zero or very nearly zero. (Note that Calc uses a
- slightly higher precision while finding the root, and thus the second
- number may be slightly different from the value you would compute from
- the equation yourself.)
- X
- The @kbd{v h} (@code{calc-head}) command is a handy way to extract
- the first element of the result vector, discarding the error term.
- X
- The initial guess can be a real number, in which case Calc searches
- for a real solution near that number, or a complex number, in which
- case Calc searches the whole complex plane near that number for a
- solution, or it can be an interval form which restricts the search
- to real numbers inside that interval.
- X
- Calc tries to use @kbd{a d} to take the derivative of the equation.
- If this succeeds, it uses Newton's method. If the equation is not
- differentiable Calc uses a bisection method. (If Newton's method
- appears to be going astray, Calc switches over to bisection if it
- can, or otherwise gives up. In this case it may help to try again
- with a slightly different initial guess.) If the initial guess is a
- complex number, the function must be differentiable.
- X
- If the formula (or the difference between the sides of an equation)
- is negative at one end of the interval you specify and positive at
- the other end, the root finder is guaranteed to find a root.
- Otherwise, Calc subdivides the interval into small parts looking for
- positive and negative values to bracket the root. When your guess is
- an interval, Calc will not look outside that interval for a root.
- X
- @kindex H a R
- @tindex wroot
- The @kbd{H a R} [@code{wroot}] command is similar to @kbd{a R}, except
- that if the initial guess is an interval for which the function has
- the same sign at both ends, then rather than subdividing the interval
- Calc attempts to widen it to enclose a root. Use this mode if
- you are not sure if the function has a root in your interval.
- X
- If the function is not differentiable, and you give a simple number
- instead of an interval as your initial guess, Calc uses this widening
- process even if you did not type the Hyperbolic flag. (If the function
- @emph{is} differentiable, Calc uses Newton's method which does not
- require a bounding interval in order to work.)
- X
- If Calc leaves the @code{root} or @code{wroot} function in symbolic
- form on the stack, it will normally display an explanation for why
- no root was found. If you miss this explanation, press @kbd{w}
- (@code{calc-why}) to get it back.
- X
- @node Minimization, Numerical Systems of Equations, Root Finding, Numerical Solutions
- @subsection Minimization
- X
- @noindent
- @kindex a N
- @kindex H a N
- @kindex a X
- @kindex H a X
- @pindex calc-find-minimum
- @pindex calc-find-maximum
- @tindex minimize
- @tindex maximize
- @cindex Minimization, numerical
- The @kbd{a N} (@code{calc-find-minimum}) [@code{minimize}] command
- finds a minimum value for a formula. It is very similar in operation
- to @kbd{a R} (@code{calc-find-root}): You give the formula and an initial
- guess on the stack, and are prompted for the name of a variable. The guess
- may be either a number near the desired minimum, or an interval enclosing
- the desired minimum. The function returns a vector containing the
- value of the the variable which minimizes the formula's value, along
- with the minimum value itself.
- X
- Note that this command looks for a @emph{local} minimum. Many functions
- have more than one minimum; some, like @c{$x \sin x$}
- @cite{x sin(x)}, have infinitely
- many. In fact, there is no easy way to define the ``global'' minimum
- of @c{$x \sin x$}
- @cite{x sin(x)} but Calc can still locate any particular local minimum
- for you. Calc basically goes downhill from the initial guess until it
- finds a point at which the function's value is greater both to the left
- and to the right. Calc does not use derivatives when minimizing a function.
- X
- If your initial guess is an interval and it looks like the minimum
- occurs at one or the other endpoint of the interval, Calc will return
- that endpoint only if that endpoint is closed; thus, minimizing @cite{17 x}
- over @cite{[2..3]} will return @cite{[2, 38]}, but minimizing over
- @cite{(2..3]} would report no minimum found.
- X
- Most functions are smooth and flat near their minimum values. Because
- of this flatness, if the current precision is, say, 12 digits, the
- variable can only be determined meaningfully to about six digits. Thus
- you should set the precision to twice as many digits as you need in your
- answer.
- X
- @tindex wminimize
- @tindex wmaximize
- The @kbd{H a N} [@code{wminimize}] command, analogously to @kbd{H a R},
- expands the guess interval to enclose a minimum rather than requiring
- that the minimum lie inside the interval you supply.
- X
- The @kbd{a X} (@code{calc-find-maximum}) [@code{maximize}] and
- @kbd{H a X} [@code{wmaximize}] commands effectively minimize the
- negative of the formula you supply.
- X
- The formula must evaluate to a real number at all points inside the
- interval (or near the initial guess if the guess is a number). If
- the initial guess is a complex number the variable will be minimized
- over the complex numbers; if it is real or an interval it will
- be minimized over the reals.
- X
- @node Numerical Systems of Equations, , Minimization, Numerical Solutions
- @subsection Systems of Equations
- X
- @noindent
- @cindex Systems of equations, numerical
- The @kbd{a R} command can also solve systems of equations. In this
- case, the equation should instead be a vector of equations, the
- guess should instead be a vector of numbers (intervals are not
- supported), and the variable should be a vector of variables. You
- can omit the brackets while entering the list of variables. Each
- equation must be differentiable by each variable for this mode to
- work. The result will be a vector of two vectors: The variable
- values that solved the system of equations, and the differences
- between the sides of the equations with those variable values.
- There must be the same number of equations as variables. Since
- only plain numbers are allowed as guesses, the Hyperbolic flag has
- no effect when solving a system of equations.
- X
- It is also possible to minimize over many variables with @kbd{a N}
- (or maximize with @kbd{a X}). Once again the variable name should
- be replaced by a vector of variables, and the initial guess should
- be an equal-sized vector of initial guesses. But, unlike the case of
- multidimensional root finding, the formula being minimized should
- still be a single formula, @emph{not} a vector. Beware that
- multidimensional minimization is currently @emph{very} slow.
- X
- @node Curve Fitting, Summations, Numerical Solutions, Algebra
- @section Curve Fitting
- X
- @noindent
- The @kbd{a F} command fits a set of data to a @dfn{model formula},
- such as @cite{y = m x + b} where @cite{m} and @cite{b} are parameters
- to be determined. For a typical set of measured data there will be
- no single @cite{m} and @cite{b} that exactly fit the data; in this
- case, Calc chooses values of the parameters that provide the closest
- possible fit.
- X
- @menu
- * Linear Fits::
- * Polynomial and Multilinear Fits::
- * Error Estimates for Fits::
- * Standard Nonlinear Models::
- * Curve Fitting Details::
- * Interpolation::
- @end menu
- X
- @node Linear Fits, Polynomial and Multilinear Fits, Curve Fitting, Curve Fitting
- @subsection Linear Fits
- X
- @noindent
- @kindex a F
- @pindex calc-curve-fit
- @tindex fit
- @cindex Linear regression
- @cindex Least-squares fits
- The @kbd{a F} (@code{calc-curve-fit}) [@code{fit}] command attempts
- to fit a set of data (@cite{x} and @cite{y} vectors of numbers) to a
- straight line, polynomial, or other function of @cite{x}. For the
- moment we will consider only the case of fitting to a line, and we
- will ignore the issue of whether or not the model was in fact a good
- fit for the data.
- X
- In a standard linear least-squares fit, we have a set of @cite{(x,y)}
- data points that we wish to fit to the @dfn{model} @cite{y = m x + b}
- by adjusting the parameters @cite{m} and @cite{b} to make the calculated
- @cite{y} values from the formula as close as possible to the actual
- @cite{y} values from the data set. (In a polynomial fit, the model is
- instead, say, @cite{y = a x^3 + b x^2 + c x + d}. In a multilinear fit,
- we have data points of the form @cite{(x1,x2,x3,y)} and our model is
- @cite{y = a x1 + b x2 + c x3 + d}. These will be discussed later.)
- X
- In the model formula, variables like @cite{x} and @cite{x2} are called
- the @dfn{independent variables}, and @cite{y} is the @dfn{dependent
- variable}. Variables like @cite{m}, @cite{a}, and @cite{b} are called
- the @dfn{parameters} of the model.
- X
- The @kbd{a F} command takes the data set to be fitted from the stack.
- By default, it expects the data in the form of a matrix. For example,
- for a linear or polynomial fit, this would be a @c{$2\times N$}
- @asis{2xN} matrix where
- the first row is a list of @cite{x} values and the second row has the
- corresponding @cite{y} values. For the multilinear fit shown above,
- the matrix would have four rows (@cite{x1}, @cite{x2}, @cite{x3}, and
- @cite{y}, respectively).
- X
- If you happen to have an @c{$N\times2$}
- @asis{Nx2} matrix instead of a @c{$2\times N$}
- @asis{2xN} matrix,
- just press @kbd{v t} first to transpose the matrix.
- X
- After you type @kbd{a F}, Calc prompts you to select a model. For a
- linear fit, press the digit @kbd{1}.
- X
- Calc then prompts for you to name the variables. By default it chooses
- high letters like @cite{x} and @cite{y} for independent variables and
- low letters like @cite{a} and @cite{b} for parameters. (The dependent
- variable doesn't need a name.) The two kinds of variables are separated
- by a semicolon. Since you generally care more about the names of the
- independent variables than of the parameters, Calc also allows you to
- name only them and let the parameters use default names.
- X
- For example, suppose the data matrix
- X
- @ifinfo
- @group
- @example
- [ [ 1, 2, 3, 4, 5 ]
- X [ 5, 7, 9, 11, 13 ] ]
- @end example
- @end group
- @end ifinfo
- @tex
- \turnoffactive
- \turnoffactive
- $$ \pmatrix{ 1 & 2 & 3 & 4 & 5 \cr
- X 5 & 7 & 9 & 11 & 13 }
- $$
- @end tex
- X
- @noindent
- is on the stack and we wish to do a simple linear fit. Type
- @kbd{a F}, then @kbd{1} for the model, then @kbd{RET} to use
- the default names. The result will be the formula @cite{3 + 2 x}
- on the stack. Calc has created the model expression @kbd{a + b x},
- then found the optimal values of @cite{a} and @cite{b} to fit the
- data. (In this case, it was able to find an exact fit.) Calc then
- substituted those values for @cite{a} and @cite{b} in the model.
- X
- The @kbd{a F} command puts two entries in the trail. One is, as
- always, a copy of the result that went to the stack; the other is
- a vector of the actual parameter values, written as equations:
- @cite{[a = 3, b = 2]}, in case you'd rather read them from a
- vector than pick them out of the formula. (You can type @kbd{t y}
- to move this vector to the stack; @pxref{Trail Commands}.)
- X
- Specifying a different independent variable name will affect the
- resulting formula: @kbd{a F 1 k RET} produces @kbd{3 + 2 k}.
- Changing the parameter names (@kbd{a F 1 k;b,m RET}) will affect
- the equations that go into the trail.
- X
- @tex
- \bigskip
- @end tex
- X
- To see what happens when the fit is not exact, we could change
- the number 13 in the data matrix to 14 and try the fit again.
- The result is:
- X
- @example
- 2.6 + 2.2 x
- @end example
- X
- Evaluating this formula, say with @kbd{v x 5 RET TAB V M $ RET}, shows
- a reasonably close match to the y-values in the data.
- X
- @example
- [4.8, 7., 9.2, 11.4, 13.6]
- @end example
- X
- Since there is no line which passes through all the @i{N} data points,
- Calc has chosen a line that best approximates the data points using
- the method of least squares. The idea is to define the @dfn{chi-square}
- error measure
- X
- @ifinfo
- @example
- chi^2 = sum((y_i - (a + b x_i))^2, i, 1, N)
- @end example
- @end ifinfo
- @tex
- \turnoffactive
- $$ \chi^2 = \sum_{i=1}^N (y_i - (a + b x_i))^2 $$
- @end tex
- X
- @noindent
- which is clearly zero if @cite{a + b x} exactly fits all data points,
- and increases as various @cite{a + b x_i} values fail to match the
- corresponding @cite{y_i} values. There are several reasons why the
- summand is squared, one of them being to ensure that @c{$\chi^2 \ge 0$}
- @cite{chi^2 >= 0}.
- Least-squares fitting simply chooses the values of @cite{a} and @cite{b}
- for which the error @c{$\chi^2$}
- @cite{chi^2} is as small as possible.
- X
- Other kinds of models do the same thing but with a different model
- formula in place of @cite{a + b x_i}.
- X
- @tex
- \bigskip
- @end tex
- X
- A numeric prefix argument causes the @kbd{a F} command to take the
- data in some other form than one big matrix. A positive argument @i{N}
- will take @i{N} items from the stack, corresponding to the @i{N} rows
- of a data matrix. In the linear case, @i{N} must be 2 since there
- is always one independent variable and one dependent variable.
- X
- A prefix of zero or plain @kbd{C-u} is a compromise; Calc takes two
- items from the stack, an @i{N}-row matrix of @cite{x} values, and a
- vector of @cite{y} values. If there is only one independent variable,
- the @cite{x} values can be either a one-row matrix or a plain vector,
- in which case the @kbd{C-u} prefix is the same as a @kbd{C-u 2} prefix.
- SHAR_EOF
- true || echo 'restore of calc.texinfo failed'
- fi
- echo 'End of part 46'
- echo 'File calc.texinfo is continued in part 47'
- echo 47 > _shar_seq_.tmp
- exit 0
- exit 0 # Just in case...
- --
- Kent Landfield INTERNET: kent@sparky.IMD.Sterling.COM
- Sterling Software, IMD UUCP: uunet!sparky!kent
- Phone: (402) 291-8300 FAX: (402) 291-4362
- Please send comp.sources.misc-related mail to kent@uunet.uu.net.
-