0
$\begingroup$

I've got an "Underflow occurred in computation" problem. Here's a simplified version of the code:

toplot = 
  Table[
    Exp[-((x - 1.21*10^15)^4/(2*10^25))],
    {x, 1.20*10^15, 1.22*10^15, 10^12}, {y, 1.20*10^15, 1.22*10^15, 10^12}];

ListDensityPlot[toplot, PlotRange -> All]

and the error is

General::unfl: Underflow occurred in computation. >>
...
General::stop: Further output of General::unfl will be suppressed during this calculation. >>

When I replace the power 4 with the power 2 in the code as follows:

toplot = 
  Table[
    Exp[-((x - 1.21*10^15)^2/(2*10^25))],
    {x, 1.20*10^15, 1.22*10^15, 10^12}, {y, 1.20*10^15, 1.22*10^15, 10^12}];

the code works without any error and I get

ListDensityPlot[toplot, PlotRange -> All]

enter image description here

Unfortunately that's not the function I need :(

I found some other topics about the underflow problem, but I wasn't still able to fix my problem by just reading them.

Any help/suggestions to avoid/bypass the problem are welcome.

$\endgroup$
  • $\begingroup$ I suppose it's about numerical calculation error. $\endgroup$ – Wjx Jul 19 '16 at 11:30
  • $\begingroup$ $MinNumber=6.229688249675322*10^-1355718576299610 It's quite small, that's why I'm surprised that the computation has an underflow, but I'm pretty far from being an expert... $\endgroup$ – Fraccalo Jul 19 '16 at 13:12
  • $\begingroup$ You're getting underflow, but you're still getting a valid plot right? $\endgroup$ – Feyre Jul 19 '16 at 14:23
  • 1
    $\begingroup$ Ponder this Min@Table[-((x - 1.21*10^15)^4/(2*10^25)), {x, 1.20*10^15, 1.22*10^15,10^12}, {y, 1.20*10^15, 1.22*10^15, 10^12}]/Log[10], the exponent base 10 of your smallest number. $\endgroup$ – Michael E2 Jul 19 '16 at 15:08
  • 1
    $\begingroup$ Why couldn't you rescale your units and avoid all this trouble? $\endgroup$ – J. M. will be back soon Jul 19 '16 at 15:12
1
$\begingroup$

The difficulty of doing numerical calculation with the 4th power as opposed to the square is immense. This easily demonstrated by doing a couple of exact computations.

With[{x = (12/10)*10^15}, Exp[-((x - (121/100)*10^15)^2/(2*10^25))]]

1/E^5

With[{x = (12/10)*10^15}, Exp[-((x - (121/100)*10^15)^4/(2*10^25))]]

1/E^500000000000000000000000000

It should now be evident to you why you are getting underflow with the 4th power, but not with the square.

$\endgroup$
  • $\begingroup$ Honestly, the 10^25 and 10^15 just look like a bad choice of units on the OP's part... $\endgroup$ – J. M. will be back soon Jul 19 '16 at 16:08
  • 1
    $\begingroup$ @J.M. Absolutely agree. Scaling is the solution. But that was already covered in the comments. I post this because I want the OP to see the immense numeric effect of the 4th power, and how easy it is to use Mathematica to illuminate the issue. $\endgroup$ – m_goldberg Jul 19 '16 at 16:14
  • $\begingroup$ @m_goldberg thank you for your help! Unfortunately it wasn't easy to scale the problem because that was just a code that I had to add and match to another old program, and scaling all that code would have been too long! Just for the sake of completeness, the correct value for the denominator was ~(2*10^48): by using it the program works perfectly! $\endgroup$ – Fraccalo Jul 19 '16 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.