# StreamPlot in Polar Coordinates

I want to use StreamPlot to map out the field lines of an electric field $\mathbf{E}$ given by $$\mathbf{E} = \frac{3D}{4r^{4}}(3\cos(\theta)^{2}-1)\mathbf{\hat{r}} +\frac{3D}{4r^{4}}\sin(2\theta)\boldsymbol{\hat{\theta}}$$ I could convert it to Cartesian coordinates, but I have quite a few more fields to plot, so I would rather leave it in polar coordinates. How can I get StreamPlot to accept polar coordinates?

-
Just use the function! You will get the stream lines in the $r-\theta$ space! –  Spawn1701D Apr 9 at 22:10
possible duplicate with no answers : mathematica.stackexchange.com/questions/18550/… –  andre Apr 9 at 22:11
Thanks Andre, although the other one was posted first, I've marked it as the duplicate and this one as the canonical version. –  Verbeia Apr 9 at 23:05

One way to do it is to write our own wrapper function which does the conversion and feeds it to StreamPlot. Thereby we have the convenience of a selfcontained function without the hassle of having to do the conversion manually every time. We can convert our field

field =  3/(4r^4) (3Cos[\[Theta]]^2-1) Overscript[r, ^]
+ 3/(4r^4) Sin[2\[Theta]] Overscript[\[Theta], ^]


to cartesian form by preparing a set of conversion rules from polar to cartesian coordinates

tocartesian = {Overscript[r, ^] -> x/r Overscript[x, ^] + y/r Overscript[y, ^],
Overscript[\[Theta], ^] -> -(y/Sqrt[x^2+y^2]) Overscript[x, ^]+x/Sqrt[x^2+y^2] Overscript[y, ^],
r -> Sqrt[x^2+y^2],
\[Theta] -> ArcTan[x,y] };


and a rule to make this into a list afterwards

cartesianlist = (a_ Overscript[x, ^] + b_ Overscript[y, ^]) -> {a, b};


Then we can let Mathematica repeatedly apply (//.) our tocartesian rule to eliminate all occurences of r and then let FullSimplify help us to eliminate the trigonometric functions. At last we use cartesianlist to switch to list form:

cartesianfield = FullSimplify[field //. tocartesian] /. cartesianlist


For convenient usage we define our own PolarStreamPlot function

PolarStreamPlot[{rfield_,thetafield_}, opts___] := Module[
{tocartesian,cartesianlist,field,cartesianfield},
tocartesian={Overscript[r, ^]->x/r Overscript[x, ^]+y/r Overscript[y, ^],
Overscript[\[Theta], ^]->-(y/Sqrt[x^2+y^2])Overscript[x, ^]
+ x/Sqrt[x^2+y^2] Overscript[y, ^],
r->Sqrt[x^2+y^2], \[Theta]->ArcTan[x,y]};
cartesianlist=(a_ Overscript[x, ^] + b_ Overscript[y, ^])->{a,b};
field = rfield Overscript[r, ^] + thetafield Overscript[\[Theta], ^];
cartesianfield = FullSimplify[field//.tocartesian]/.cartesianlist;
StreamPlot[cartesianfield, opts]
]


and now we can feed it our original $r$-$\theta$ field definition directly

PolarStreamPlot[
{3/(4r^4) (3Cos[\[Theta]]^2-1),
3/(4r^4) Sin[2\[Theta]]},
{x, -3, 3}, {y, -3, 3}
]


-
TransformedField["Polar" -> "Cartesian", field, {r, θ} -> {x, y}] will also do the conversion for you, if field = {3/(4 r^4) (3 Cos[θ]^2 - 1), 3/(4 r^4) Sin[2 θ]}. –  Michael E2 Apr 10 at 1:00
@MichaelE2 one up for v9!!!! –  Spawn1701D Apr 10 at 4:42

If you have version 9, the TransformedField as mentioned by MichaelE2 is the way to go. In version 8, the analogous thing (which can also still be used in version 9), is this:

Needs["VectorAnalysis"]
Clear[field, r, θ, ϕ];
m = Transpose[
Transpose[JacobianMatrix[#, Spherical @@ #]]/
ScaleFactors[Spherical @@ #]] &@{r, θ, ϕ};
field[r_, θ_, ϕ_] =
Simplify[m.{3/(4 r^4) (3 Cos[θ]^2 - 1),
3/(4 r^4) Sin[2 θ], 0}];

StreamPlot[
Delete[
field @@ CoordinatesFromCartesian[{x, 0, z}, Spherical], 2],
{x, -3, 3}, {z, -3, 3}]


It uses the VectorAnalysis package like Spawn's answer, but I didn't see any reason why you would first plot the function in polar coordinates, so I inserted the coordinate transformation directly in the StreamPlot. This works for any field defined as a function (field) of the three spherical coordinates, as shown above. All you need is to replace field by field @@ CoordinatesFromCartesian[{x, y, z}, Spherical]. In the plot here, I just set y=0 to get the field lines in the x-z plane.

Edit

The field in the question was given in spherical coordinates, but I copied it from a comment and assumed it was cartesian components. So to correct that, in the definition of field, I added the transformation to the spherical unit vectors. I did this in the most general way I could think of, so that the field can be changed easily. All that you need is the transformation matrix m. If you desire any other coordinate system, just replace Spherical above.

The definition of field also is kept three-dimensional for generality, so that in the StreamPlot I have to Delete one of the three components. Since I'm plotting the x-z plane, I drop the y component which is zero there.

-
The field is given in polar space ($\hat r, \hat\theta$), so first of all you have to change the base of the tangent space. Then you have to consider the transformations involved. For spherical coordinates the change is$$r=\sqrt(x^2+y^2+z^2),\\ \theta=\arccos(z/r),\\ \phi=\arctan(y/x),$$ for cylindrical $$r=\sqrt(x^2+y^2),\\ \theta=\arctan(y/x),\\ z=z$$ To coincide you have to set z=0 and use the first and the third argument. Cylindrical coordinates are better because they are just the cartesian product of polar space with the Real line. –  Spawn1701D Apr 10 at 4:32
What i did was to first create the stream plot in polar space and then transform the whole plot in cartesian ones. It is evident from your plot that something is wrong though ... Nevertheless v9 comes to the rescue!! –  Spawn1701D Apr 10 at 4:34
Oh yes, I overlooked that part. I'll edit my answer to fix it usng the same method I used here. –  Jens Apr 10 at 5:28

Ok the stream plot on the polar space is given by say

With[{D=1},plot=StreamPlot[{3*D/(4r^4)(3Cos[θ]^2-1),3*D/(4r^4) Sin[2θ]},{r,0,1},{θ,0,2*π},
StreamScale -> {Automatic, Automatic, Automatic, Function[{x, y, vx, vy, n}, x]}]]


Now, IF you want this stream plot embedded on cartesian space you can either transform the vector field to cartesian space as you correctly say or do the following:

Needs["VectorAnalysis"]
Show[plot/.
Arrow[v:{__?VectorQ}]:>Arrow[(Most[CoordinatesToCartesian[Append[#, 0], Cylindrical]]&/@v)],
PlotRange -> Automatic]


I suppose your question is if there is a function like PolarPlot for StreamPlot or other plot functions. The short answer is no. You can either change before hand the function or the field or embed the field afterward. Which method is best depends on the specific trasformation involved and if you want to exploit Mathematica's routines in giving you the best presentation of the field.

-
It works (+1), but I had to correct a small syntax error in StreamScale where a } is missing. –  Jens Apr 10 at 5:43
@Jens thanx! I am correcting it now! –  Spawn1701D Apr 10 at 5:51