1
$\begingroup$

I'm tinkering with Mathematica and plotted this function today:

d = 0.000001
Plot[y = (x^2 - 4)/(x - 2), {x, 2 - d, 2 + d}]

This function is undefined when x = 2, so I wanted to plot the area where x is really close to 2.

However the results were weird:

enter image description here

In the place where I would expect to be an empty dot, I saw some wiggly stuff.

I "zoomed in" the function by making the d even smaller:

d = 0.0000001
Plot[y = (x^2 - 4)/(x - 2), {x, 2 - d, 2 + d}]

And got an even more surprising outcome:

enter image description here

How can this be explained? I would expect a straight line and a "hole" in it where x=2.

$\endgroup$
1
$\begingroup$

What you are seeing is the interaction between machine arithmetic imprecision and the way Plot draws curves by drawing lines between a mesh of points it has computed as being on the curve. If the computation is done at low precision, the computed points don't follow the actual curve very well and you get jaggies. You can fix the jaggies by asking for more precision.

Before

With[{k = 7},
  Plot[(x^2 - 4)/(x - 2), {x, 2 - 10^-k, 2 + 10^-k}, 
  PlotRange -> {4 - 10^-k, 4 + 10^-k}]

before

After

With[{k = 7},
  Plot[(x^2 - 4)/(x - 2), {x, 2 - 10^-k, 2 + 10^-k}, 
  PlotRange -> {4 - 10^-k, 4 + 10^-k},
  WorkingPrecision -> 100]]

after

$\endgroup$
2
$\begingroup$
d = 10^-7;
Table[Plot[y = (x^2 - 4)/(x - 2), {x, 2 - d, 2 + d}, 
  WorkingPrecision -> p, PlotLabel -> p], {p, 7, 10, 1}]

fig1

$\endgroup$
  • $\begingroup$ can you please provide more information about these plots? $\endgroup$ – Valentin Sep 24 '18 at 13:25
  • $\begingroup$ @Valentin, depending on WorkingPrecision->p you can see an empty area at p=7, a hole at p=8, what you wanted to see at p=9 or a solid line at p=10. Do you want to know why this is so? $\endgroup$ – Alex Trounev Sep 24 '18 at 13:41

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.