2

I want do a contour plot in Mathematica as:

ContourPlot[{Re[F[5/4, x + I y]] == 0, Im[F[5/4, x + I y]] == 0}, {x, -20, 20}, {y, -20, 20}, PlotPoints -> 100];

where function F[b,z] is defined as:

F[b_,z_]:=(2*((b - I*z)/E^(2*Ibz) + E^(2*Ibz)(b + Iz)))/E^b^2 + (Sqrt[Pi]*(1 + 2*z^2)(Erf[b - Iz] + Erf[b + I*z]))/E^z^2;

The plot is not smooth in some area.

Why is that?

Thanks- mike

enter image description here

mike
  • 5,604
  • 1
    Without trying your code (my Mathematica is busy), you likely need to use the MaxRecursion option in addition to PlotPoints. – Szabolcs Sep 08 '14 at 20:34
  • 1
    There is a dedicated Stack Exchange site for Mathematica users at mathematica.stackexchange.com. – heropup Sep 08 '14 at 20:50
  • can someone help me to migrate it to mathematica site? thanks – mike Sep 08 '14 at 21:00
  • 1
    My Mathematica is not busy any more and I tried it. It looks to be a precision issue. I suspect that your F has catastrophic cancellation. Anyway, setting a WorkingPrecision fixes it, though it will be very slow. I used WorkingPrecision -> 20 and PlotPoints -> 100, though you can likely reduce that to WorkingPrecision -> 10 and PlotPoints -> 50. It is not a high precision that you need here to fix it. WorkingPrecision triggers using Mma's built-in arbitrary precision arithmetic, which can do precision tracking. Thanks to precision tracking it knows ... – Szabolcs Sep 08 '14 at 21:29
  • 1
    ... when to automatically boost the number of digits used internally to obtain a (likely) reliable end result of a given precision. Thus WorkingPrecision -> 10 will likely work as well (haven't tried) even though it starts with fewer digits than the default machine precision. http://i.stack.imgur.com/ivOmy.png – Szabolcs Sep 08 '14 at 21:30
  • @Szabolcs. Thanks so much! Could you please paste your comments to an answer? So that I can accept it. – mike Sep 08 '14 at 21:56
  • 1
    @mike: Since this has been answered here, and would most likely be a duplicate on Mathematica, a Mathematica mod has suggested that we leave this here. – robjohn Sep 09 '14 at 06:11
  • @robjohn. Sure. Thanks for the help! – mike Sep 09 '14 at 14:55

1 Answers1

2

This happens due to insufficient precision to compute the values of F in that region. By default Mathematica uses machine precision, i.e. a fixed ~16 digits of decimal precision. This function F is likely prone to catastrophic cancellation.

The fix is to use Mathematica's arbitrary precision arithmetic, which also supports precision tracking. Since Mathematica has an estimate of the precision of the results, it can automatically increase the number of digits it uses internally to achieve a satisfactory result. It can also detect if the result has no significant digits. While it's not 100% reliable, this feature is very useful in practice.

To force the system to use its builtin arbitrary precision arithmetic, simply add the option WorkingPrecision -> 10 (or higher if necessary).

The computation from the screenshot is very slow, instead I recommend something like

PlotPoints -> 40, MaxRecursion -> 3, WorkingPrecision -> 10
Szabolcs
  • 1,117