code:
function [Pcyl Psph] = cart2cylsph(Pcart)% converts a point in cartesian to cylindrical and spherical cooridantes
% input and output as 3 by 1 vectors
x=Pcart(1);y = Pcart(2);z= Pcart(3);rho= sqrt(x^2+y^2);r = sqrt(x^2+y^2+z^2);if x,y > 0 phi = atan(y/x);elseif x,y < 0 phi = atan(y/x)+pi;elseif x>0 && y<0 phi = 2*pi-atan(y/x);else phi = pi-atan(y/x);endif z>0 theta=atan(rho/z);else theta= pi - atan(rho/z);endPcyl= [rho phi z]';Psph = [r theta phi]';end
Best Answer