ATAN2_SAFE

To perform the same function as Matlab built-in function atan2 but safely guarded against erroneous
714 Downloads
Updated 7 Jul 2008

View License

This will perform the same function as Matlab built-in function ATAN2 but
safely guarded against erroneous results when the inputs are very small (near to a machine zero).

Due to the round off errors in the numerical calculations, the two
inputs to ATAN2, Y and X, had better be thought as

x=x_exact + (or -) rand*eps;

y=y_exact + (or -) rand*eps;

When the theoretically expected values x_exact and y_exact are
far above the machine accuracy, ATAN2 will give you a good
answer, insensitive to the small unpredictable round off errors.
On the other hand, when one or both of the theoretical values is
zero, ATAN2 becomes very sensitive to the round off errors and
will give an erroneous result. Consider the case where
both x_exact and y_exact are zero, then the call to ATAN2 is
equivalent to the following random experiment

x=(rand-0.5)/0.5*eps;

y=(rand-0.5)/0.5*eps;

theta=atan2(y, x);

Now theta becomes a random number ranging all over between
-pi and pi. (This should apply to ATAN as well).

Since ATAN2(0,0) will give an exact zero, and since the round-off
errors due to the machine accuracy cannot be meaningfully
distinguished from the true zero, we had better clear off (zero out)
the small round off errors before we call ATAN2. To relieve you
from the cleaning burden every time when you need to call ATAN2,
ATAN2_safe automates the cleaning and the calling two steps
for you.

Cite As

Zhigang Xu (2024). ATAN2_SAFE (https://www.mathworks.com/matlabcentral/fileexchange/20134-atan2_safe), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2008a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0.0

The help message is improved.