2
$\begingroup$

Can anyone explain why Mathematica struggles with the following (equivalent) plots?

Plot[x Abs[Sqrt[0.0002^2 - x^2] - Sqrt[0.0001^2 - x^2]], {x, 1000, 
  10000}]
Plot[x^2 Abs[Sqrt[0.0002^2/x^2 - 1] - Sqrt[0.0001^2/x^2 - 1]], {x, 
  1000, 10000}]

I'm getting very 'random' behaviour for a function which should approach a constant asymptotically. Any ideas how to fix this? Thanks

$\endgroup$
2
$\begingroup$

You're just seeing numerical precision issues when working with machine numbers. Using higher precision will fix this. In order to use higher precision, you need to make sure the function to be plotted is exact, so the following should produce your expected output:

Plot[
    x Abs[Sqrt[(2/10000)^2-x^2] - Sqrt[(1/10000)^2-x^2]],
    {x,1000,10000},
    WorkingPrecision->20
]

enter image description here

and similarly for the other example.

$\endgroup$
0
$\begingroup$

You only use the expression Abs[Sqrt[0.0002^2 - x^2] - Sqrt[0.0001^2 - x^2]] at 1000<x<10000. In this domain, the expression Sqrt[0.0002^2 - x^2] - Sqrt[0.0001^2 - x^2] is purely imaginary, and you can help Mma by rewriting your expression identically:

Abs[Sqrt[0.0002^2 - x^2] - Sqrt[0.0001^2 - x^2]]≡Sqrt[x^2 - 0.0002^2] - Sqrt[x^2 - 0.0001^2]

After that you get the following statement:

Plot[x Sqrt[x^2 - 0.0002^2] - Sqrt[x^2 - 0.0001^2], {x, 1000, 10000}]

yielding

enter image description here

The same takes place with the second plot.

Have fun!

$\endgroup$
  • $\begingroup$ But the x at the front multiplies the whole expression. When I do this with your new plot, I still get the random looking behaviour. $\endgroup$ – Chris Jan 21 at 11:16
  • $\begingroup$ I obtained the plot shown above. If you do not, you may have problems outside of Mma. $\endgroup$ – Alexei Boulbitch Jan 21 at 15:22
  • $\begingroup$ @AlexeiBoulbitch what is this rule / identity $$| \sqrt{c_1-x^2}-\sqrt{c_2-x^2}| =\sqrt{x^2-c_2}-\sqrt{x^2-c_1}$$ that you are claiming? is this a property of absolute? $\endgroup$ – user13892 Jan 21 at 18:16
  • $\begingroup$ If you have any imaginary number, say, x=2*I then its absolute value is Abs[x]=2. If you have a function like y=Sqrt[c-x^2] and you only study it at x>Sqrt[c], then at x>Sqrt[c] you can rewrite y=Sqrt[c-x^2]=I*Sqrt[x^2-c]. Therfore, if you are interested in the absolute value of such a function, you get Abs[Sqrt[c-x^2]]=I*Sqrt[x^2-c] which is only valid in the domain specified above. Anyway, the origin of your problem with the plot was the numerous application of Abs to a complex function during building the plot. $\endgroup$ – Alexei Boulbitch Jan 22 at 8:51
0
$\begingroup$

If you look carefully, the variation is quite small. The plot range magnifies the effect of the round-off error. Compared to the number 1, the value seems fairly stable:

Plot[x Abs[Sqrt[0.0002^2 - x^2] - Sqrt[0.0001^2 - x^2]],
 {x, 1000, 10000}, AxesOrigin -> {0, -1}]

enter image description here

Another way is to rationalize to a more numerically stable expression:

rationalizeNumericallyUnsoundSqrt[expr_] := 
 expr /. Sqrt[q_] - x_ :> Simplify[q - x^2]/(Sqrt[q] + x);

Plot[x^2 Abs[Sqrt[0.0002^2/x^2 - 1] - Sqrt[0.0001^2/x^2 - 1]] // 
   rationalizeNumericallyUnsoundSqrt // Evaluate, {x, 1000, 10000}]

enter image description here

$\endgroup$

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.