I just started using Mathematica and came across a problem. I would like to solve more elegantly. I have measurement data in {x,y,z} form and want to transform these into sperical coordinates. I know how to do it using simple functions. But the code gets ugly.
I would like something like:
v={x,y,z}
TranformSpherical[v]
I have looked in the documentation and only found something for version 9, I am using 8 and it did not work when I tried it. Also I have not found a clear solution anywhere else. Hope someone here knows a simple solution to the probem.
The equations are given on Wikipedia and are simple function evaluations. What's stopping you from simply computing them, and how does it get ugly?



Make sure to use ArcTan[x, y] in Mathematica, which computes the four-quadrant arctangent. For more information see the article about atan2.
In version 9
CoordinateTransformData["Cartesian" -> "Spherical", "Mapping", {x, y, z}]
gives you {Sqrt[x^2 + y^2 + z^2], ArcTan[z, Sqrt[x^2 + y^2]], ArcTan[x, y]}
which expresses the three spherical coordinates in terms of {x,y,z}
CoordinateTransform["Cartesian" -> "Spherical", {x, y, z}]
will give you the same thing, but can also be used for conversion. If you have a list {{x0,y0,z0},{x1,y1,z1},...} of Cartesian coordinates, you can apply CoordinateTransform like this
cartesianList = RandomReal[{0, 1}, {4, 3}];
CoordinateTransform["Cartesian" -> "Spherical", #] & /@ cartesianList
In earlier versions
<< Calculus`VectorAnalysis`
SetCoordinates[Spherical]
There is a notebook at the mathworld.wolfram.com site page for Spherical Coordinates. Close to the beginning is an example for what you are doing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With