Mathematica How to plot 3D vector field in spherical coordinates with Mathematica

AI Thread Summary
The discussion focuses on plotting a 3D vector field in spherical coordinates using Mathematica, specifically the vector field represented as -1/r^2 * e_r. Participants clarify the mathematical representation of the vector field and provide conversion formulas from spherical to Cartesian coordinates. They share Mathematica code for both 2D and 3D vector plots, detailing how to express the radial unit vector and the components of the vector field. Additionally, there are inquiries about issues with vector visibility in certain regions of the plot. The conversation emphasizes the importance of accurate code and mathematical representation for effective visualization.
rbwang1225
Messages
112
Reaction score
0
I want to graph this vector field -^r/r^2

but I don't know how to do.

Any help would be appreciated.
 
Physics news on Phys.org
Could you enter your vector field in Latexform (tex) (/tex) (replace the "(" and ")" by square brackets? I can't interpret your equation at the moment, i can only guess, what you mean:
-\frac{1^r}{r^2} \cdot \vec{e_r}
or maybe:
-\frac{r}{r^2} \cdot \vec{e_r} = -\frac{1}{r} \cdot \vec{e_r}?

\vec{e_r} is the unit vector in radial direction. Addtionally if you could give more information, if there is any, about your physical problem this would help!

You can test your equation here (without the need of the (tex)(\tex):
https://www.physicsforums.com/mathjax/test/preview.html
Latex information for PhysicsForum:
https://www.physicsforums.com/showthread.php?t=386951
 
Last edited by a moderator:
What I meant is this \frac{\hat{r}}{r^2}.

Regards
Ren-Bo
 
Ok i assume: \hat{r}=\vec{e_r} is the unit vector in radial direction.

So the following vector field should be evaluated ( i added a minus sign, just remove it for your case):
\vec{V}=-\frac{1}{r^2} \cdot \vec{e_r}

2D Polar Case
What i did to begin with, was reduce your problem to the polar case. The conversion formulas from polar to cartesian coordinate system are (http://en.wikipedia.org/wiki/Polar_coordinate_system):
r=\sqrt{x^2+y^2}
\varphi=\arctan(\frac{y}{x})
and the radial unit vector replaced by cartesian unit vectors is:
\vec{e_r}=\vec{e_x} \cos(\varphi)+\vec{e_y} \sin(\varphi)
All those equations put into your vector field, result in the vector field now being expressed by cartesian coordinates, which you can plot with the following code:
Code:
(* Mathematica Code *)
r[x_, y_] := Sqrt[x^2 + y^2]
\[CurlyPhi][x_, y_] := ArcTan[x, y]
ex[x_, y_] := Cos[\[CurlyPhi][x, y, z]]
ey[x_, y_] := Sin[\[CurlyPhi][x, y, z]]
Vx[x_, y_] := -(1/r[x, y]^2) ex[x, y]
Vy[x_, y_] := -(1/r[x, y]^2) ey[x, y]

VectorPlot[{Vx[x, y], Vy[x, y]}, {x, -1, 1}, {y, -1, 1},
 VectorPoints -> 9,
 RegionFunction -> Function[{x, y, z}, 0.001 < x^2 + y^2 < 2] (*To not evaluate the pole at 0,0 *)
 ]
which results in the plot in attachment 1.
Sidenote: can someone tell me why certain values of the VectorPoints do not display any vectors at all ? Bug or Feature? :)

3D Spherical Case
In a similar way your initial problem is solved. For the conversion from spherical to cartesian see: http://en.wikipedia.org/wiki/Spherical_coordinate_system
The radial unit vector expressed by cartesian unit vectors is this time more complex:
\vec{e_r}=\vec{e_x} \sin(\theta) \cos(\varphi)+ \vec{e_y} \sin{\theta} \sin(\varphi) + \vec{e_z} \cos(\theta)
Code:
r[x_, y_, z_] := Sqrt[x^2 + y^2 + z^2]
\[CurlyPhi][x_, y_] := ArcTan[x, y]
\[Theta][x_, y_, z_] := ArcCos[z/Sqrt[x^2 + y^2 + z^2]]
(* Unit vectors *)
ex[x_, y_, z_] := Sin[\[Theta][x, y, z]] Cos[\[CurlyPhi][x, y, z]]
ey[x_, y_, z_] := Sin[\[Theta][x, y, z]] Sin[\[CurlyPhi][x, y, z]]
ez[x_, y_, z_] := Cos[\[Theta][x, y, z]]
(* Components of the Vector Field *)
Vx[x_, y_, z_] := -(1/r[x, y, z]^2) ex[x, y, z]
Vy[x_, y_, z_] := -(1/r[x, y, z]^2) ey[x, y, z]
Vz[x_, y_, z_] := -(1/r[x, y, z]^2) ez[x, y, z]

VectorPlot3D[{Vx[x, y, z], Vy[x, y, z], Vz[x, y, z]}, {x, -1, 
  1}, {y, -1, 1}, {z, -1, 1},
 VectorPoints -> 5,
 VectorStyle -> "Arrow3D", VectorColorFunction -> Hue,
 AxesLabel -> {"x", "y", "z"}]
For the result see attachment 2.

I would appreciate if someone would crosscheck my results. Since I am not that sure about my 3D case when i look onto the attached picture it seems for the line given by x=0,y=0,z there are no "red vectors" for example which point into the center.

@rbwang1225: Please tell me if that helped, and drop in your solution or mathematica code and or picture here when your finished please!
 

Attachments

  • PolarPlot.png
    PolarPlot.png
    2.2 KB · Views: 1,520
  • 3DPlot.png
    3DPlot.png
    7.2 KB · Views: 1,930
Last edited:
Which version of Mathematica do you use? I failed to use your code in M7 & M8. I don't know why since I think the code is right.

Regards
 
Last edited:
Sorry i broke the code by "cleaning it up", which means i did leave the z-dependence in some equations for \varphi and in the end breaking the code :), here you go, should work with M7.

2D-Case
Code:
(* Mathematica Code *)
r[x_, y_] := Sqrt[x^2 + y^2]
\[CurlyPhi][x_, y_] := ArcTan[x, y]
ex[x_, y_] := Cos[\[CurlyPhi][x, y]]
ey[x_, y_] := Sin[\[CurlyPhi][x, y]]
Vx[x_, y_] := -(1/r[x, y]^2) ex[x, y]
Vy[x_, y_] := -(1/r[x, y]^2) ey[x, y]

VectorPlot[{Vx[x, y], Vy[x, y]}, {x, -1, 1}, {y, -1, 1},
 VectorPoints -> 9,
 RegionFunction -> Function[{x, y}, 0.001 < x^2 + y^2 < 2] (*To not evaluate the pole at 0,0 *)
 ]

3D Spherical Case
Code:
r[x_, y_, z_] := Sqrt[x^2 + y^2 + z^2]
\[CurlyPhi][x_, y_] := ArcTan[x, y]
\[Theta][x_, y_, z_] := ArcCos[z/Sqrt[x^2 + y^2 + z^2]]
(* Unit vectors *)
ex[x_, y_, z_] := Sin[\[Theta][x, y, z]] Cos[\[CurlyPhi][x, y]]
ey[x_, y_, z_] := Sin[\[Theta][x, y, z]] Sin[\[CurlyPhi][x, y]]
ez[x_, y_, z_] := Cos[\[Theta][x, y, z]]
(* Components of the Vector Field *)
Vx[x_, y_, z_] := -(1/r[x, y, z]^2) ex[x, y, z]
Vy[x_, y_, z_] := -(1/r[x, y, z]^2) ey[x, y, z]
Vz[x_, y_, z_] := -(1/r[x, y, z]^2) ez[x, y, z]

VectorPlot3D[{Vx[x, y, z], Vy[x, y, z], Vz[x, y, z]}, {x, -1, 
  1}, {y, -1, 1}, {z, -1, 1},
 VectorPoints -> 5,
 VectorStyle -> "Arrow3D", VectorColorFunction -> Hue,
 AxesLabel -> {"x", "y", "z"}]

If you discover why the red arrows arent plottet for (x=0,y=0,z) please let me know. (or if they work correctly in M8)
 
Last edited:
The following is what I got in M8

Untitled-2.jpeg


Thank you!
 

Similar threads

Replies
4
Views
2K
Replies
2
Views
2K
Replies
3
Views
3K
Replies
3
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Back
Top