Square root
collapse all in page
Syntax
B = sqrt(X)
Description
example
B = sqrt(X)
returnsthe square root of each element of the array X
.For the elements of X
that are negative or complex, sqrt(X)
producescomplex results.
The sqrt
function’s domain includesnegative and complex numbers, which can lead to unexpected resultsif 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 messagesrather than return complex results, use realsqrt instead.
Examples
collapse all
Square Root of Vector Elements
Open Live Script
Create a row vector containing both negative and positive values.
X = -2:2
X = 1×5 -2 -1 0 1 2
Compute the square root of each element of X
.
Y = sqrt(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
X
— Input array
scalar | vector | matrix | multidimensional array | table | timetable
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
IEEE Compliance
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 | |
---|---|---|
|
| |
|
|
|
Tips
See sqrtm for the matrix square root.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
This function fully supports tall arrays. Formore information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Simulation produces an error. Generated standalonecode returns
NaN
when the input valuex
isreal, but the output should be complex. To get the complex result,make the input value complex by passing incomplex(x)
.If you generate code for standalone targets and the input to
sqrt
in your MATLAB code is not a constant, the value that the generated code returns forsqrt(-0)
is identical to the value that the standard library function of your C/C++ compiler returns. If the standard library function complies with the IEEE-754 Standard, the generated code returns-0
.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
Simulation produces an error. Generated standalone code returns
NaN
when the input valuex
is real, but the output should be complex. To get the complex result, make the input value complex by passing incomplex(x)
.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
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).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a
expand all
R2023a: Perform calculations directly on tables and timetables
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.
See Also
nthroot | sqrtm | realsqrt
Commande MATLAB
Vous avez cliqué sur un lien qui correspond à cette commande MATLAB:
Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本 (日本語)
- 한국 (한국어)
Contact your local office