# SliceContourPlot3D works perfectly, but ContourPlot3D returns multiple errors

I'm plotting the electric field of a charged ring based a solution from Jackson's Electrodynamics.

Mathematica handles VectorPlot3D and SliceVectorPlot3D for the field without a hitch, and SliceContourPlot3D of the field magnitude as well.

However, attempting to produce a straight-up ContourPlot3D of the field magnitude returns multiple errors, including Power::infy,Infinity::indet, and Power::indet, along with general stop for those errors.

Does anyone have any ideas as to why this might be, and how to work around it to generate a contour plot? This is, by the way, version 10.3.

(I aplogize for the greek letters, they really didn't seem to want to copy cleanly.)

Clear["Global*"]

Φ[s_, z_] := Piecewise[{
{q*Sum[(R^l/(s^2 + z^2)^(0.5*(l + 1)))*LegendreP[l, 1/Sqrt[1 + s^2/z^2]], {l, 0, k}], Sqrt[s^2 + z^2] > R},
{q*Sum[((s^2 + z^2)^(0.5*l)/R^(l + 1))*LegendreP[l, 1/Sqrt[1 + s^2/z^2]], {l, 0, k}], Sqrt[s^2 + z^2] < R}
}]

dΦ = -{D[Φ[s, z], s], 0, D[Φ[s, z], z]};
dΦc = Simplify[TransformedField["Cylindrical" -> "Cartesian", dΦ, {s, ϕ, z} -> {x, y, Q}] /. Q -> z];

k = 5; q = 5; R = 0.2;
cont = SliceContourPlot3D[Norm[dΦc], "CenterPlanes",
{x, -0.35, 0.35}, {y, -0.35, 0.35}, {z, -0.35, 0.35},
ImageSize -> Large, PlotLegends -> Automatic, Contours -> 16,
ColorFunction -> "Rainbow"]
Clear[q, k, R];

k = 5; q = 5; R = 0.2;
ContourPlot3D[Norm[dΦc],
{x, -0.35, 0.35}, {y, -0.35, 0.35}, {z, -0.35, 0.35},
ImageSize -> Large, PlotLegends -> Automatic, Contours -> 16,
ColorFunction -> "Rainbow", RegionFunction -> Function[{x, y, z}, x*y*z > 0]]

Clear[k, q, R]

• I notice when I try to run your code, simply doing the setup, everything up to and including the Simplify part takes a long time. When answering questions people might often just abort the command if it takes a long time to run and move on to another question (I'm guilty of that sometimes). If you define the variables k, q, and R ahead of time, then it takes much less time. Feb 14, 2017 at 18:17
• Thank you for the warning, @JasonB. I'll make sure I think about it next time I post something. Feb 14, 2017 at 21:55

Extended comment here, since it doesn't answer the underlying question of why does this happen, hopefully someone else may have an answer about why you get those errors. This is a workaround I would use.

For something like this (where the computations seem to take forever and I don't know how long it will take to do the underlying adaptive sampling) I always try to use ListContourPlot3D instead. It allows me to see the progress easily, it allows me to decide how long it's going to take.

Check how long a single computation takes,

Norm[dΦc] /. {x -> .2, y -> .2,
z -> .2} // RepeatedTiming
(* {0.0011, 59.3032} *)


Now decide how long it will take to get a 3D grid over your range {-0.35, 0.35} using a step size of 0.02,

36^3 First[%]
(* 52. *)


You can use a finer grid if you want to wait more than 52 seconds.

data = Table[
Norm[dΦc] /. {x -> xx, y -> yy, z -> zz},
{xx, -.35, .35, .02}, {yy, -.35, .35, .02}, {zz, -.35, .35, .02}];~Monitor~{xx, yy, zz}


(using Monitor to check the progress), and then plot it

ListContourPlot3D[data,
DataRange -> {{-.35, .35}, {-.35, .35}, {-.35, .35}},
ImageSize -> Large, PlotLegends -> Automatic, Contours -> 16,
ColorFunction -> "Rainbow",
RegionFunction -> Function[{x, y, z}, x*y*z > 0]]


You might get even better results with a finer grid. Or you can use ListInterpolation and get something like this, but it still gives the same errors (just returns a result quicker).

Using your definitions one can determine that the source of the problem is when x and y are very close to zero.

contourList = Partition[
Flatten[
Table[{x, y, z, Norm[dΦc]}, {x, -0.35, 0.35, 0.01},
{y, -0.35, 0.35, 0.01},
{z, -0.35, 0.35, 0.01}
]
], 4];


Now seek out the elements where the value is Indeterminate.

Cases[contourList, {x___, Indeterminate, y___}]

The indeterminate ones are where x and y are close to zero (i.e., the vertical z axis).

This is because there is a pre-multiplier term that has the value

dΦc[[1, 3]]
(* 1/Sqrt[x^2 + y^2] *)
`