Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mathematica 8: Convert from cartesian to spherical coordinates

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.

like image 291
Mike Volk Avatar asked Jan 19 '26 11:01

Mike Volk


2 Answers

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?

enter image description here

enter image description here

enter image description here

Make sure to use ArcTan[x, y] in Mathematica, which computes the four-quadrant arctangent. For more information see the article about atan2.

like image 107
Andrew Mao Avatar answered Jan 21 '26 03:01

Andrew Mao


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.

like image 30
stuartw Avatar answered Jan 21 '26 05:01

stuartw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!