home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume24 / gnucalc / part46 < prev    next >
Encoding:
Text File  |  1991-11-01  |  55.5 KB  |  1,209 lines

  1. Newsgroups: comp.sources.misc
  2. From: daveg@synaptics.com (David Gillespie)
  3. Subject:  v24i094:  gnucalc - GNU Emacs Calculator, v2.00, Part46/56
  4. Message-ID: <1991Nov1.183840.21155@sparky.imd.sterling.com>
  5. X-Md4-Signature: 978bcd10a1385fa395f7fac5ddec7ca2
  6. Date: Fri, 1 Nov 1991 18:38:40 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: daveg@synaptics.com (David Gillespie)
  10. Posting-number: Volume 24, Issue 94
  11. Archive-name: gnucalc/part46
  12. Environment: Emacs
  13. Supersedes: gmcalc: Volume 13, Issue 27-45
  14.  
  15. ---- Cut Here and unpack ----
  16. #!/bin/sh
  17. # do not concatenate these parts, unpack them in order with /bin/sh
  18. # file calc.texinfo continued
  19. #
  20. if test ! -r _shar_seq_.tmp; then
  21.     echo 'Please unpack part 1 first!'
  22.     exit 1
  23. fi
  24. (read Scheck
  25.  if test "$Scheck" != 46; then
  26.     echo Please unpack part "$Scheck" next!
  27.     exit 1
  28.  else
  29.     exit 0
  30.  fi
  31. ) < _shar_seq_.tmp || exit 1
  32. if test ! -f _shar_wnt_.tmp; then
  33.     echo 'x - still skipping calc.texinfo'
  34. else
  35. echo 'x - continuing file calc.texinfo'
  36. sed 's/^X//' << 'SHAR_EOF' >> 'calc.texinfo' &&
  37. the function names @code{thecoefs} and @code{thefactors} are used only
  38. as placeholders; there are no actual Calc functions by those names.)
  39. X
  40. @kindex H a f
  41. @tindex factors
  42. The @kbd{H a f} [@code{factors}] command also factors a polynomial,
  43. but it returns a list of factors instead of an expression which is the
  44. product of the factors.  Each factor is represented by a sub-vector
  45. of the factor, and the power with which it appears.  For example,
  46. @cite{x^5 + x^4 - 33 x^3 + 63 x^2} factors to @cite{(x + 7) x^2 (x - 3)^2}
  47. in @kbd{a f}, or to @cite{[ [x, 2], [x+7, 1], [x-3, 2] ]} in @kbd{H a f}.
  48. If there is an overall numeric factor, it always comes first in the list.
  49. The functions @code{factor} and @code{factors} allow a second argument
  50. when written in algebraic form; @samp{factor(x,v)} factors @cite{x} with
  51. respect to the specific variable @cite{v}.  The default is to factor with
  52. respect to all the variables that appear in @cite{x}.
  53. X
  54. @kindex a c
  55. @pindex calc-collect
  56. @tindex collect
  57. The @kbd{a c} (@code{calc-collect}) [@code{collect}] command rearranges a
  58. formula as a
  59. polynomial in a given variable, ordered in decreasing powers of that
  60. variable.  For example, given @cite{1 + 2 x + 3 y + 4 x y^2} on
  61. the stack, @kbd{a c x} would produce @cite{(2 + 4 y^2) x + (1 + 3 y)},
  62. and @kbd{a c y} would produce @cite{(4 x) y^2 + 3 y + (1 + 2 x)}.
  63. The polynomial will be expanded out using the distributive law as
  64. necessary:  Collecting @cite{x} in @cite{(x - 1)^3} produces
  65. @cite{x^3 - 3 x^2 + 3 x - 1}.  Terms not involving @cite{x} will
  66. not be expanded.
  67. X
  68. The ``variable'' you specify at the prompt can actually be any
  69. expression: @kbd{a c ln(x+1)} will collect together all terms multiplied
  70. by @samp{ln(x+1)} or integer powers thereof.  If @samp{x} also appears
  71. in the formula in a context other than @samp{ln(x+1)}, @kbd{a c} will
  72. treat those occurrences as unrelated to @samp{ln(x+1)}, i.e., as constants.
  73. X
  74. @kindex a x
  75. @pindex calc-expand
  76. @tindex expand
  77. The @kbd{a x} (@code{calc-expand}) [@code{expand}] command expands an
  78. expression by applying the distributive law everywhere.  It applies to
  79. products, quotients, and powers involving sums.  By default, it fully
  80. distributes all parts of the expression.  With a numeric prefix argument,
  81. the distributive law is applied only the specified number of times, then
  82. the partially expanded expression is left on the stack.
  83. X
  84. The @kbd{a x} and @kbd{j D} commands are somewhat redundant.  Use
  85. @kbd{a x} if you want to expand all products of sums in your formula.
  86. Use @kbd{j D} if you want to expand a particular specified term of
  87. the formula.  There is an exactly analogous correspondence between
  88. @kbd{a f} and @kbd{j M}.  (The @kbd{j D} and @kbd{j M} commands
  89. also know many other kinds of expansions, such as
  90. @samp{exp(a + b) = exp(a) exp(b)}.)
  91. X
  92. Calc's automatic simplifications will sometimes reverse a partial
  93. expansion.  For example, the first step in expanding @cite{(x+1)^3} is
  94. to write @cite{(x+1) (x+1)^2}.  If @kbd{a x} stops there and tries
  95. to put this formula onto the stack, though, Calc will automatically
  96. simplify it back to @cite{(x+1)^3} form.  The solution is to turn
  97. simplification off first (@pxref{Simplification Modes}), or to run
  98. @kbd{a x} without a numeric prefix argument so that it expands all
  99. the way in one step.
  100. X
  101. @kindex a a
  102. @pindex calc-apart
  103. @tindex apart
  104. The @kbd{a a} (@code{calc-apart}) [@code{apart}] command expands a
  105. rational function by partial fractions.  A rational function is the
  106. quotient of two polynomials; @code{apart} pulls this apart into a
  107. sum of rational functions with simple denominators.  In algebraic
  108. notation, the @code{apart} function allows a second argument that
  109. specifies which variable to use as the ``base''; by default, Calc
  110. chooses the base variable automatically.
  111. X
  112. @kindex a n
  113. @pindex calc-normalize-rat
  114. @tindex nrat
  115. The @kbd{a n} (@code{calc-normalize-rat}) [@code{nrat}] command
  116. attempts to arrange a formula into a quotient of two polynomials.
  117. For example, given @cite{1 + (a + b/c) / d}, the result would be
  118. @cite{(b + a c + c d) / c d}.  The quotient is reduced, so that
  119. @kbd{a n} will simplify @cite{(x^2 + 2x + 1) / (x^2 - 1)} by dividing
  120. out the common factor @cite{x + 1}, yielding @cite{(x + 1) / (x - 1)}.
  121. X
  122. @kindex a \
  123. @pindex calc-poly-div
  124. @tindex pdiv
  125. The @kbd{a \} (@code{calc-poly-div}) [@code{pdiv}] command divides
  126. two polynomials @cite{u} and @cite{v}, yielding a new polynomial
  127. @cite{q}.  If several variables occur in the inputs, the inputs are
  128. considered multivariate polynomials.  (Calc divides by the variable
  129. with the largest power in @cite{u} first, or, in the case of equal
  130. powers, chooses the variables in alphabetical order.)  For example,
  131. dividing @cite{x^2 + 3 x + 2} by @cite{x + 2} yields @cite{x + 1}.
  132. The remainder from the division, if any, is reported at the bottom
  133. of the screen and is also placed in the Trail along with the quotient.
  134. X
  135. Using @code{pdiv} in algebraic notation, you can specify the particular
  136. variable to be used as the base:  `@t{pdiv(}@i{a}@t{,}@i{b}@t{,}@i{x}@t{)}'.
  137. If @code{pdiv} is given only two arguments (as is always the case with
  138. the @kbd{a \} command), then it does a multivariate division as outlined
  139. above.
  140. X
  141. @kindex a %
  142. @pindex calc-poly-rem
  143. @tindex prem
  144. The @kbd{a %} (@code{calc-poly-rem}) [@code{prem}] command divides
  145. two polynomials and keeps the remainder @cite{r}.  The quotient
  146. @cite{q} is discarded.  For any formulas @cite{a} and @cite{b}, the
  147. results of @kbd{a \} and @kbd{a %} satisfy @cite{a = q b + r}.
  148. (This is analogous to plain @kbd{\} and @kbd{%}, which compute the
  149. integer quotient and remainder from dividing two numbers.)
  150. X
  151. @kindex a /
  152. @kindex H a /
  153. @pindex calc-poly-div-rem
  154. @tindex pdivrem
  155. @tindex pdivide
  156. The @kbd{a /} (@code{calc-poly-div-rem}) [@code{pdivrem}] command
  157. divides two polynomials and reports both the quotient and the
  158. remainder as a vector @cite{[q, r]}.  The @kbd{H a /} [@code{pdivide}]
  159. command divides two polynomials and constructs the formula
  160. @cite{q + r/b} on the stack.  (Naturally if the remainder is zero,
  161. this will immediately simplify to @cite{q}.)
  162. X
  163. @kindex a g
  164. @pindex calc-poly-gcd
  165. @tindex pgcd
  166. The @kbd{a g} (@code{calc-poly-gcd}) [@code{pgcd}] command computes
  167. the greatest common divisor of two polynomials.  (The GCD actually
  168. is unique only to within a constant multiplier; Calc attempts to
  169. choose a GCD which will be unsurprising.)  For example, the @kbd{a n}
  170. command uses @kbd{a g} to take the GCD of the numerator and denominator
  171. of a quotient, then divides each by the result using @kbd{a \}.  (The
  172. definition of GCD ensures that this division can take place without
  173. leaving a remainder.)
  174. X
  175. While the polynomials used in operations like @kbd{a /} and @kbd{a g}
  176. often have integer coefficients, this is not required.  Calc can also
  177. deal with polynomials over the rationals or floating-point reals.
  178. Polynomials with modulo-form coefficients are also useful in many
  179. applications; if you enter @samp{(x^2 + 3 x - 1) mod 5}, Calc
  180. automatically transforms this into a polynomial over the field of
  181. integers mod 5:  @samp{(1 mod 5) x^2 + (3 mod 5) x + (4 mod 5)}.
  182. X
  183. Congratulations and thanks go to Ove Ewerlid
  184. (@code{ewerlid@@mizar.DoCS.UU.SE}), who contributed many of the
  185. polynomial routines used in the above commands.
  186. X
  187. @xref{Decomposing Polynomials}, for several useful functions for
  188. extracting the individual coefficients of a polynomial.
  189. X
  190. @node Calculus, Solving Equations, Polynomials, Algebra
  191. @section Calculus
  192. X
  193. @noindent
  194. The following calculus commands do not automatically simplify their
  195. inputs or outputs using @code{calc-simplify}.  You may find it helps
  196. to do this by hand by typing @kbd{a s} or @kbd{a e}.  It may also help
  197. to use @kbd{a x} and/or @kbd{a c} to arrange a result in the most
  198. readable way.
  199. X
  200. @menu
  201. * Differentiation::
  202. * Integration::
  203. * Customizing the Integrator::
  204. * Numerical Integration::
  205. * Taylor Series::
  206. @end menu
  207. X
  208. @node Differentiation, Integration, Calculus, Calculus
  209. @subsection Differentiation
  210. X
  211. @kindex a d
  212. @kindex H a d
  213. @pindex calc-derivative
  214. @tindex deriv
  215. @tindex tderiv
  216. @noindent
  217. The @kbd{a d} (@code{calc-derivative}) [@code{deriv}] command computes
  218. the derivative of the expression on the top of the stack with respect to
  219. some variable, which it will prompt you to enter.  Normally, variables
  220. in the formula other than the specified differentiation variable are
  221. considered constant, i.e., @samp{deriv(y,x)} is reduced to zero.  With
  222. the Hyperbolic flag, the @code{tderiv} (total derivative) operation is used
  223. instead, in which derivatives of variables are not reduced to zero
  224. unless those variables are known to be ``constant,'' i.e., independent
  225. of any other variables.  (The built-in special variables like @code{pi}
  226. are considered constant, as are variables that have been declared
  227. @code{const}; @pxref{Declarations}.)
  228. X
  229. With a numeric prefix argument @var{n}, this computes the @var{n}th
  230. derivative.
  231. X
  232. When working with trigonometric functions, it is best to switch to
  233. radians mode first (with @kbd{m r}).  The derivative of @samp{sin(x)}
  234. in degrees is @samp{(pi/180) cos(x)}, probably not the expected
  235. answer!
  236. X
  237. If you use the @code{deriv} function directly in an algebraic formula,
  238. you can write @samp{deriv(f,x,x0)} which represents the derivative
  239. of @cite{f} with respect to @cite{x}, evaluated at the point @cite{x=x0}.
  240. X
  241. If the formula being differentiated contains functions which Calc does
  242. not know, the derivatives of those functions are produced by adding
  243. primes (apostrophe characters).  For example, @samp{deriv(f(2x), x)}
  244. produces @samp{2 f'(2 x)}, where the function @code{f'} represents the
  245. derivative of @code{f}.
  246. X
  247. For functions you have defined with the @kbd{Z F} command, Calc expands
  248. the functions according to their defining formulas unless you have
  249. also defined @code{f'} suitably.  For example, suppose we define
  250. @samp{sinc(x) = sin(x)/x} using @kbd{Z F}.  If we then differentiate
  251. the formula @samp{sinc(2 x)}, the formula will be expanded to
  252. @samp{sin(2 x) / (2 x)} and differentiated.  However, if we also
  253. define @samp{sinc'(x) = dsinc(x)}, say, then Calc will write the
  254. result as @samp{2 dsinc(x)}.  @xref{Algebraic Definitions}.
  255. X
  256. For multi-argument functions @samp{f(x,y,z)}, the derivative with respect
  257. to the first argument is written @samp{f'(x,y,z)}; derivatives with
  258. respect to the other arguments are @samp{f'2(x,y,z)} and @samp{f'3(x,y,z)}.
  259. Various higher-order derivatives can be formed in the obvious way, e.g.,
  260. @samp{f'@var{}'(x)} (the second derivative of @code{f}) or
  261. @samp{f'@var{}'2'3(x,y,z)} (@code{f} differentiated with respect to each
  262. argument once).@refill
  263. X
  264. @node Integration, Customizing the Integrator, Differentiation, Calculus
  265. @subsection Integration
  266. X
  267. @kindex a i
  268. @pindex calc-integral
  269. @tindex integ
  270. @noindent
  271. The @kbd{a i} (@code{calc-integral}) [@code{integ}] command computes the
  272. indefinite integral of the expression on the top of the stack with
  273. respect to a variable.  The integrator is not guaranteed to work for
  274. all integrable functions, but it is able to integrate several large
  275. classes of formulas.  In particular, any polynomial or rational function
  276. (a polynomial divided by a polynomial) is acceptable.  (Rational functions
  277. don't have to be in explicit quotient form, however; @c{$x/(1+x^{-2}$}
  278. @cite{x/(1+x^-2)}
  279. is not strictly a quotient of polynomials, but it is equivalent to
  280. @cite{x^3/(x^2+1)}, which is.)  Also, square roots of terms involving
  281. @cite{x} and @cite{x^2} may appear in rational functions being
  282. integrated.  Finally, rational functions involving trigonometric or
  283. hyperbolic functions can be integrated.
  284. X
  285. @ifinfo
  286. If you use the @code{integ} function directly in an algebraic formula,
  287. you can also write @samp{integ(f,x,v)} which expresses the resulting
  288. indefinite integral in terms of variable @code{v} instead of @code{x}.
  289. With four arguments, @samp{integ(f(x),x,a,b)} represents a definite
  290. integral from @code{a} to @code{b}.
  291. @end ifinfo
  292. @tex  
  293. If you use the @code{integ} function directly in an algebraic formula,
  294. you can also write @samp{integ(f,x,v)} which expresses the resulting
  295. indefinite integral in terms of variable @code{v} instead of @code{x}.
  296. With four arguments, @samp{integ(f(x),x,a,b)} represents a definite
  297. integral $\int_a^b f(x) \, dx$.
  298. @end tex
  299. X
  300. Please note that the current implementation of Calc's integrator sometimes
  301. produces results that are significantly more complex than they need to
  302. be.  For example, the integral Calc finds for @c{$1/(x+\sqrt{x^2+1})$}
  303. @cite{1/(x+sqrt(x^2+1))}
  304. is several times more complicated than the answer Mathematica
  305. returns for the same input, although the two forms are numerically
  306. equivalent.  Also, any indefinite integral should be considered to have
  307. an arbitrary constant of integration added to it, although Calc does not
  308. write an explicit constant of integration in its result.  For example,
  309. Calc's solution for @c{$1/(1+\tan x)$}
  310. @cite{1/(1+tan(x))} differs from the solution given
  311. in the @emph{CRC Math Tables} by a constant factor of @c{$\pi i / 2$}
  312. @cite{pi i / 2},
  313. due to a different choice of constant of integration.
  314. X
  315. The Calculator remembers all the integrals it has done.  If conditions
  316. change in a way that would invalidate the old integrals, they will be
  317. thrown out.  If you suspect this is not happening when it should,
  318. use the @code{calc-flush-caches} command; @pxref{Caches}.
  319. X
  320. @vindex IntegLimit
  321. Calc normally will pursue integration by substitution or integration by
  322. parts up to 3 nested times before abandoning an approach as fruitless.
  323. If the integrator is taking too long, you can lower this limit by storing
  324. a number (like 2) in the variable @code{IntegLimit}.  (The @kbd{s I}
  325. command is a convenient way to edit @code{IntegLimit}.)  If this variable
  326. has no stored value or does not contain a nonnegative integer, a limit
  327. of 3 is used.  The lower this limit is, the greater the chance that Calc
  328. will be unable to integrate a function it could otherwise handle.  Raising
  329. this limit allows the Calculator to solve more integrals, though the time
  330. it takes may grow exponentially.  You can monitor the integrator's actions
  331. by creating an Emacs buffer called @code{*Trace*}.  If such a buffer
  332. exists, the @kbd{a i} command will write a log of its actions there.
  333. X
  334. If you want to manipulate integrals in a purely symbolic way, you can
  335. set the integration nesting limit to 0 to prevent all but fast
  336. table-lookup solutions of integrals.  You might then wish to define
  337. rewrite rules for integration by parts, various kinds of substitutions,
  338. and so on.  @xref{Rewrite Rules}.
  339. X
  340. @node Customizing the Integrator, Numerical Integration, Integration, Calculus
  341. @subsection Customizing the Integrator
  342. X
  343. @vindex IntegRules
  344. @noindent
  345. Calc has two built-in rewrite rules called @code{IntegRules} and
  346. @code{IntegAfterRules} which you can edit to define new integration
  347. methods.  @xref{Rewrite Rules}.  At each step of the integration process,
  348. Calc wraps the current integrand in a call to the fictitious function
  349. @samp{integtry(@var{expr},@var{var})}, where @var{expr} is the
  350. integrand and @var{var} is the integration variable.  If your rules
  351. rewrite this to be a plain formula (not a call to @code{integtry}, then
  352. Calc will use this formula as the integral of @var{expr}.  For example,
  353. the rule @samp{integtry(mysin(x),x) := -mycos(x)} would define a rule to
  354. integrate a function @code{mysin} that acts like the sine function.
  355. Then, putting @samp{4 mysin(2y+1)} on the stack and typing @kbd{a i y}
  356. will produce the integral @samp{-2 mycos(2y+1)}.  Note that Calc has
  357. automatically made various transformations on the integral to allow it
  358. to use your rule; integral tables generally give rules for
  359. @samp{mysin(a x + b)}, but you don't need to use this generality in
  360. your @code{IntegRules}.
  361. X
  362. Your rule may do further integration by calling @code{integ}.  For
  363. example, @samp{integtry(twice(u),x) := twice(integ(u))} allows Calc
  364. to integrate @samp{twice(sin(x))} to get @samp{twice(-cos(x))}.
  365. Note that @code{integ} was called with only one argument.  This notation
  366. is allowed only within @code{IntegRules}; it means ``integrate this
  367. with respect to the same integration variable.''  If Calc is unable
  368. to integrate @code{u}, the integration that invoked @code{IntegRules}
  369. also fails.  Thus integrating @samp{twice(f(x))} fails, returning the
  370. unevaluated integral @samp{integ(twice(f(x)), x)}.  It is still legal
  371. to call @code{integ} with two or more arguments, however; in this case,
  372. if @code{u} is not integrable, @code{twice} itself will still be
  373. integrated:  If the above rule is changed to @samp{... := twice(integ(u,x))},
  374. then integrating @samp{twice(f(x))} will yield @samp{twice(integ(f(x),x))}.
  375. X
  376. If a rule instead produces the formula @samp{integsubst(@var{sexpr},
  377. @var{svar})}, either replacing the top-level @code{integtry} call or
  378. nested anywhere inside the expression, then Calc will apply the
  379. substitution @samp{@var{u} = @var{sexpr}(@var{svar})} to try to
  380. integrate the original @var{expr}.  For example, the rule
  381. @samp{sqrt(a) := integsubst(sqrt(x),x)} says that if Calc ever finds
  382. a square root in the integrand, it should attempt the substitution
  383. @samp{u = sqrt(x)}.  (This particular rule is unnecessary because
  384. Calc always tries ``obvious'' substitutions where @var{sexpr} actually
  385. appears in the integrand.)  The variable @var{svar} may be the same
  386. as the @var{var} that appeared in the call to @code{integtry}, but
  387. it need not be.
  388. X
  389. When integrating according to an @code{integsubst}, Calc uses the
  390. equation solver to find the inverse of @var{sexpr} (if the integrand
  391. refers to @var{var} anywhere except in subexpressions that exactly
  392. match @var{sexpr}).  It uses the differentiator to find the derivative
  393. of @var{sexpr} and/or its inverse (it has two methods that use one
  394. derivative or the other).  You can also specify these items by adding
  395. extra arguments to the @code{integsubst} your rules construct; the
  396. general form is @samp{integsubst(@var{sexpr}, @var{svar}, @var{sinv},
  397. @var{sprime})}, where @var{sinv} is the inverse of @var{sexpr} (still
  398. written as a function of @var{svar}), and @var{sprime} is the
  399. derivative of @var{sexpr} with respect to @var{svar}.  If you don't
  400. specify these things, and Calc is not able to work them out on its
  401. own with the information it knows, then your substitution rule will
  402. work only in very specific, simple cases.
  403. X
  404. Calc applies @code{IntegRules} as if by @kbd{C-u 1 a r IntegRules};
  405. in other words, Calc stops rewriting as soon as any rule in your rule
  406. set succeeds.  (If it weren't for this, the @samp{integsubst(sqrt(x),x)}
  407. example above would keep on adding layers of @code{integsubst} calls
  408. forever!)
  409. X
  410. @vindex IntegSimpRules
  411. Another set of rules, stored in @code{IntegSimpRules}, are applied
  412. every time the integrator uses @kbd{a s} to simplify an intermediate
  413. result.  For example, putting the rule @samp{twice(x) := 2 x} into
  414. @code{IntegSimpRules} would tell Calc to convert the @code{twice}
  415. function into a form it knows whenever integration is attempted.
  416. X
  417. One more way to influence the integrator is to define a function with
  418. the @kbd{Z F} command (@pxref{Algebraic Definitions}).  Calc's
  419. integrator automatically expands such functions according to their
  420. defining formulas, even if you originally asked for the function to
  421. be left unevaluated for symbolic arguments.  (Certain other Calc
  422. systems, such as the differentiator and the equation solver, also
  423. do this.)
  424. X
  425. @vindex IntegAfterRules
  426. Sometimes Calc is able to find a solution to your integral, but it
  427. expresses the result in a way that is unnecessarily complicated.  If
  428. this happens, you can either use @code{integsubst} hints as described
  429. above to try to hint at a more direct path to the desired result, or
  430. you can use @code{IntegAfterRules}.  This is an extra rule set that
  431. runs after the main integrator returns its result; basically, Calc does
  432. an @kbd{a r IntegAfterRules} on the result before showing it to you.
  433. (It also does an @kbd{a s}, without @code{IntegSimpRules}, after that
  434. to further simplify the result.)  For example, Calc's integrator
  435. sometimes produces expressions of the form @samp{ln(1+x) - ln(1-x)};
  436. the default @code{IntegAfterRules} rewrite this into the more readable
  437. form @samp{2 arctanh(x)}.  Note that, unlike @code{IntegRules},
  438. @code{IntegSimpRules} and @code{IntegAfterRules} are applied any number
  439. of times until no further changes are possible.  Rewriting by
  440. @code{IntegAfterRules} occurs only after the main integrator has
  441. finished, not at every step as for @code{IntegRules} and
  442. @code{IntegSimpRules}.
  443. X
  444. @node Numerical Integration, Taylor Series, Customizing the Integrator, Calculus
  445. @subsection Numerical Integration
  446. X
  447. @kindex a I
  448. @pindex calc-num-integral
  449. @tindex ninteg
  450. @noindent
  451. If you want a purely numerical answer to an integration problem, you can
  452. use the @kbd{a I} (@code{calc-num-integral}) [@code{ninteg}] command.  This
  453. command prompts for an integration variable, a lower limit, and an
  454. upper limit.  Except for the integration variable, all other variables
  455. that appear in the integrand formula must have stored values.  (A stored
  456. value, if any, for the integration variable itself is ignored.)
  457. X
  458. Numerical integration works by evaluating your formula at many points in
  459. the specified interval.  Calc uses an ``open Romberg'' method; this means
  460. that it does not evaluate the formula actually at the endpoints (so that
  461. it is safe to integrate @samp{sin(x)/x} from zero, for example).  Also,
  462. the Romberg method works especially well when the function being
  463. integrated is fairly smooth.  If the function is not smooth, Calc will
  464. have to evaluate it at quite a few points before it can accurately
  465. determine the value of the integral.
  466. X
  467. Integration is much faster when the current precision is small.  It is
  468. best to set the precision to the smallest acceptable number of digits
  469. before you use @kbd{a I}.  If Calc appears to be taking too long, press
  470. @kbd{C-g} to halt it and try a lower precision.  If Calc still appears
  471. to need hundreds of evaluations, check to make sure your function is
  472. well-behaved in the specified interval.
  473. X
  474. It is possible for the lower integration limit to be @samp{-inf} (minus
  475. infinity).  Likewise, the upper limit may be plus infinity.  Calc
  476. internally transforms the integral into an equivalent one with finite
  477. limits.
  478. X
  479. @node Taylor Series, , Numerical Integration, Calculus
  480. @subsection Taylor Series
  481. X
  482. @kindex a t
  483. @pindex calc-taylor
  484. @tindex taylor
  485. @noindent
  486. The @kbd{a t} (@code{calc-taylor}) [@code{taylor}] command computes a
  487. power series expansion or Taylor series of a function.  You specify the
  488. variable and the desired number of terms.  You may give an expression of
  489. the form @samp{@var{var} = @var{a}} or @samp{@var{var} - @var{a}} instead
  490. of just a variable to produce a Taylor expansion about the point @var{a}.
  491. You may specify the number of terms with a numeric prefix argument;
  492. otherwise the command will prompt you for the number of terms.  Note that
  493. many series expansions have coefficients of zero for some terms, so you
  494. may appear to get fewer terms than you asked for.@refill
  495. X
  496. If the @kbd{a i} command is unable to find a symbolic integral for a
  497. function, you can get an approximation by integrating the function's
  498. Taylor series.
  499. X
  500. @node Solving Equations, Numerical Solutions, Calculus, Algebra
  501. @section Solving Equations
  502. X
  503. @noindent
  504. @kindex a S
  505. @pindex calc-solve-for
  506. @tindex solve
  507. @cindex Equations, solving
  508. @cindex Solving equations
  509. The @kbd{a S} (@code{calc-solve-for}) [@code{solve}] command rearranges
  510. an equation to solve for a specific variable.  An equation is an
  511. expression of the form @cite{L = R}.  For example, the command @kbd{a S x}
  512. will rearrange @cite{y = 3x + 6} to the form, @cite{x = y/3 - 2}.  If the
  513. input is not an equation, it is treated like an equation of the
  514. form @cite{X = 0}.
  515. X
  516. This command also works for inequalities, as in @cite{y < 3x + 6}.
  517. Some inequalities cannot be solved where the analogous equation could
  518. be; for example, solving @cite{a < b c} for @cite{b} is impossible
  519. without knowing the sign of @cite{c}.  In this case, @kbd{a S} will
  520. produce the result @c{$b \mathbin{\hbox{\code{!=}}} a/c$}
  521. @cite{b != a/c} (using the not-equal-to operator)
  522. to signify that the direction of the inequality was unknown.  The
  523. inequality @c{$a \le b \, c$}
  524. @cite{a <= b c} is not even partially solved.
  525. @xref{Declarations}, for a way to tell Calc that the signs of the
  526. variables in a formula are in fact known.
  527. X
  528. Two useful commands for working with the result of @kbd{a S} are
  529. @kbd{a .} (@pxref{Logical Operations}), which converts @samp{x = y/3 - 2}
  530. to @samp{y/3 - 2}, and @kbd{s l} (@pxref{Let Command}) which evaluates
  531. a formula with @samp{x} set equal to @samp{y/3 - 2}.
  532. X
  533. @menu 
  534. * Multiple Solutions::
  535. * Solving Systems of Equations::
  536. * Decomposing Polynomials::
  537. @end menu
  538. X
  539. @node Multiple Solutions, Solving Systems of Equations, Solving Equations, Solving Equations
  540. @subsection Multiple Solutions
  541. X
  542. @noindent
  543. @kindex H a S
  544. @tindex fsolve
  545. Some equations have more than one solution.  The Hyperbolic flag
  546. (@code{H a S}) [@code{fsolve}] tells the solver to report the fully
  547. general family of solutions.  It will invent variables @code{n1},
  548. @code{n2}, @dots{}, which represent independent arbitrary integers, and
  549. @code{s1}, @code{s2}, @dots{}, which represent independent arbitrary
  550. signs (either @i{+1} or @i{-1}).  If you don't use the Hyperbolic
  551. flag, Calc will use zero in place of all arbitrary integers, and plus
  552. one in place of all arbitrary signs.  Note that variables like @code{n1}
  553. and @code{s1} are not given any special interpretation in Calc except by
  554. @code{calc-solve-for} itself.  As usual, you can use the @kbd{s l}
  555. (@code{calc-let}) command to obtain solutions for various actual values
  556. of these variables.
  557. X
  558. For example, @kbd{' x^2 = y @key{RET} H a S x @key{RET}} solves to
  559. get @samp{x = s1 sqrt(y)}, indicating that the two solutions to the
  560. equation are @samp{sqrt(y)} and @samp{-sqrt(y)}.  Another way to
  561. think about it is that the square-root operation is really a
  562. two-valued function; since every Calc function must return a
  563. single result, @code{sqrt} chooses to return the positive result.
  564. Then @kbd{H a S} doctors this result using @code{s1} to indicate
  565. the full set of possible values of the mathematical square-root.
  566. X
  567. There is a similar phenomenon going the other direction:  Suppose
  568. we solve @samp{sqrt(y) = x} for @code{y}.  Calc squares both sides
  569. to get @samp{y = x^2}.  This is correct, except that it introduces
  570. some dubious solutions.  Consider solving @samp{sqrt(y) = -3}:
  571. Calc will report @cite{y = 9} as a valid solution, which is true
  572. in the mathematical sense of square-root, but false (there is no
  573. solution) for the actual Calc positive-valued @code{sqrt}.  This
  574. happens for both @kbd{a S} and @kbd{H a S}.
  575. X
  576. @cindex @code{GenCount} variable
  577. @vindex GenCount
  578. @tindex an
  579. @tindex as
  580. If you store a positive integer in the Calc variable @code{GenCount},
  581. then Calc will generate formulas of the form @samp{as(@var{n})} for
  582. arbitrary signs, and @samp{an(@var{n})} for arbitrary integers,
  583. where @var{n} represents succeeding values taken by incrementing
  584. @code{GenCount} by one.  While the normal arbitrary sign and
  585. integer symbols start over at @code{s1} and @code{n1} with each
  586. new Calc command, the @code{GenCount} approach will give each
  587. arbitrary value a name that is unique throughout the entire Calc
  588. session.  Also, the arbitrary values are function calls instead
  589. of variables, which is advantageous in some cases.  For example,
  590. you can make a rewrite rule that recognizes all arbitrary signs
  591. using a pattern like @samp{as(n)}.  The @kbd{s l} command only works
  592. on variables, but you can use the @kbd{a b} (@code{calc-substitute})
  593. command to substitute actual values for function calls like @samp{as(3)}.
  594. X
  595. The @kbd{s G} (@code{calc-edit-GenCount}) command is a convenient
  596. way to create or edit this variable.  Press @kbd{M-# M-#} to finish.
  597. X
  598. If you have not stored a value in @code{GenCount}, or if the value
  599. in that variable is not a positive integer, the regular
  600. @code{s1}/@code{n1} notation is used.
  601. X
  602. @kindex I a S
  603. @kindex H I a S
  604. @tindex finv
  605. @tindex ffinv
  606. With the Inverse flag, @kbd{I a S} [@code{finv}] treats the expression
  607. on top of the stack as a function of the specified variable and solves
  608. to find the inverse function, written in terms of the same variable.
  609. For example, @kbd{I a S x} inverts @cite{2x + 6} to @cite{x/2 - 3}.
  610. You can use both Inverse and Hyperbolic [@code{ffinv}] to obtain a
  611. fully general inverse, as described above.
  612. X
  613. @kindex a P
  614. @pindex calc-poly-roots
  615. @tindex roots
  616. Some equations, specifically polynomials, have a known, finite number
  617. of solutions.  The @kbd{a P} (@code{calc-poly-roots}) [@code{roots}]
  618. command uses @kbd{H a S} to solve an equation in general form, then, for
  619. all arbitrary-sign variables like @code{s1}, and all arbitrary-integer
  620. variables like @code{n1} for which @code{n1} only usefully varies over
  621. a finite range, it expands these variables out to all their possible
  622. values.  The results are collected into a vector, which is returned.
  623. For example, @samp{roots(x^4 = 1, x)} returns the four solutions
  624. @samp{[1, -1, (0, 1), (0, -1)]}.  Generally an @var{n}th degree
  625. polynomial will always have @var{n} roots on the complex plane.
  626. (If you have given a @code{real} declaration for the solution
  627. variable, then only the real-valued solutions, if any, will be
  628. reported; @pxref{Declarations}.)
  629. X
  630. Note that because @kbd{a P} uses @kbd{H a S}, it is able to deliver
  631. symbolic solutions if the polynomial has symbolic coefficients.  Also
  632. note that Calc's solver is not able to get exact symbolic solutions
  633. to all polynomials.  Polynomials containing powers up to @cite{x^4}
  634. can always be solved exactly; polynomials of higher degree sometimes
  635. can be:  @cite{x^6 + x^3 + 1} is converted to @cite{(x^3)^2 + (x^3) + 1},
  636. which can be solved for @cite{x^3} using the quadratic equation, and then
  637. for @cite{x} by taking cube roots.  But in many cases, like
  638. @cite{x^6 + x + 1}, Calc does not know how to rewrite the polynomial
  639. into a form it can solve.  The @kbd{a P} command can still deliver a
  640. list of numerical roots, however, provided that symbolic mode (@kbd{m s})
  641. is not turned on.  (If you work with symbolic mode on, recall that the
  642. @kbd{N} (@code{calc-eval-num}) key is a handy way to reevaluate the
  643. formula on the stack with symbolic mode temporarily off.)  Naturally,
  644. @kbd{a P} can only provide numerical roots if the polynomial coefficents
  645. are all numbers (real or complex).
  646. X
  647. @node Solving Systems of Equations, Decomposing Polynomials, Multiple Solutions, Solving Equations
  648. @subsection Solving Systems of Equations
  649. X
  650. @noindent
  651. @cindex Systems of equations, symbolic
  652. You can also use the commands described above to solve systems of
  653. simultaneous equations.  Just create a vector of equations, then
  654. specify a vector of variables for which to solve.  (You can omit
  655. the surrounding brackets when entering the vector of variables
  656. at the prompt.)
  657. X
  658. For example, putting @samp{[x + y = a, x - y = b]} on the stack
  659. and typing @kbd{a S x,y @key{RET}} produces the vector of solutions
  660. @samp{[x = a - (a-b)/2, y = (a-b)/2]}.  The result vector will
  661. have the same length as the variables vector, and the variables
  662. will be listed in the same order there.  Note that the solutions
  663. are not always simplified as far as possible; the solution for
  664. @cite{x} here could be improved by an application of @kbd{j M}
  665. (@code{calc-sel-merge}) on the @samp{/} sign, followed by
  666. @kbd{M-2 j v} also on the @samp{/} sign (which runs @kbd{a s},
  667. @code{calc-simplify}, to simplify the quotient).
  668. X
  669. Calc's algorithm works by trying to eliminate one variable at a
  670. time by solving one of the equations for that variable and then
  671. substituting into the other equations.  Calc will try all the
  672. possiblities, but you can speed things up by noting that Calc
  673. first tries to eliminate the first variable with the first
  674. equation, then the second variable with the second equation,
  675. and so on.  It also helps to put the simpler (e.g., more linear)
  676. equations toward the front of the list.  Calc's algorithm will
  677. solve any system of linear equations, and also many kinds of
  678. nonlinear systems.
  679. X
  680. @tindex elim
  681. Normally there will be as many variables as equations.  If you
  682. give fewer variables than equations (an ``over-determined'' system
  683. of equations), Calc will find a partial solution.  For example,
  684. typing @kbd{a S y @key{RET}} with the above system of equations
  685. would produce @samp{[y = a - x]}.  There are now several ways to
  686. express this solution in terms of the original variables; Calc uses
  687. the first one that it finds.  You can control the choice by adding
  688. variable specifiers of the form @samp{elim(@var{v})} to the
  689. variables list.  This says that @var{v} should be eliminated from
  690. the equations; the variable will not appear at all in the solution.
  691. For example, typing @kbd{a S y,elim(x)} would yield
  692. @samp{[y = a - (b+a)/2]}.
  693. X
  694. If the variables list contains only @code{elim} specifiers,
  695. Calc simply eliminates those variables from the equations
  696. and then returns the resulting equations.  For example,
  697. @kbd{a S elim(x)} produces @samp{[a - 2 y = b]}.  Every variable
  698. eliminated will reduce the number of equations in the system
  699. by one.
  700. X
  701. Again, @kbd{a S} gives you one solution to the system of
  702. equations.  If there are several solutions, you can use @kbd{H a S}
  703. to get a general family of solutions, or, if there is a finite
  704. number of solutions, you can use @kbd{a P} to get a list.  (In
  705. the latter case, the result will take the form of a matrix where
  706. the rows are different solutions and the columns correspond to the
  707. variables you requested.)
  708. X
  709. Another way to deal with certain kinds of overdetermined systems of
  710. equations is the @kbd{a F} command, which does least-squares fitting
  711. to satisfy the equations.  @xref{Curve Fitting}.
  712. X
  713. @node Decomposing Polynomials, , Solving Systems of Equations, Solving Equations
  714. @subsection Decomposing Polynomials
  715. X
  716. @noindent
  717. @tindex poly
  718. The @code{poly} function takes a polynomial and a variable as
  719. arguments, and returns a vector of polynomial coefficients (constant
  720. coefficient first).  For example, @samp{poly(x^3 + 2 x, x)} returns
  721. @cite{[0, 2, 0, 1]}.  If the input is not a polynomial in @cite{x},
  722. the call to @code{poly} is left in symbolic form.  If the input does
  723. not involve the variable @cite{x}, the input is returned in a list
  724. of length one, representing a polynomial with only a constant
  725. coefficient.  The call @samp{poly(x, x)} returns the vector @cite{[0, 1]}.
  726. The last element of the returned vector is guaranteed to be nonzero;
  727. note that @samp{poly(0, x)} returns the empty vector @cite{[]}.
  728. Note that @cite{x} may actually be any formula; for example,
  729. @samp{poly(sin(x)^2 - sin(x) + 3, sin(x))} returns @cite{[3, -1, 1]}.
  730. X
  731. @cindex Coefficients of polynomial
  732. @cindex Degree of polynomial
  733. To get the @cite{x^k} coefficient of polynomial @cite{p}, use
  734. @samp{poly(p, x)_(k+1)}.  To get the degree of polynomial @cite{p},
  735. use @samp{vlen(poly(p, x)) - 1}.  For example, @samp{poly((x+1)^4, x)}
  736. returns @samp{[1, 4, 6, 4, 1]}, so @samp{poly((x+1)^4, x)_(2+1)}
  737. gives the @cite{x^2} coefficient of this polynomial, 6.
  738. X
  739. @tindex gpoly
  740. One important feature of the solver is its ability to recognize
  741. formulas which are ``essentially'' polynomials.  This ability is
  742. made available to the user through the @code{gpoly} function, which
  743. is used just like @code{poly}:  @samp{gpoly(@var{expr}, @var{var})}.
  744. If @var{expr} is a polynomial in some term which includes @var{var}, then
  745. this function will return a vector @samp{[@var{x}, @var{c}, @var{a}]}
  746. where @var{x} is the term that depends on @var{var}, @var{c} is a
  747. vector of polynomial coefficients (like the one returned by @code{poly}),
  748. and @var{a} is a multiplier which is usually 1.  Basically,
  749. @samp{@var{expr} = @var{a}*(@var{c}_1 + @var{c}_2 @var{x} +
  750. @var{c}_3 @var{x}^2 + ...)}.  The last element of @var{c} is
  751. guaranteed to be non-zero, and @var{c} will not equal @samp{[1]}
  752. (i.e., the trivial decomposition @var{expr} = @var{x} is not
  753. considered a polynomial).  One side effect is that @samp{gpoly(x, x)}
  754. and @samp{gpoly(6, x)}, both of which might be expected to recognize
  755. their arguments as polynomials, will not because the decomposition
  756. is considered trivial.
  757. X
  758. For example, @samp{gpoly((x-2)^2, x)} returns @samp{[x, [4, -4, 1], 1]},
  759. since the expanded form of this polynomial is @cite{4 - 4 x + x^2}.
  760. X
  761. The term @var{x} may itself be a polynomial in @var{var}.  This is
  762. done to reduce the size of the @var{c} vector.  For example,
  763. @samp{gpoly(x^4 + x^2 - 1, x)} returns @samp{[x^2, [-1, 1, 1], 1]},
  764. since a quadratic polynomial in @cite{x^2} is easier to solve than
  765. a quartic polynomial in @cite{x}.
  766. X
  767. A few more examples of the kinds of polynomials @code{gpoly} can
  768. discover:
  769. X
  770. @smallexample
  771. sin(x) - 1               [sin(x), [-1, 1], 1]
  772. x + 1/x - 1              [x, [1, -1, 1], 1/x]
  773. x + 1/x                  [x^2, [1, 1], 1/x]
  774. x^3 + 2 x                [x^2, [2, 1], x]
  775. x + x^2:3 + sqrt(x)      [x^1:6, [1, 1, 0, 1], x^1:2]
  776. x^(2a) + 2 x^a + 5       [x^a, [5, 2, 1], 1]
  777. (exp(-x) + exp(x)) / 2   [e^(2 x), [0.5, 0.5], e^-x]
  778. @end smallexample
  779. X
  780. The @code{poly} and @code{gpoly} functions accept a third integer argument
  781. which specifies the largest degree of polynomial that is acceptable.
  782. If this is @cite{n}, then only @var{c} vectors of length @cite{n+1}
  783. or less will be returned.  Otherwise, the @code{poly} or @code{gpoly}
  784. call will remain in symbolic form.  For example, the equation solver
  785. can handle quartics and smaller polynomials, so it calls
  786. @samp{gpoly(@var{expr}, @var{var}, 4)} to discover whether @var{expr}
  787. can be treated by its linear, quadratic, cubic, or quartic formulas.
  788. X
  789. @tindex pdeg
  790. The @code{pdeg} function computes the degree of a polynomial;
  791. @samp{pdeg(p,x)} is the highest power of @code{x} that appears in
  792. @code{p}.  This is the same as @samp{vlen(poly(p,x))-1}, but is
  793. much more efficient.  If @code{p} is constant with respect to @code{x},
  794. then @samp{pdeg(p,x) = 0}.  If @code{p} is not a polynomial in @code{x}
  795. (e.g., @samp{pdeg(2 cos(x), x)}, the function remains unevaluated.
  796. It is possible to omit the second argument @code{x}, in which case
  797. @samp{pdeg(p)} returns the highest total degree of any term of the
  798. polynomial, counting all variables that appear in @code{p}.  Note
  799. that @code{pdeg(c) = pdeg(c,x) = 0} for any nonzero constant @code{c};
  800. the degree of the constant zero is considered to be @code{-inf}
  801. (minus infinity).
  802. X
  803. @tindex plead
  804. The @code{plead} function finds the leading term of a polynomial.
  805. Thus @samp{plead(p,x)} is equivalent to @samp{poly(p,x)_vlen(poly(p,x))},
  806. though again more efficient.  In particular, @samp{plead((2x+1)^10, x)}
  807. returns 1024 without expanding out the list of coefficients.  The
  808. value of @code{plead(p,x)} will be zero only if @cite{p = 0}.
  809. X
  810. @tindex pcont
  811. The @code{pcont} function finds the @dfn{content} of a polynomial.  This
  812. is the greatest common divisor of all the coefficients of the polynomial.
  813. With two arguments, @code{pcont(p,x)} effectively uses @samp{poly(p,x)}
  814. to get a list of coefficients, then uses @code{pgcd} (the polynomial
  815. GCD function) to combine these into an answer.  For example,
  816. @samp{pcont(4 x y^2 + 6 x^2 y, x)} is @samp{2 y}.  The content is
  817. basically the ``biggest'' polynomial that can be divided into @code{p}
  818. exactly.  The sign of the content is the same as the sign of the leading
  819. coefficient.
  820. X
  821. With only one argument, @samp{pcont(p)} computes the numerical
  822. content of the polynomial, i.e., the @code{gcd} of the numerical
  823. coefficients of all the terms in the formula.  Note that @code{gcd}
  824. is defined on rational numbers as well as integers; it computes
  825. the @code{gcd} of the numerators and the @code{lcm} of the
  826. denominators.  Thus @samp{pcont(4:3 x y^2 + 6 x^2 y)} returns 2:3.
  827. Dividing the polynomial by this number will clear all the
  828. denominators, as well as dividing by any common content in the
  829. numerators.  The numerical content of a polynomial is negative only
  830. if all the coefficients in the polynomial are negative.
  831. X
  832. @tindex pprim
  833. The @code{pprim} function finds the @dfn{primitive part} of a
  834. polynomial, which is simply the polynomial divided (using @code{pdiv}
  835. if necessary) by its content.  If the input polynomial has rational
  836. coefficients, the result will have integer coefficients in simplest
  837. terms.
  838. X
  839. @node Numerical Solutions, Curve Fitting, Solving Equations, Algebra
  840. @section Numerical Solutions
  841. X
  842. @noindent
  843. Not all equations can be solved symbolically.  The commands in this
  844. section use numerical algorithms that can find a solution to a specific
  845. instance of an equation to any desired accuracy.  Note that these
  846. commands are slower than their algebraic cousins; it is a good idea
  847. to try @kbd{a S} before resorting to @kbd{a R}.
  848. X
  849. (@xref{Curve Fitting}, for some other, more specialized, operations
  850. on numerical data.)
  851. X
  852. @menu
  853. * Root Finding::
  854. * Minimization::
  855. * Numerical Systems of Equations::
  856. @end menu
  857. X
  858. @node Root Finding, Minimization, Numerical Solutions, Numerical Solutions
  859. @subsection Root Finding
  860. X
  861. @noindent
  862. @kindex a R
  863. @pindex calc-find-root
  864. @tindex root
  865. @cindex Newton's method
  866. @cindex Roots of equations
  867. @cindex Numerical root-finding
  868. The @kbd{a R} (@code{calc-find-root}) [@code{root}] command finds a
  869. numerical solution (or @dfn{root}) of an equation.  (This command treats
  870. inequalities the same as equations.  If the input is any other kind
  871. of formula, it is interpreted as an equation of the form @cite{X = 0}.)
  872. X
  873. The @kbd{a R} command requires an initial guess on the top of the
  874. stack, and a formula in the second-to-top position.  It prompts for a
  875. solution variable, which must appear in the formula.  All other variables
  876. that appear in the formula must have assigned values, i.e., when
  877. a value is assigned to the solution variable and the formula is
  878. evaluated with @kbd{=}, it should evaluate to a number.  Any assigned
  879. value for the solution variable itself is ignored and unaffected by
  880. this command.
  881. X
  882. When the command completes, the initial guess is replaced on the stack
  883. by a vector of two numbers:  The value of the solution variable that
  884. solves the equation, and the difference between the lefthand and
  885. righthand sides of the equation at that value.  Ordinarily, the second
  886. number will be zero or very nearly zero.  (Note that Calc uses a
  887. slightly higher precision while finding the root, and thus the second
  888. number may be slightly different from the value you would compute from
  889. the equation yourself.)
  890. X
  891. The @kbd{v h} (@code{calc-head}) command is a handy way to extract
  892. the first element of the result vector, discarding the error term.
  893. X
  894. The initial guess can be a real number, in which case Calc searches
  895. for a real solution near that number, or a complex number, in which
  896. case Calc searches the whole complex plane near that number for a
  897. solution, or it can be an interval form which restricts the search
  898. to real numbers inside that interval.
  899. X
  900. Calc tries to use @kbd{a d} to take the derivative of the equation.
  901. If this succeeds, it uses Newton's method.  If the equation is not
  902. differentiable Calc uses a bisection method.  (If Newton's method
  903. appears to be going astray, Calc switches over to bisection if it
  904. can, or otherwise gives up.  In this case it may help to try again
  905. with a slightly different initial guess.)  If the initial guess is a
  906. complex number, the function must be differentiable.
  907. X
  908. If the formula (or the difference between the sides of an equation)
  909. is negative at one end of the interval you specify and positive at
  910. the other end, the root finder is guaranteed to find a root.
  911. Otherwise, Calc subdivides the interval into small parts looking for
  912. positive and negative values to bracket the root.  When your guess is
  913. an interval, Calc will not look outside that interval for a root.
  914. X
  915. @kindex H a R
  916. @tindex wroot
  917. The @kbd{H a R} [@code{wroot}] command is similar to @kbd{a R}, except
  918. that if the initial guess is an interval for which the function has
  919. the same sign at both ends, then rather than subdividing the interval
  920. Calc attempts to widen it to enclose a root.  Use this mode if
  921. you are not sure if the function has a root in your interval.
  922. X
  923. If the function is not differentiable, and you give a simple number
  924. instead of an interval as your initial guess, Calc uses this widening
  925. process even if you did not type the Hyperbolic flag.  (If the function
  926. @emph{is} differentiable, Calc uses Newton's method which does not
  927. require a bounding interval in order to work.)
  928. X
  929. If Calc leaves the @code{root} or @code{wroot} function in symbolic
  930. form on the stack, it will normally display an explanation for why
  931. no root was found.  If you miss this explanation, press @kbd{w}
  932. (@code{calc-why}) to get it back.
  933. X
  934. @node Minimization, Numerical Systems of Equations, Root Finding, Numerical Solutions
  935. @subsection Minimization
  936. X
  937. @noindent
  938. @kindex a N
  939. @kindex H a N
  940. @kindex a X
  941. @kindex H a X
  942. @pindex calc-find-minimum
  943. @pindex calc-find-maximum
  944. @tindex minimize
  945. @tindex maximize
  946. @cindex Minimization, numerical
  947. The @kbd{a N} (@code{calc-find-minimum}) [@code{minimize}] command
  948. finds a minimum value for a formula.  It is very similar in operation
  949. to @kbd{a R} (@code{calc-find-root}):  You give the formula and an initial
  950. guess on the stack, and are prompted for the name of a variable.  The guess
  951. may be either a number near the desired minimum, or an interval enclosing
  952. the desired minimum.  The function returns a vector containing the
  953. value of the the variable which minimizes the formula's value, along
  954. with the minimum value itself.
  955. X
  956. Note that this command looks for a @emph{local} minimum.  Many functions
  957. have more than one minimum; some, like @c{$x \sin x$}
  958. @cite{x sin(x)}, have infinitely
  959. many.  In fact, there is no easy way to define the ``global'' minimum
  960. of @c{$x \sin x$}
  961. @cite{x sin(x)} but Calc can still locate any particular local minimum
  962. for you.  Calc basically goes downhill from the initial guess until it
  963. finds a point at which the function's value is greater both to the left
  964. and to the right.  Calc does not use derivatives when minimizing a function.
  965. X
  966. If your initial guess is an interval and it looks like the minimum
  967. occurs at one or the other endpoint of the interval, Calc will return
  968. that endpoint only if that endpoint is closed; thus, minimizing @cite{17 x}
  969. over @cite{[2..3]} will return @cite{[2, 38]}, but minimizing over
  970. @cite{(2..3]} would report no minimum found.
  971. X
  972. Most functions are smooth and flat near their minimum values.  Because
  973. of this flatness, if the current precision is, say, 12 digits, the
  974. variable can only be determined meaningfully to about six digits.  Thus
  975. you should set the precision to twice as many digits as you need in your
  976. answer.
  977. X
  978. @tindex wminimize
  979. @tindex wmaximize
  980. The @kbd{H a N} [@code{wminimize}] command, analogously to @kbd{H a R},
  981. expands the guess interval to enclose a minimum rather than requiring
  982. that the minimum lie inside the interval you supply.
  983. X
  984. The @kbd{a X} (@code{calc-find-maximum}) [@code{maximize}] and
  985. @kbd{H a X} [@code{wmaximize}] commands effectively minimize the
  986. negative of the formula you supply.
  987. X
  988. The formula must evaluate to a real number at all points inside the
  989. interval (or near the initial guess if the guess is a number).  If
  990. the initial guess is a complex number the variable will be minimized
  991. over the complex numbers; if it is real or an interval it will
  992. be minimized over the reals.
  993. X
  994. @node Numerical Systems of Equations, , Minimization, Numerical Solutions
  995. @subsection Systems of Equations
  996. X
  997. @noindent
  998. @cindex Systems of equations, numerical
  999. The @kbd{a R} command can also solve systems of equations.  In this
  1000. case, the equation should instead be a vector of equations, the
  1001. guess should instead be a vector of numbers (intervals are not
  1002. supported), and the variable should be a vector of variables.  You
  1003. can omit the brackets while entering the list of variables.  Each
  1004. equation must be differentiable by each variable for this mode to
  1005. work.  The result will be a vector of two vectors:  The variable
  1006. values that solved the system of equations, and the differences
  1007. between the sides of the equations with those variable values.
  1008. There must be the same number of equations as variables.  Since
  1009. only plain numbers are allowed as guesses, the Hyperbolic flag has
  1010. no effect when solving a system of equations.
  1011. X
  1012. It is also possible to minimize over many variables with @kbd{a N}
  1013. (or maximize with @kbd{a X}).  Once again the variable name should
  1014. be replaced by a vector of variables, and the initial guess should
  1015. be an equal-sized vector of initial guesses.  But, unlike the case of
  1016. multidimensional root finding, the formula being minimized should
  1017. still be a single formula, @emph{not} a vector.  Beware that
  1018. multidimensional minimization is currently @emph{very} slow.
  1019. X
  1020. @node Curve Fitting, Summations, Numerical Solutions, Algebra
  1021. @section Curve Fitting
  1022. X
  1023. @noindent
  1024. The @kbd{a F} command fits a set of data to a @dfn{model formula},
  1025. such as @cite{y = m x + b} where @cite{m} and @cite{b} are parameters
  1026. to be determined.  For a typical set of measured data there will be
  1027. no single @cite{m} and @cite{b} that exactly fit the data; in this
  1028. case, Calc chooses values of the parameters that provide the closest
  1029. possible fit.
  1030. X
  1031. @menu
  1032. * Linear Fits::
  1033. * Polynomial and Multilinear Fits::
  1034. * Error Estimates for Fits::
  1035. * Standard Nonlinear Models::
  1036. * Curve Fitting Details::
  1037. * Interpolation::
  1038. @end menu
  1039. X
  1040. @node Linear Fits, Polynomial and Multilinear Fits, Curve Fitting, Curve Fitting
  1041. @subsection Linear Fits
  1042. X
  1043. @noindent
  1044. @kindex a F
  1045. @pindex calc-curve-fit
  1046. @tindex fit
  1047. @cindex Linear regression
  1048. @cindex Least-squares fits
  1049. The @kbd{a F} (@code{calc-curve-fit}) [@code{fit}] command attempts
  1050. to fit a set of data (@cite{x} and @cite{y} vectors of numbers) to a
  1051. straight line, polynomial, or other function of @cite{x}.  For the
  1052. moment we will consider only the case of fitting to a line, and we
  1053. will ignore the issue of whether or not the model was in fact a good
  1054. fit for the data.
  1055. X
  1056. In a standard linear least-squares fit, we have a set of @cite{(x,y)}
  1057. data points that we wish to fit to the @dfn{model} @cite{y = m x + b}
  1058. by adjusting the parameters @cite{m} and @cite{b} to make the calculated
  1059. @cite{y} values from the formula as close as possible to the actual
  1060. @cite{y} values from the data set.  (In a polynomial fit, the model is
  1061. instead, say, @cite{y = a x^3 + b x^2 + c x + d}.  In a multilinear fit,
  1062. we have data points of the form @cite{(x1,x2,x3,y)} and our model is
  1063. @cite{y = a x1 + b x2 + c x3 + d}.  These will be discussed later.)
  1064. X
  1065. In the model formula, variables like @cite{x} and @cite{x2} are called
  1066. the @dfn{independent variables}, and @cite{y} is the @dfn{dependent
  1067. variable}.  Variables like @cite{m}, @cite{a}, and @cite{b} are called
  1068. the @dfn{parameters} of the model.
  1069. X
  1070. The @kbd{a F} command takes the data set to be fitted from the stack.
  1071. By default, it expects the data in the form of a matrix.  For example,
  1072. for a linear or polynomial fit, this would be a @c{$2\times N$}
  1073. @asis{2xN} matrix where
  1074. the first row is a list of @cite{x} values and the second row has the
  1075. corresponding @cite{y} values.  For the multilinear fit shown above,
  1076. the matrix would have four rows (@cite{x1}, @cite{x2}, @cite{x3}, and
  1077. @cite{y}, respectively).
  1078. X
  1079. If you happen to have an @c{$N\times2$}
  1080. @asis{Nx2} matrix instead of a @c{$2\times N$}
  1081. @asis{2xN} matrix,
  1082. just press @kbd{v t} first to transpose the matrix.
  1083. X
  1084. After you type @kbd{a F}, Calc prompts you to select a model.  For a
  1085. linear fit, press the digit @kbd{1}.
  1086. X
  1087. Calc then prompts for you to name the variables.  By default it chooses
  1088. high letters like @cite{x} and @cite{y} for independent variables and
  1089. low letters like @cite{a} and @cite{b} for parameters.  (The dependent
  1090. variable doesn't need a name.)  The two kinds of variables are separated
  1091. by a semicolon.  Since you generally care more about the names of the
  1092. independent variables than of the parameters, Calc also allows you to
  1093. name only them and let the parameters use default names.
  1094. X
  1095. For example, suppose the data matrix
  1096. X
  1097. @ifinfo
  1098. @group
  1099. @example
  1100. [ [ 1, 2, 3, 4,  5  ]
  1101. X  [ 5, 7, 9, 11, 13 ] ]
  1102. @end example
  1103. @end group
  1104. @end ifinfo
  1105. @tex
  1106. \turnoffactive
  1107. \turnoffactive
  1108. $$ \pmatrix{ 1 & 2 & 3 & 4  & 5  \cr
  1109. X             5 & 7 & 9 & 11 & 13 }
  1110. $$
  1111. @end tex
  1112. X
  1113. @noindent
  1114. is on the stack and we wish to do a simple linear fit.  Type
  1115. @kbd{a F}, then @kbd{1} for the model, then @kbd{RET} to use
  1116. the default names.  The result will be the formula @cite{3 + 2 x}
  1117. on the stack.  Calc has created the model expression @kbd{a + b x},
  1118. then found the optimal values of @cite{a} and @cite{b} to fit the
  1119. data.  (In this case, it was able to find an exact fit.)  Calc then
  1120. substituted those values for @cite{a} and @cite{b} in the model.
  1121. X
  1122. The @kbd{a F} command puts two entries in the trail.  One is, as
  1123. always, a copy of the result that went to the stack; the other is
  1124. a vector of the actual parameter values, written as equations:
  1125. @cite{[a = 3, b = 2]}, in case you'd rather read them from a
  1126. vector than pick them out of the formula.  (You can type @kbd{t y}
  1127. to move this vector to the stack; @pxref{Trail Commands}.)
  1128. X
  1129. Specifying a different independent variable name will affect the
  1130. resulting formula: @kbd{a F 1 k RET} produces @kbd{3 + 2 k}.
  1131. Changing the parameter names (@kbd{a F 1 k;b,m RET}) will affect
  1132. the equations that go into the trail.
  1133. X
  1134. @tex
  1135. \bigskip
  1136. @end tex
  1137. X
  1138. To see what happens when the fit is not exact, we could change
  1139. the number 13 in the data matrix to 14 and try the fit again.
  1140. The result is:
  1141. X
  1142. @example
  1143. 2.6 + 2.2 x
  1144. @end example
  1145. X
  1146. Evaluating this formula, say with @kbd{v x 5 RET TAB V M $ RET}, shows
  1147. a reasonably close match to the y-values in the data.
  1148. X
  1149. @example
  1150. [4.8, 7., 9.2, 11.4, 13.6]
  1151. @end example
  1152. X
  1153. Since there is no line which passes through all the @i{N} data points,
  1154. Calc has chosen a line that best approximates the data points using
  1155. the method of least squares.  The idea is to define the @dfn{chi-square}
  1156. error measure
  1157. X
  1158. @ifinfo
  1159. @example
  1160. chi^2 = sum((y_i - (a + b x_i))^2, i, 1, N)
  1161. @end example
  1162. @end ifinfo
  1163. @tex
  1164. \turnoffactive
  1165. $$ \chi^2 = \sum_{i=1}^N (y_i - (a + b x_i))^2 $$
  1166. @end tex
  1167. X
  1168. @noindent
  1169. which is clearly zero if @cite{a + b x} exactly fits all data points,
  1170. and increases as various @cite{a + b x_i} values fail to match the
  1171. corresponding @cite{y_i} values.  There are several reasons why the
  1172. summand is squared, one of them being to ensure that @c{$\chi^2 \ge 0$}
  1173. @cite{chi^2 >= 0}.
  1174. Least-squares fitting simply chooses the values of @cite{a} and @cite{b}
  1175. for which the error @c{$\chi^2$}
  1176. @cite{chi^2} is as small as possible.
  1177. X
  1178. Other kinds of models do the same thing but with a different model
  1179. formula in place of @cite{a + b x_i}.
  1180. X
  1181. @tex
  1182. \bigskip
  1183. @end tex
  1184. X
  1185. A numeric prefix argument causes the @kbd{a F} command to take the
  1186. data in some other form than one big matrix.  A positive argument @i{N}
  1187. will take @i{N} items from the stack, corresponding to the @i{N} rows
  1188. of a data matrix.  In the linear case, @i{N} must be 2 since there
  1189. is always one independent variable and one dependent variable.
  1190. X
  1191. A prefix of zero or plain @kbd{C-u} is a compromise; Calc takes two
  1192. items from the stack, an @i{N}-row matrix of @cite{x} values, and a
  1193. vector of @cite{y} values.  If there is only one independent variable,
  1194. the @cite{x} values can be either a one-row matrix or a plain vector,
  1195. in which case the @kbd{C-u} prefix is the same as a @kbd{C-u 2} prefix.
  1196. SHAR_EOF
  1197. true || echo 'restore of calc.texinfo failed'
  1198. fi
  1199. echo 'End of  part 46'
  1200. echo 'File calc.texinfo is continued in part 47'
  1201. echo 47 > _shar_seq_.tmp
  1202. exit 0
  1203. exit 0 # Just in case...
  1204. -- 
  1205. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  1206. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  1207. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  1208. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  1209.