RegionPlot3D in cylindrical or spherical coordinates?
You can always hide away the coordinate transformations inside a function that calls RegionPlot3D
. Here's a quick & dirty sphericalRegionPlot3D
:
sphericalRegionPlot3D[
ineq_, {r_, rmin_: 0, rmax_: 1}, {th_, thmin_: 0, thmax_}, {ph_,
phmin_, phmax_}, opts___] := RegionPlot3D[With[{
r = Sqrt[x^2 + y^2 + z^2],
th = ArcCos[z/Sqrt[x^2 + y^2 + z^2]],
ph = ArcTan[x, y] + Pi},
ineq && rmin <= r <= rmax && thmin <= th <= thmax &&
phmin <= ph <= phmax],
{x, -rmax, rmax}, {y, -rmax, rmax}, {z, -rmax, rmax},
MeshFunctions -> {Sqrt[#1^2 + #2^2 + #3^2] &, ArcTan[#1, #2] &,
ArcCos[#3/Sqrt[#1^2 + #2^2 + #3^2]] &}, opts];
SetAttributes[sphericalRegionPlot3D, HoldAll];
Example:
sphericalRegionPlot3D[
Mod[ph, Pi/3] < Pi/6, {r, 2, 3}, {th, 1, Pi}, {ph, 0, 2 Pi},
PlotPoints -> 100, Mesh -> {3, 60, 30}]
While not positive, I believe the answer is that RegionPlot
does not support spherical (or other non-Cartesian) coordinates natively.
If correct, I guess the question becomes "What's the easiest way to plot a region defined in terms of spherical coordinates, without resorting to converting the equations by hand?" V9 has commands to ease this process.
Here are a couple of examples that show how you can use the TransformedField
field command to convert your expression and you can use CoordinateTransform
to generate a mesh that reflects the coordinate system that you want.
sphericalExpression = 1 + rho^2 - 2 rho*Cos[theta] Sin[phi];
cartesianExpression =
TransformedField["Spherical" -> "Cartesian",
sphericalExpression, {rho, phi, theta} -> {x, y, z}]
meshFunctions = Function /@ CoordinateTransform[
"Cartesian" -> "Spherical", {#1, #2, #3}]
RegionPlot3D[cartesianExpression < 1, {x, 0, 2}, {y, -1, 1}, {z, -1, 1},
MeshFunctions -> meshFunctions, PlotPoints -> 50, MaxRecursion -> 8,
ViewPoint -> {2, 0, 3}, Boxed -> False,
AxesEdge -> {{-1, -1}, {1, -1}, {1, -1}}]
I've cranked up PlotPoints
and MaxRecursion
since mesh lines are fairly complicated on the other side of the figure.
Show[%, ViewPoint -> {-3, 0, 2}]
That example was chosen to yield a simple expression in Cartesian coordinates. Arbitrary expressions might yield numerical warnings but generally work. If you'd like to constrain your figure to lie within a sphere, you can do so by including a x^2+y^2+z^2<1
in your region specification.
sphericalExpression = Sin[rho] + Cos[theta]*phi;
cartesianExpression =
TransformedField["Spherical" -> "Cartesian",
sphericalExpression, {rho, phi, theta} -> {x, y, z}] // Simplify
RegionPlot3D[
cartesianExpression < 1 && x^2 + y^2 + z^2 < 1, {x, -1, 1}, {y, -1,
1}, {z, -1, 1}, MaxRecursion -> 5, PlotPoints -> 50,
MeshFunctions -> meshFunctions] // Quiet
More generalized version:
newRegionPlot3D[
expr_, {u_, umin_, umax_}, {v_, vmin_, vmax_}, {w_, wmin_, wmax_}, tr_, opts___] :=
Module[{x, y, z, newExpr, xyz},
newExpr = TransformedField[tr -> "Cartesian", expr, {u, v, w} -> {x, y, z}];
xyz = CoordinateTransform[tr -> "Cartesian", {u, v, w}];
{xmax, ymax, zmax} = NMaxValue[{#,
umin < u < umax && vmin < v < vmax && wmin < w < wmax}, {u, v, w}] & /@ xyz;
{xmin, ymin, zmin} = NMinValue[{#, umin < u < umax && vmin < v < vmax && wmin < w < wmax}, {u, v, w}] & /@ xyz;
RegionPlot3D[
newExpr, {x, xmin, xmax}, {y, ymin, ymax}, {z, zmin, zmax}, opts]
]
Some examples:
newRegionPlot3D[r < 1, {r, 0, 1}, {phi, 0, 2 Pi}, {th, 0, Pi}, "Spherical"]
newRegionPlot3D[ro < 1, {ro, 0, 1}, {phi, 0, 2 Pi}, {z, 0, 1}, "Cylindrical"]