realsqrt

Square root that errors on complex results.

Category: Mathematics > Elementary Math

Syntax

Y = realsqrt(X)

Returns sqrt(X) for nonnegative real X, and an error if any element is negative.

Description

sqrt(-4) is 0 + 2i. realsqrt(-4) instead stops with Realsqrt produced complex result., which catches the case in which a negative value reaches a square root by mistake, for example a variance that rounding made slightly negative. For nonnegative input the result is identical to sqrt.

Input arguments

X
Real array whose elements are expected to be nonnegative.

Output arguments

Y
Real array of the same size as X.

Example

Strict square root

realsqrt([4 9 16])
sqrt(-4)
Output
ans = 1×3

     2     3     4
ans = 0.0000 + 2.0000i

See also

sqrt reallog realpow nthroot