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

In summary, the conversation is about graphing a vector field given by the equation -^r/r^2, where the unit vector in radial direction is represented by \vec{e_r}. The conversation covers the 2D and 3D cases, with code provided for both. The 2D case is plotted using Mathematica, while the 3D case is plotted using Mathematica 7. There is a question about the behavior of the vector field at (x=0,y=0,z), which is still being investigated.
  • #1
rbwang1225
118
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
  • #2
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:
[tex]-\frac{1^r}{r^2} \cdot \vec{e_r}[/tex]
or maybe:
[tex]-\frac{r}{r^2} \cdot \vec{e_r} = -\frac{1}{r} \cdot \vec{e_r}[/tex]?

[tex]\vec{e_r}[/tex] 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:
  • #3
What I meant is this [tex]\frac{\hat{r}}{r^2} [/tex].

Regards
Ren-Bo
 
  • #4
Ok i assume: [tex]\hat{r}=\vec{e_r}[/tex] 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):
[tex]\vec{V}=-\frac{1}{r^2} \cdot \vec{e_r}[/tex]

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):
[tex]r=\sqrt{x^2+y^2}[/tex]
[tex]\varphi=\arctan(\frac{y}{x})[/tex]
and the radial unit vector replaced by cartesian unit vectors is:
[tex]\vec{e_r}=\vec{e_x} \cos(\varphi)+\vec{e_y} \sin(\varphi)[/tex]
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:
[tex]\vec{e_r}=\vec{e_x} \sin(\theta) \cos(\varphi)+ \vec{e_y} \sin{\theta} \sin(\varphi) + \vec{e_z} \cos(\theta) [/tex]
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,464
  • 3DPlot.png
    3DPlot.png
    7.2 KB · Views: 1,866
Last edited:
  • #5
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:
  • #6
Sorry i broke the code by "cleaning it up", which means i did leave the z-dependence in some equations for [tex]\varphi[/tex] 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:
  • #7
The following is what I got in M8

Untitled-2.jpeg


Thank you!
 

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

1. How do I define a 3D vector field in spherical coordinates in Mathematica?

To define a 3D vector field in spherical coordinates in Mathematica, you can use the function VectorPlot3D and specify the vector components in terms of the spherical coordinates {r, θ, φ}. For example, if you want to plot the vector field F = {r, θ, φ}, you can use the command VectorPlot3D[{r, θ, φ}, {r, 0, 1}, {θ, 0, π}, {φ, 0, 2π}].

2. How can I customize the appearance of the 3D vector field plot?

You can use various options in the VectorPlot3D function to customize the appearance of the 3D vector field plot. Some of the commonly used options include VectorStyle to change the color and shape of the vectors, VectorScale to adjust the length of the vectors, and VectorPoints to control the density of the vectors in the plot. You can also use PlotRange to specify the range of values for the axes, and AxesLabel to label the axes.

3. How do I convert Cartesian coordinates to spherical coordinates in Mathematica?

You can use the function CoordinateTransform to convert Cartesian coordinates to spherical coordinates in Mathematica. The syntax is CoordinateTransform["Cartesian" -> "Spherical", {x, y, z}], where {x, y, z} are the Cartesian coordinates. This will give you the corresponding spherical coordinates {r, θ, φ}.

4. Can I plot multiple 3D vector fields in the same plot?

Yes, you can plot multiple 3D vector fields in the same plot by using the VectorPlot3D function multiple times and specifying the different vector fields. You can also use the option ShowLegend to add a legend to the plot to distinguish between the different vector fields.

5. How can I create an interactive 3D vector field plot in Mathematica?

You can use the Animate function in Mathematica to create an interactive 3D vector field plot. This will allow you to change the values of certain parameters and see the corresponding changes in the plot. You can also use the Manipulate function to create a similar interactive plot with sliders to control the parameters.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
778
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
280
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
565
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
428
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Introductory Physics Homework Help
Replies
2
Views
389
Back
Top