Real nth root of real numbers
collapse all in page
Syntax
Y = nthroot(X,N)
Description
example
Y = nthroot(X,N)
returns the real nth root of X
. If an element in X
is negative, then the corresponding element in N
must be an odd integer.
Examples
collapse all
Real Root of Negative Number
Open Live Script
Find the real cube root of -27
.
nthroot(-27,3)
ans = -3
For comparison, calculate (-27)^(1/3)
. The result is the complex cube root of -27
.
(-27)^(1/3)
Real Roots of Scalar
Open Live Script
Calculate several real nth roots of -8
.
N = [5 3 -1];Y = nthroot(-8,N)
Y = 1×3 -1.5157 -2.0000 -0.1250
Real Roots of Vector
Open Live Script
Create a row vector of bases, X
, and a column vector of roots to calculate, N
.
X = [4 -3 -5];N = [1; -1; 3];
Calculate the real nth roots of the elements in X
. The result is a matrix containing all combinations of bases and roots. For example, Y(3,1)
is the 3rd root of 4.
Y = nthroot(X,N)
Y = 3×3 4.0000 -3.0000 -5.0000 0.2500 -0.3333 -0.2000 1.5874 -1.4422 -1.7100
Element-Wise Roots of Matrix
Open Live Script
Create a matrix of bases, X
, and a matrix of roots to calculate, N
. Each element in X
corresponds to an element in N
.
X = [-2 -2 -2; 4 -3 -5];N = [1 -1 3; 1/2 5 3];
Calculate the real nth roots of the elements in X
.
Y = nthroot(X,N)
Y = 2×3 -2.0000 -0.5000 -1.2599 16.0000 -1.2457 -1.7100
Except for the signs (which are treated separately), the result is comparable to abs(X).^(1./N)
. By contrast, you can calculate the complex roots using X.^(1./N)
.
Input Arguments
collapse all
X
— Input array
scalar | vector | matrix | multidimensional array | table | timetable
Input array, specified as a scalar, vector, matrix, multidimensional array, table, or timetable. The elements of X
must be real. Inputs X
and N must either be the same size or have sizes that are compatible.
Data Types: single
| double
| table
| timetable
N
— Roots to calculate
scalar | vector | matrix | multidimensional array | table | timetable
Roots to calculate, specified as a scalar, vector, matrix, multidimensional array, table, or timetable. The elements of N
must be real. If an element in X is negative, the corresponding element in N
must be an odd integer. Inputs X
and N
must either be the same size or have sizes that are compatible.
If
X
is a multidimensional array,N
cannot be a table or timetable.If
X
is a table or timetable, thenN
can be the same data type asX
or a scalar, vector, matrix, or multidimensional array of a compatible size. If both inputs are tables or timetables, then see Rules for Table and Timetable Mathematics for the input requirements.
Data Types: single
| double
| table
| timetable
Tips
While
power
is a more efficientfunction for computing the roots of numbers, in cases where both realand complex roots exist,power
returns only thecomplex roots. In these cases, usenthroot
to obtainthe real roots.
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™.
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™.
This function fully supports GPU arrays. 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
R2023b: Support for implicit expansion of arguments
nthroot
supports implicit expansion of the input arguments X
and N
. For more information on the required input sizes, see Compatible Array Sizes for Basic Operations.
If your code relies on the errors that MATLAB® previously returned for mismatched sizes, particularly within a try
/catch
block, then your code might no longer catch those errors.
R2023a: Perform calculations directly on tables and timetables
The nthroot
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
sqrt | power
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
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