The second argument to unwrap is not the period of the input data, but the tolerance. The function always unwraps data assuming a 2π interval. That is, it wants to see that x(i)-x(i+1) is larger than the tolerance before unwrapping, and smaller after unwrapping. In the case of a tolerance of pi/2, if, for example, x(i)=0 and x(i+1)=3, the jump is larger than the tolerance, but adding or subtracting 2*pi to x(i+1) does not improve things.
One work-around would be to multiply the input by 2, and divide by 2 after unwrapping:
unwrap(theta_atan * 2) / 2
However, it is always best to use atan2 to obtain an angle.
Changing the atan function so that it ranges from 0 to 2*pi
Matlab atan Result Range - Stack Overflow
math - Python atan or atan2, what should I use? - Stack Overflow
How exactly is atan2 implemented in matlab?
The second argument to unwrap is not the period of the input data, but the tolerance. The function always unwraps data assuming a 2π interval. That is, it wants to see that x(i)-x(i+1) is larger than the tolerance before unwrapping, and smaller after unwrapping. In the case of a tolerance of pi/2, if, for example, x(i)=0 and x(i+1)=3, the jump is larger than the tolerance, but adding or subtracting 2*pi to x(i+1) does not improve things.
One work-around would be to multiply the input by 2, and divide by 2 after unwrapping:
unwrap(theta_atan * 2) / 2
However, it is always best to use atan2 to obtain an angle.
Atan takes single argument and Atan2 takes two arguments.The purpose of using two arguments instead of one is to gather information on the signs of the inputs in order to return the appropriate quadrant of the computed angle, which is not possible for the single-argument Atan

Atan2 result is always between -pi and pi.
Reference: https://en.wikipedia.org/wiki/Atan2
docstring for math.atan:
atan(x) Return the arc tangent (measured in radians) of x.
docstring for math.atan2:
atan2(y, x) Return the arc tangent (measured in radians) of y/x. Unlike atan(y/x), the signs of both x and y are considered.
To be very complete, here's what the doc says about atan2:
math.atan2(y, x) Return atan(y / x), in radians. The result is between -pi and pi. The vector in the plane from the origin to point (x, y) makes this angle with the positive X axis. The point of atan2() is that the signs of both inputs are known to it, so it can compute the correct quadrant for the angle. For example, atan(1) and atan2(1, 1) are both pi/4, but atan2(-1, -1) is -3*pi/4.
So it's pretty clear: the outputs are different because of the signs of ImZ and ImR. atan2 returns the appropriate quadrant, unlike atan.
Hi everyone,
I was using the following formula to find the angle between three coordinate values (i.e., P1, P0, P2; finding angle at P0)
ang = (180/pi)*(atan2(abs(det([P2-P0;P1-P0])),dot(P2-P0,P1-P0)));
formula taken from: https://in.mathworks.com/matlabcentral/answers/57736-how-to-calculate-degree-between-3-points-in-matlab#answer_69886
So, it worked fine when I took the coordinate values as ;
P0 = [3,1];P1 = [1,3];P2 = [4,4];figure;plot(P0(1),P0(2),"*"); hold on;plot(P1(1),P1(2),"*"); hold on;plot(P2(1),P2(2),"*"); hold on;ylim([0 7])xlim([0 5])legend("P0","P1","P2")% P0 is the center where the angle would beang = (180/pi)*(atan2(abs(det([P2-P0;P1-P0])),dot(P2-P0,P1-P0))); % formula to get angle% ang = 63.439 deg
But when I took three coordinate values as the following then it gave me a strange angle value, which is probably the supplmentary angle at P0.
P1 = [-16.49,-17.69];P0 = [-25.83,-21.73];P2 = [-40.77,-18.10]figure;plot(P0(1),P0(2),"*"); hold on;plot(P1(1),P1(2),"*"); hold on;plot(P2(1),P2(2),"*"); hold on;legend("P0","P1","P2");ang = (180/pi)*(atan2(abs(det([P2-P0;P1-P0])),dot(P2-P0,P1-P0)));% ang = 142.9526 deg ; seems like the supplementary angle at P0.
Why is it happening? Any clues?
I need to apply this formula to calculate angle over 100s of coordinates and but if this kind of non-uniformity would happen then I wouldn't be able to use it.
Any help would be appreciated!
Thank you.
I have also asked in the Matlab community at Mathworks:
https://in.mathworks.com/matlabcentral/answers/1793970-error-in-finding-the-angle-between-three-points-using-atan2