0

I am given (x,y) points and I know the radius. I want to to find the angle of each point on circle. In the future I want to use this code as part of a test whether the angles are distributed uniformly on the interval [0,2Pi].

The code I've tried is

b <- matrix(c(d[,2],d[,5]),ncol=2)
S <- (b/sqrt((rowSums(b^2))))
#S <- matrix(S[!is.na(S)],ncol=2)
lim <- c(-1,1)
plot(S,xlim=lim,ylim=lim,xlab=expression(S1),ylab=expression(S2))
#S<-S[!is.na(S)]
U <- matrix(c(0),nrow=nrow(S),ncol=1)
for (i in 1:nrow(S)) {
    U[i,1] <- (atan(abs(S[i,1]/S[i,2])))
}

That code gave me some angles, but none lay in the interval [270,360].

points on circle

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Ganaa
  • 5
  • 3
  • 2
    It would be helpful if you include the values of all the variables, such as `b`, and give the exact error message. – F Trias May 13 '20 at 23:31
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 13 '20 at 23:50
  • 1
    Your U_i are atan(abs(S[i,1]/S[i,2])). atan of a positive number is never in [270:360]. – G5W May 14 '20 at 00:32
  • 2
    Use `atan2` instead of `atan`. – user2554330 May 14 '20 at 00:43

1 Answers1

3

Hi if you could give some simple sample of your data that would be very helpful.

However I can initially see some problems with the use of atan().

  1. atan takes a single argument and returns an angle in radians NOT degrees.
  2. atan result is ambiguous because you dont know what quadrant the values are in. For instance there is not way of distinguishing whether a negative x value is because we are in the 2nd or 4th quadrant.

To solve this you can use atan2() which takes two arguments and thus removes the ambiguity see here. For intance:

using just atan you can see this problem (I am converting to degrees here)

atan(1/1) * 180/pi # first quadrant
# 45
atan(1/-1) * 180/pi # second quadrant
# -45
atan(-1/-1) * 180/pi # third quadrant
# 45
atan(-1/1) * 180/pi # fourth quadrant
# -45

As you can see you are only getting results on [-90,90] ( [-pi/2, pi/2] ).

But using atan2()

atan2(y = 1, x = 1) * 180/pi # first quadrant
# 45
atan2(y = 1, x = -1) * 180/pi # second quadrant
# 135
atan2(y = -1, x = -1) * 180/pi # third quadrant
# -135 same as 225 (360-135)
atan2(y = -1, x = 1) * 180/pi # fourth quadrant
# -45 same as 315 (360-45)

As you can see now you are able to remove the ambiguity surrounding which quadrant your values falls in using atan2.

Croote
  • 1,382
  • 1
  • 7
  • 15