Search code examples
matlabmatlab-figure

spurious lines using fimplicit on a modular function


I want to plot collections of repeating circular arcs and am having trouble with spurious lines showing up in the plots. For example, one of the plots I want is given by

a = @(x,y) ((mod(x,1) + 0.5).^2 + (mod(y,1) - 0.5).^2 - 1)
fimplicit(a,[-1,1],'MeshDensity',500)

but the output is incorrect as far as I can tell:enter image description here

The implicit function is decidedly not zero on the verticle lines. I assume something funny is happening with the fimplicit algorithm and modular arithmetic. Any ideas how to get around this? Thanks!


Solution

  • That probably happens because your function is discontinuous at the lines x = k with k integer, as a surface plot reveals:

    fsurf(a, [-2 2])
    

    To verify that the discontinuity is the likely reason, consider the simpler example

    f = @(x,y) (2*(x>=0)-1).*(2*(y>=0)-1);
    

    This function is discontinuous at x = 0 and at y = 0. It jumps from 1 to −1 at x = 0 and at y = 0, but it never equals 0.

    fsurf(f, [-2 2])
    

    It can be seen that fimplicit is confused by the discontinuity, and thinks the function is 0 there:

    fimplicit(f,[-2,2],'MeshDensity',500)
    

    Looking at the source code of fimplicit, the actual work is seen to be done (on R2017b at least) by the class matlab.graphics.function.ImplicitFunctionLine in the second to last line. That class is a .p file, and is thus obfuscated, which means that unfortunately its source code cannot be seen.