atan2 - Four-quadrant inverse tangent - MATLAB (original) (raw)
Four-quadrant inverse tangent
Syntax
Description
P = atan2([Y](#bucseo2-Y),[X](#bucseo2-X))
returns thefour-quadrant inverse tangent (tan-1) of Y
andX
, which must be real. The atan2
function follows the convention that atan2(x,x)
returns0
when x
is mathematically zero (either0
or -0
).
Examples
Find the four-quadrant inverse tangent of the point y = 4
, x = -3
.
Convert 4 + 3i
into polar coordinates.
theta = atan2(imag(z),real(z))
The radius r
and the angle theta
are the polar coordinate representation of 4 + 3i
.
Alternatively, use angle
to calculate theta
.
Convert r
and theta
back into the original complex number.
Plot atan2(Y,X)
for -4<Y<4
and -4<X<4
.
Define the interval to plot over.
[X,Y] = meshgrid(-4:0.1:4,-4:0.1:4);
Find atan2(Y,X)
over the interval.
Use surf
to generate a surface plot of the function. Note that plot
plots the discontinuity that exists at Y=0
for all X<0
.
surf(X,Y,P); view(45,45);
Input Arguments
_y_-coordinates, specified as a scalar, vector, matrix, multidimensional array, table, or timetable. Inputs Y
andX
must either be the same size or have sizes that are compatible (for example, Y
is anM
-by-N
matrix andX
is a scalar or1
-by-N
row vector). For more information, see Compatible Array Sizes for Basic Operations.
Data Types: single
| double
| table
| timetable
_x_-coordinates, specified as a scalar, vector, matrix, multidimensional array, table, or timetable. Inputs Y
andX
must either be the same size or have sizes that are compatible (for example, Y
is anM
-by-N
matrix andX
is a scalar or1
-by-N
row vector). For more information, see Compatible Array Sizes for Basic Operations.
Data Types: single
| double
| table
| timetable
More About
The four-quadrant inverse tangent, atan2(Y,X)
, returns values in the closed interval [–π, π] based on the values of Y and X, as shown in the graphic.
In contrast, atan(Y/X)
returns results that are limited to the interval [–π/2, π/2], shown on the right side of the diagram.
For real inputs, atan2
has a few behaviors that differ from those recommended in the IEEE®-754 Standard.
| | MATLAB® | IEEE | | | ------------ | ---- | ---- | | atan2(0,-0) | 0 | pi | | atan2(-0,-0) | 0 | -pi |
Extended Capabilities
Theatan2
function fully supports tall arrays. For more information, see Tall Arrays.
The atan2
function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006a
The atan2
function can calculate on all variables within a table or timetable without indexing to access those variables. All variables must have data types that support the calculation. For more information, see Direct Calculations on Tables and Timetables.