# DensityPlot showing artifacts [closed]

This is my first post on Mathematica.SE, and I'm still a bit new to Mathematica. I believe I'm probably doing something silly in my code, but I have not been able to find where.

I'm trying to plot a density plot of a function, but the plot is being chopped on the upper-right and lower-left. I'm don't understand why. I already tried most of the options on the DensityPlot webpage, but nothing seems to work.

When I make a PNG file of the plot, there are aditional hyperbola-like curves exactly passing through the region that was chopped off.

The code I'm using is the folowing:

F[r_, ra_] := (1 - Exp[-r^2/ra^2])/(2 π (r^2));
u1[x_, y_, G1_, a_, b_, rc1_] := -(y - b) G1 F[Sqrt[(x - a)^2 + (y - b)^2], rc1];
v1[x_, y_, G1_, a_, b_, rc1_] := (x - a) G1 F[Sqrt[(x - a)^2 + (y - b)^2], rc1];
u2[x_, y_, v0_, y0_, c_] :=  ((y - c)/y0 ) v0;
v2[x_, y_, v0_, y0_, c_] := 0;

u[x_, y_, G1_, a1_, b1_, rc1_, G2_, a2_, b2_, rc2_, c_, v0_, y0_] :=
u1[x, y, G1, a1, b1, rc1] + u1[x, y, G2, a2, b2, rc2] + u2[x, y, v0, y0, c];

v[x_, y_, G1_, a1_, b1_, rc1_, G2_, a2_, b2_, rc2_, c_, v0_, y0_] :=
v1[x, y, G1, a1, b1, rc1] + v1[x, y, G2, a2, b2, rc2] + v2[x, y, v0, y0, c];

gradU[x_, y_, G1_, a1_, b1_, rc1_, G2_, a2_, b2_, rc2_, c_, v0_, y0_] :=
{{Derivative[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0][u]
[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0],
Derivative[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0][u]
[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0]},
{Derivative[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0][v]
[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0],
Derivative[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0][v]
[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0]}};

w[x_, y_, G1_, a1_, b1_, rc1_, G2_, a2_, b2_, rc2_, c_, v0_, y0_] :=
Derivative[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0][v]
[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0] -
Derivative[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0][u]
[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0];

utilde[x_, y_, G1_, a1_, b1_, rc1_, G2_, a2_, b2_, rc2_, c_, v0_, y0_] :=
Derivative[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0][w]
[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0];

vtilde[x_, y_, G1_, a1_, b1_, rc1_, G2_, a2_, b2_, rc2_, c_, v0_, y0_] :=
- Derivative[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0][w]
[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0];

gradU2[x_, y_, G1_, a1_, b1_, rc1_, G2_, a2_, b2_, rc2_, c_, v0_, y0_] :=
{{
Derivative[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0][utilde]
[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0],
Derivative[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0][utilde]
[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0]},
{Derivative[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0][vtilde]
[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0],
Derivative[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0][vtilde]
[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0]}};

wtilde[x_, y_, G1_, a1_, b1_, rc1_, G2_, a2_, b2_, rc2_, c_, v0_, y0_] :=
Derivative[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0][vtilde]
[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0] -
Derivative[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0][utilde]
[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0] ;

var[x_, y_, G1_, a1_, b1_, rc1_, G2_, a2_, b2_, rc2_, c_, v0_, y0_] :=
UnitStep[
w[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0]
wtilde[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0]]
Eigenvalues[gradU2[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0]][[2]];

lambGraph[rc_, X0_, Y0_, G1_, a1_, b1_, rc1_, G2_, a2_, b2_, rc2_, c_, v0_, y0_] :=
Show[
DensityPlot[
Im[var[x - X0, y - Y0, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0]],
{x, -2 rc, 2 rc}, {y, -2 rc, 2 rc},
PlotPoints -> 100,
ColorFunction -> "SunsetColors", PlotLegends -> Automatic ],
StreamPlot[
{u[x - X0, y - Y0, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0],
v[x - X0, y - Y0, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0]},
{x, -2 rc, 2 rc}, {y, -2 rc, 2 rc}]];

graph0 = lambGraph[1, 0, 0, 1, 0, 0, 1, 0, 3, 0, 1, 0, 0.0, 1]

Export["single-Oseen-vc.png", graph0, Background -> None]


### Edit 1

@march, this is the original image

Using PlotRange -> All didn't solve my problem, but it made the lines I mentioned stand out.

I'm not sure how much of the above code is really necessary to produce the problem; probably half of it. I'm trying to make a simpler example, but still haven't managed to reproduce the my issue with simpler code. As soon as I have one, I will post it.

### Edit 2

It seems that the issue is with UnitStep, as Jason B showed, but I still need it to use UnitStep. I know that the chopping I'm seeing is wrong, so it must be Mathematica that is plotting wrong.

Is there an alternative way to write my code that wouldn't cause this kind of problems?

Edit 3:

The problem was wtidle, the correct definition is

wtilde[x_, y_, G1_, a1_, b1_, rc1_, G2_, a2_, b2_, rc2_, c_, v0_, y0_] :=
Derivative[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0][vtilde]
[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0] -
Derivative[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0][utilde]
[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0] ;


The derivatives were wrong on the second term, now everything is alright.

Thanks everybody for the help.

• Okay. How much of this code is really necessary? Can you reproduce your problem with a simpler example? Can you post the picture as well so that we can see what you mean? Did you try PlotRange -> All? – march Jan 3 '16 at 20:31
• your wtilde goes negative so UnitStep is zero. Its not at all clear why you think the plot is wrong. – george2079 Jan 5 '16 at 14:55
• @george2079, I thought that my plot was wrong because I'm still learning to use mathematica and there are some parameters that, if you don't ajust, they can produce all kind of strange results, one example is the PlotPoints, in some cases, if there isn't enough, which is usually the default, it can produce jagged pictures, for example. I thought it was a similar case, but it wasn't – Hydro Guy Jan 6 '16 at 15:59

This has nothing to do withDensityPlot, and you can see this by plotting one dimensional slices of your function:

The cause is the UnitStep in the definition of var. If you remove that, then you get the following

Edit Just to show that Mathematica is correctly plotting the functions you have encoded, let's break up the function into two parts, those inside of and outside of the UnitStep function, using the values you are plotting

outfunc =
FullSimplify@
ComplexExpand@
Im@Evaluate[
Eigenvalues[
gradU2[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0,
y0]][[2]] /.
Inner[Rule, {rc, X0, Y0, G1, a1, b1, rc1, G2, a2, b2, rc2, c,
v0, y0}, {1, 0, 0, 1, 0, 0, 1, 0, 3, 0, 1, 0, 0.0, 1}, List]]
(* (2 E^(-9 - 2 (-3 + x) x -
2 y^2) (E^(4 ((-3 + x)^2 + y^2)) (-1 + 2 x^2 + 2 y^2)^2)^(1/4)
Sin[1/2 Arg[E^(2 ((-3 + x)^2 + y^2)) (-1 + 2 x^2 + 2 y^2)]])/π *)

stepfunc = FullSimplify@Evaluate[
w[x, y, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0] wtilde[x, y,
G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0, y0] /.
Inner[Rule, {rc, X0, Y0, G1, a1, b1, rc1, G2, a2, b2, rc2, c, v0,
y0}, {1, 0, 0, 1, 0, 0, 1, 0, 3, 0, 1, 0, 0.0, 1}, List]]
(* (E^(-2 (x^2 + y^2)) (2 - 4 x (x + y)))/π^2 *)


Then plot them for the two lines above (with the function inside the UnitStep plotted using dashed lines), with the same color code.

For the first case, where y = x, the two functions cross the axis at the exact same point, so when you multiply one function by the UnitStep of the other, there is no discontinuity. In the second plot, where y = 0.5 x, the dashed line crosses the axis before the other line hits the axis. This means there will be a discontinuity in the total function.

If this is unexpected behavior, you must look to the definitions of w, wtilde, and gradU2.

• alright, I removed the UnitStep and indeed it solved the chopping problem, but I need it there for other cases, where there still are problems like this one, and I know this chopping is wrong because I have plot with it in other programs, and calculated it by hand in this case, and it shouldn't affect this region. I'll update my question, thank you – Hydro Guy Jan 4 '16 at 13:25