atan2

Syntax

``P = atan2(Y,X)``

Description

example

````P = atan2(Y,X)` computes the four-quadrant inverse tangent (arctangent) of `Y` and `X`.Symbolic arguments `X` and `Y` are assumed to be real, and `atan2(Y,X)` returns values in the interval `[-pi,pi]`.```

Examples

collapse all

Compute the arctangents of these parameters. Because these numbers are not symbolic objects, you get floating-point results.

`P = [atan2(1,1), atan2(pi,4), atan2(Inf,Inf)]`
```P = 1×3 0.7854 0.6658 0.7854 ```

Compute the arctangents of these parameters which are converted to symbolic objects.

`P = [atan2(sym(1),1), atan2(sym(pi),sym(4)), atan2(Inf,sym(Inf))]`
```P =  $\left(\begin{array}{ccc}\frac{\pi }{4}& \mathrm{atan}\left(\frac{\pi }{4}\right)& \frac{\pi }{4}\end{array}\right)$```

Compute the limits of this symbolic expression.

```syms x P_minusinf = limit(atan2(x^2/(1 + x),x),x,-Inf)```
```P_minusinf =  $-\frac{3 \pi }{4}$```
`P_plusinf = limit(atan2(x^2/(1 + x),x),x,Inf)`
```P_plusinf =  $\frac{\pi }{4}$```

Compute the arctangents of the elements of matrices `Y` and `X`.

```Y = sym([3 sqrt(3); 1 1]); X = sym([sqrt(3) 3; 1 0]); P = atan2(Y,X)```
```P =  $\left(\begin{array}{cc}\frac{\pi }{3}& \frac{\pi }{6}\\ \frac{\pi }{4}& \frac{\pi }{2}\end{array}\right)$```

Input Arguments

collapse all

y-coordinates, specified as a symbolic number, variable, expression, or function, or as a vector, matrix, or array of symbolic numbers, variables, expressions, or functions. All numerical elements of `Y` must be real.

Inputs `Y` and `X` must either be the same size or have sizes that are compatible (for example, `Y` is an `M`-by-`N` matrix and `X` is a scalar or `1`-by-`N` row vector). For more information, see Compatible Array Sizes for Basic Operations.

x-coordinates, specified as a symbolic number, variable, expression, or function, or as a vector, matrix, or array of symbolic numbers, variables, expressions, or functions. All numerical elements of `X` must be real.

Inputs `Y` and `X` must either be the same size or have sizes that are compatible (for example, `Y` is an `M`-by-`N` matrix and `X` is a scalar or `1`-by-`N` row vector). For more information, see Compatible Array Sizes for Basic Operations.

collapse all

If X ≠ 0 and Y ≠ 0, then

`$\text{atan2}\left(Y,X\right)=\text{atan}\left(\frac{Y}{X}\right)+\frac{\pi }{2}\text{sign}\left(Y\right)\left(1-\text{sign}\left(X\right)\right)$`

Results returned by `atan2(Y,X)` belong to the closed interval `[-pi,pi]`. Meanwhile, results returned by `atan(Y/X)` belong to the closed interval `[-pi/2,pi/2]`.

Tips

• Calling `atan2` for numbers (or vectors or matrices of numbers) that are not symbolic objects invokes the MATLAB® `atan2` function.

• If one of the arguments `X` and `Y` is a vector or a matrix, and another one is a scalar, then `atan2` expands the scalar into a vector or a matrix of the same length with all elements equal to that scalar.

• If `X = 0` and `Y > 0`, then `atan2(Y,X)` returns `pi/2`.

If `X = 0` and `Y < 0`, then `atan2(Y,X)` returns `-pi/2`.

If `X = Y = 0`, then `atan2(Y,X)` returns `0`.

Alternatives

For complex `Z = X + Y*i`, the call `atan2(Y,X)` is equivalent to `angle(Z)`.

Version History

Introduced in R2013a