Tom
Tom

Reputation: 55

Spherical to Cartesian Coordinate Converter

I am simply trying to create a working spherical to Cartesian coordinate converter. However, it is not producing the correct solutions and I have multi-checked that the equations for conversion are correct. Even more odd is that this function works in another script, but not on its own. I am using Spyder. Why is this happening?

import numpy as np

R = 0.02
Phi = 90
Theta = 0

def Sphere2Cart(r,theta,phi):
    xOut = r*np.sin(phi)*np.cos(theta)
    yOut = r*np.sin(phi)*np.sin(theta)
    zOut = r*np.cos(phi)
    return xOut,yOut,zOut

x,y,z = Sphere2Cart(R,Theta,Phi)

The answer it gives me:

Answer the definition provides

The correct answer should be: (x,y,z) = (0.02,0,0)

Upvotes: 1

Views: 383

Answers (1)

MikaelF
MikaelF

Reputation: 3615

It looks like your R, Phi and Theta are in degrees, but numpy uses radians for trigonometric functions.

To convert the values to radians, use np.deg2rad() (or math.radians()):

x,y,z = Sphere2Cart(R, np.deg2rad(Theta), np.deg2rad(Phi))

Upvotes: 2

Related Questions