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
Apart from the fact you should calculate $\phi$ with this variant on the usual arctangent, your problem statement concerns vectors of the form $a\hat{\vec{r}}+b\hat{\vec{\theta}}+c\hat{\vec{\phi}}$, so what converts is basis elements, not coordinates. (See here for how it works.) But all you need to solve the problem at hand is $\hat{\vec{r}}\times\hat{\vec{\theta}}=\hat{\vec{\phi}}$ etc.; the basis is right-handed. See also here. You don't need to switch to Cartesian coordinates at any point. So we have $$(6\hat{\vec{r}}+\hat{\vec{\theta}}+2\hat{\vec{\phi}})\times 4\hat{\vec{r}}=8\hat{\vec{\theta}}-4\hat{\vec{\phi}}.$$