Subject: ROOTS - find *all* roots (equation problems)
From: jhmeyers@miu.edu (John H Meyers)
Date: 1997/02/10
Newsgroups: comp.sys.hp48

Question from Nir Weiss <november@bgumail.bgu.ac.il>
in article <32F873C1.2781@bgumail.bgu.ac.il>

> 1. when i put's an eqution in the hp it gave me just one answer
>    (like when i wrote x^2-4 i only get the answer 2)
> 2. when i put's an eqution like x^3-2*x+40 i get the wrong answer
> 3. in both case im using in the solver in the eqution solver !!!

In article <Pine.SUN.3.95.970206085430.11466C-100000@geofisica>,
Wladimir Shukowsky <wladimir@iag.usp.br> indicated for each of the
above two equations that there was another initial guess which would
locate the desired root (or other root).

If you graph each function, you will readily see where the roots are;
then you can move the graphics cursor to somewhere near each root,
go into the FCN menu and press ROOT -- the HP48 will then find
the nearest root!

It is also possible to search an interval for all roots of an equation;
the program below is a re-post from 96.12.05

Examples of use of ROOTS program with the equations from above:

  'X^2-4' 'X' -10 10 1 ROOTS --> { -2 2 }

  'X^3-2*X+40' 'X' -10 10 1 ROOTS --> { -3.61468728566 }

Below is a ROOTS program, similar to ROOT, but instead of one or more
guess(es), you supply the start and end points of an interval, and an
increment for evaluating the function at equally spaced points within the
interval; then the program searches the whole interval, looking for roots
(as indicated by sign changes or exact solutions at consecutive incremental
points along the interval):

Stack diagram:

  'function' 'x' start end increment --> { list of roots }

  The "function" can be an expression, equation, or program.
  'x' is the symbol naming the variable being solved for.

%%HP: T(3); @ \-> is right-arrow, etc.
@ ROOTS - search an interval for roots, HP48G/GX
\<< \-> f x s e i \<< { } @ extra list for fixing SEQ bug if no roots
  { y x STO f \->NUM y i + x STO f \->NUM * 0 \<= @ sign change
      { f x y DUP i + 2 \->LIST ROOT } IFT } 'y' s e i + i SEQ \>>
  IF DUP SIZE THEN + SORT DUP 1 \<< DUP2 SAME { DROP } IFT \>> DOLIST
  SWAP DROP END \>> @ sorted unique roots in result list
@ 243.5 bytes (on stack), #2274h checksum

Some roots may not be found if the increment is made too coarse; conversely,
execution time unnecessarily increases if the increment is made too fine.

No program can guarantee to find all roots of any function; for example,
'SIN(1/X)=.5' has an infinite number of roots in the interval -1 to 1,
but the program above can not locate more roots than the number of
segments into which it divides the specified interval.

SEQ is supposed to return a list of whatever results are generated by
executing its program object; the "SEQ bug" mentioned in the comments
above refers to the fact that SEQ does not return any list at all (rather
than an empty list) if no results are generated; this dubious behavior
is common to all of the G/GX new functions DOLIST, DOSUBS, and SEQ.

Happy root hunting season to one and all.

-----------------------------------------------------------
With best wishes from:  John H Meyers  ( jhmeyers@mum.edu )