sqrt

Square root, element by element.

Category: Mathematics > Elementary Math

Syntax

Y = sqrt(X)

Returns the principal square root of each element of X.

Description

The principal root is the one with nonnegative real part, so sqrt(-4) is 0 + 2i. If any element is negative, the whole result array becomes complex, and the entries that were nonnegative show a zero imaginary part.

The result of sqrt is a floating-point approximation: sqrt(2)^2 == 2 is false. For the square root of a matrix as a matrix use sqrtm; for an error when the result would be complex, use realsqrt.

Input arguments

X
Numeric array, real or complex.

Output arguments

Y
Array of the same size as X. Negative real elements make the result complex.

Example

Real, negative and inexact

sqrt([1 4 9])
sqrt([4 -4 2])
sqrt(2)^2 == 2
Output
ans = 1×3

     1     2     3
ans = 1×3 complex

   2.0000 + 0.0000i   0.0000 + 2.0000i   1.4142 + 0.0000i
ans = logical
   0

Notes

Integer classes such as int8 are not accepted.

See also

realsqrt nthroot sqrtm hypot power