Cartesian to spherical coordinate :MATLAB program

571 Views Asked by At

I am a beginner to MATLAB. I have written this function, but don't understand what is wrong.

I have used a if statement to correct the phi.

Say if i use (x,y,z) = (0,-4,3) i should get (5,270,53.13) but i get phi as 90 degree.

function [r,phi, theta] = cart2sp(x,y,z)

r = (x^2+y^2 +z^2)^0.5;
phi = abs(atand(y/x));
theta = atand((x^2+y^2)^0.5/z);


if phi<0
  phi = phi+180;

else
  phi =phi;


   end

end

It would be great help if you point out whats wrong, in the code or the logic.

Thank You very much

Anupam

1

There are 1 best solutions below

0
On

You compute phi = abs(...). This cannot be negative, so your if does not make sense. IMO omit the abs and add $360\;$ if phi < 0.

Warning: This is only mathematical reasoning, I do not use Matlab.