Essentially I want to plot a function with singularities like

$\frac{1}{(x-0.5)^2+(y-0.5)^2}\cdot \frac{1}{(x+0.5)^2+(y+0.5)^2}$ 

at a higher sampling rate.

I tried to avoid the singularities by capping with some value ($20$):

\addplot3[surf, colormap/jet, samples=50, domain=-1:1] {min(1/((x-0.5)^2+(y-0.5)^2)* 1/((x+0.5)^2+(y+0.5)^2),20)};

Which yields


But that really does not stop TikZ/pfgplots from having to evaluate the function near its singularities, so it gets quite slow at compiling. So here is my question:

Is there a better/faster way to plot functions that contain singularities? Or do i have to precompute the data and give it to TikZ/pfgplots manually?

  • 1
    I suspect that the speed has nothing to do with the actual value of the function. You can plot a identically zero function at a resolution of 1000 by 1000 and it will take decades.
    – Symbol 1
    Mar 27, 2017 at 20:26
  • 1
    Use LuaLaTeX in conjunction with \pgfplotsset{compat=newest}. This will evaluate the function in Lua which is much faster. Mar 27, 2017 at 22:05
  • I tried LuaLaTeX and it was MUCH faster. Then I realized that it wasn't doing anything except showing the same old PDF. BTW, where does [range3frame] come from? Mar 28, 2017 at 3:31
  • I dont think that the singularity is a problem. You can check with 1/max(((x-0.5)^2+(y-0.5)^2)*((x+0.5)^2+(y+0.5)^2),.05) which has no singularity. The problem is that LaTeX is slow and you ask for samples=50.
    – Kpym
    Mar 28, 2017 at 17:27
  • @Kpym Is a sampling rate of 50 really that hard? LuaLaTeX is a great option, at least LaTeX wont have to compute the plot every single time. Thanks! The range3frame is a leftover from another plot, it doesn't do anything. Mar 28, 2017 at 17:51


Browse other questions tagged or ask your own question.