Reducing root precision so Union can work
- To: mathgroup at yoda.physics.unc.edu
- Subject: Reducing root precision so Union can work
- From: wiscombe at climate.gsfc.nasa.gov
- Date: Thu, 1 Oct 92 10:57:21 -0400
Dear MathGroupers, I was trying to simulate a function that I think should be built into Mma, but is not: one that makes a best effort to find ALL the roots of nonlinear equations on a given real interval, or in a given rectangular region of the complex plane (admitting that there will always be pathological examples where all roots are not found). To do this, I used FindRoot in a loop over starting values (fairly densely nested on the interval of interest). Naturally this produces multiple hits on the same root, but I presumed Union could take care of that. Unfortunately, it does not, and it led to some serious questions about just how to reduce the precision of approximate solutions so that Union can do its job. N didn't work, as the following example illustrates: roots = Table[FindRoot[E^x Sin[x]^2-Cos[x]==0, {x,i}], {i,1.2,-6,-1}] {{x -> 0.679143}, {x -> 0.679143}, {x -> -1.31704}, {x -> -1.31704}, {x -> 0.679143}, {x -> -4.72129}, {x -> -4.72129}, {x -> -4.72129}} N[%,3] {{x -> 0.679}, {x -> 0.679}, {x -> -1.32}, {x -> -1.32}, {x -> 0.679}, {x -> -4.72}, {x -> -4.72}, {x -> -4.72}} Union[%] {{x -> -4.72}, {x -> -4.72}, {x -> -1.32}, {x -> -1.32}, {x -> 0.679}, {x -> 0.679}, {x -> 0.679}} Precision[%] 19 My impression, after carefully reading the documentation for N, was that it actually reduced the precision of the numbers on which it acted, rather than just acting like a FORMAT statement in Fortran. Obviously it does not, and therefore the Union fails because there are hidden digits not visible in the output of N[%,3]. If you type the list resulting from N[%,3] directly into Union, Union works fine, of course. I consider this behavior very misleading. I searched the Mma book for other functions which do what I want, but there seem to be none (Round, for example, just gives an Integer result). Of course I could write a function to truncate results to n significant digits, but: (a) this should be built into Mma, and (b) it would be grossly inefficient. Certainly crunching a list down to its unique elements, even if those elements are the result of an approximate solution process causing them to differ in their 10th significant digit, is a common problem in numerical analysis! For reference, I am running Mma 2.0 on a MacIIfx. P.S. To forestall remarks that I should use the FindRoot options AccuracyGoal and WorkingPrecision, those really are just kludges that do not solve the fundamental problem.