B = sqrt(X) returnsthe square root of each facet of the range X.For the elements of X that are negative or complicated, sqrt(X) producescomplex outcomes.

You are watching: How to do square root in matlab

The sqrt function’s domain includesnegative and complex numbers, which have the right to cause unintended resultsif used unintentionally. For negative and complicated numbers z= u + i*w, the complex square root sqrt(z) returns

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

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

If you want negative and complex numbers to rerotate error messagesrather than return complicated outcomes, use realsqrt instead.

## Examples

collapse all

### Square Root of Vector Elements

Open Live Script

Create a row vector containing both negative and also positive worths.

X = -2:2
X = 1×5 -2 -1 0 1 2
Compute the square root of each facet 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 range scalar | vector | matrix | multidimensional array

Input selection, stated as a numeric scalar, vector, matrix,or multidimensional range.

Documents Types: single | doubleComplex Number Support: Yes

collapse all

### IEEE Compliance

For genuine inputs, sqrt has actually a couple of actions that differ from those recommended in the IEEE®-754 Standard. In particular, negative inputs develop facility results rather of NaN.

MATLAB® IEEE

sqrt(-0)

0

-0

sqrt(X) for X

0+sqrt(-X)*i

NaN

## Extended Capabilities

### Tall Arrays Calculate via arrays that have more rows than fit in memory.

This function fully supports tall arrays. Formore indevelopment, view Tall Arrays.

### C/C++ Code Generation Generate C and also C++ code making use of MATLAB® Coder™.

Usage notes and limitations:

Simulation produces an error. Generated standalonecode returns NaN as soon as the input worth x isreal, however the output must be facility. To get the complex outcome,make the input value complex by passing in complex(x).

If you geneprice code for standalone targets and the input to sqrt in your MATLAB code is not a consistent, the value that the generated code returns for sqrt(-0) is identical to the value that the typical library function of your C/C++ compiler retransforms. If the conventional library feature adheres to 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 retransforms NaN once the input value x is actual, however the output need to be facility. To gain the facility result, make the input value facility by passing in complex(x).

### Thread-Based Environment Run code in the background using MATLAB® backgroundPool or acceleprice code through Parallel Computing Toolbox™ ThreadPool.

This function fully supports thread-based settings. For even more indevelopment, view Run MATLAB Functions in Thread-Based Environment.

### GPU Arrays Accelerate code by running on a graphics processing unit (GPU) utilizing Parallel Computing Toolbox™.

Usage notes and also limitations:

If the output of the attribute running on the GPU can be facility, then you have to clearly specify its input arguments as facility. For even more information, check out Work via Complex Numbers on a GPU (Parallel Computing Toolbox).

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

### Distributed ArraysPartition large arrays throughout the combined memory of your cluster utilizing Parallel Computing Toolbox™.

This feature totally supports dispersed arrays. For more information, watch Run MATLAB Functions through Distributed Arrays (Parallel Computing Toolbox).

nthroot | sqrtm | realsqrt

Introduced before R2006a
×

## Open Example

You have actually a modified variation of this example. Do you desire to open up this instance via your edits?

No, overcreate the modified variation Yes
×

## MATLAB Command

You clicked a connect that synchronizes to this MATLAB command:

Run the command by entering it in the MATLAB Command also Window. Web browsers carry out not assistance MATLAB regulates.

Cshed
×

Select a Net Site

Choose a internet site to get translated content wbelow accessible and also see local events and also offers. Based on your location, we recommfinish that you select: .

Select internet website

You deserve to also select a web website from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best website performance. Other jiyuushikan.org nation sites are not optimized for visits from your area.

See more: What Does 4 Knocks On A Door Mean, Answer That Knock! God Is Speaking

Switzerland also