sqrt - Square root - MATLAB (original) (raw)

Main Content

Syntax

Description

B = sqrt([X](#bu57a5h-X)) returns the square root of each element of the array X. For the elements of X that are negative or complex, sqrt(X) produces complex results.

The sqrt function’s domain includes negative and complex numbers, which can lead to unexpected results if used unintentionally. For negative and complex numbers z = u + i*w, the complex square root sqrt(z) returns

sqrt(r)*(cos(phi/2) + 1i*sin(phi/2))

where r = abs(z) is the radius and phi = angle(z) is the phase angle on the closed interval -pi <= phi <= pi.

If you want negative and complex numbers to return error messages rather than return complex results, use realsqrt instead.

example

Examples

collapse all

Create a row vector containing both negative and positive values.

Compute the square root of each element of X.

Y = 1×5 complex

0.0000 + 1.4142i 0.0000 + 1.0000i 0.0000 + 0.0000i 1.0000 + 0.0000i 1.4142 + 0.0000i

Input Arguments

collapse all

Input array, specified as a numeric scalar, vector, matrix, multidimensional array, table, or timetable.

Data Types: single | double | table | timetable
Complex Number Support: Yes

More About

collapse all

For real inputs, sqrt has a few behaviors that differ from those recommended in the IEEE®-754 Standard. In particular, negative inputs produce complex results instead of NaN.

| | MATLAB® | IEEE | | | ----------------- | ------------- | --- | | sqrt(-0) | 0 | -0 | | sqrt(X) for X < 0 | 0+sqrt(-X)*i | NaN |

Tips

Extended Capabilities

expand all

Thesqrt function fully supports tall arrays. For more information, see Tall Arrays.

Usage notes and limitations:

Usage notes and limitations:

The sqrt function supports GPU array input with these usage notes and limitations:

If the output of the function running on the GPU can be complex, then you must explicitly specify its input arguments as complex. For more information, see Work with Complex Numbers on a GPU (Parallel Computing Toolbox).

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a

expand all

The sqrt 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.