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

Click For Summary

Discussion Overview

The discussion revolves around plotting a 3D vector field in spherical coordinates using Mathematica. Participants explore the mathematical representation of the vector field, conversion between coordinate systems, and the implementation of code to visualize the field. The conversation includes technical details and challenges faced in coding.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant seeks assistance in graphing the vector field represented as -^r/r^2.
  • Another participant requests clarification on the vector field's representation in LaTeX format, suggesting possible interpretations of the equation.
  • A participant clarifies that the intended vector field is \frac{\hat{r}}{r^2}.
  • There is a discussion on converting the vector field from polar to Cartesian coordinates, including the relevant equations and Mathematica code for plotting.
  • Participants provide Mathematica code for both 2D and 3D cases, detailing the conversion from spherical to Cartesian coordinates and the components of the vector field.
  • One participant raises a question about the absence of vectors in certain regions of the plot, asking for insights on whether this is a bug or a feature of the software.
  • Another participant mentions issues with running the provided code in different versions of Mathematica, indicating potential compatibility problems.
  • There are corrections made to the code by participants, with one admitting to breaking the code during cleanup and providing a revised version.
  • A participant shares their results from Mathematica version 8, contributing to the ongoing exploration of the topic.

Areas of Agreement / Disagreement

Participants express varying interpretations of the vector field and its representation, leading to multiple competing views on how to properly implement the code. The discussion remains unresolved regarding the absence of certain vectors in the plot and the compatibility of the code across different versions of Mathematica.

Contextual Notes

Limitations include potential misunderstandings of the vector field's representation, dependencies on specific definitions of coordinate systems, and unresolved issues with the Mathematica code related to version differences.

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,551
  • 3DPlot.png
    3DPlot.png
    7.2 KB · Views: 1,955
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 9 ·
Replies
9
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K