I wrote a code in python to convert my spherical coordinates to cartesian and taking the cross product of the 2 vectors and then returning it back to spherical to get my component values. But the answer I obtained is not correct and I'm wondering where did I go wrong in my math calculation?
If someone has MATLAB and could check my answers, that would be great.
Thanks!
import numpy as np
def spherical_to_cartesian(r,theta,phi):
x = r*np.sin(theta)*np.cos(phi)
y = r*np.sin(theta)*np.sin(phi)
z = r*np.cos(theta)
return(x,y,z)
def cartesian_to_spherical(x,y,z):
r = np.sqrt(x**2 + y**2 + z**2)
theta = np.arccos(z/r)
phi = np.arctan(y/x)
return(r,theta,phi)
A = np.array(spherical_to_cartesian(6,1,2))
Vector A [-2.10105293 4.59088441 3.24181384]
B = np.array(spherical_to_cartesian(4,0,0))
Vector B [0. 0. 4.]
C = np.cross(A,B)
Vector C [18.36353763 8.40421172 -0. ]
D = cartesian_to_spherical(C[0],C[1],C[2])
r: 20.195303635389514, theta: 1.5707963267948966, phi: 0.4292036732051034