MATLAB: If statement not working

if

If the Xnew is negative the answer for X should also be negative, but this is not the case. The Y does work correctly however. I included the input array, the r is constantly 13.
function [X,Y,Z] = bolcoordinaten2(Xnew,Ynew,r)
Z = r-cos(2*atan((abs(sqrt((Xnew.^2)+(Ynew.^2))))./(2*r))).*r;
a = cos(atan(Ynew./Xnew)).*r.*sin(2*atan((abs(sqrt((Xnew.^2)+(Ynew.^2))))./(2*r)));
if Xnew < 0
X = a*(-1);
else
X = a;
end
b = sin(atan(Ynew./Xnew)).*r.*sin(2*atan((abs(sqrt((Xnew.^2)+(Ynew.^2))))./(2*r)));
if Ynew < 0
Y = b*(-1);
else
Y= b;
end
end

Best Answer

mask = XNew < 0;
X = a;
X(mask) = -X(mask);