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
You compute
phi = abs(...).This cannot be negative, so yourifdoes not make sense. IMO omit theabsand add $360\;$ ifphi < 0.Warning: This is only mathematical reasoning, I do not use Matlab.