Main Content

nthroot

Real nth root of real numbers

Description

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.

example

Examples

collapse all

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)
ans = 
1.5000 + 2.5981i

Calculate several real nth roots of -8.

N = [5 3 -1];
Y = nthroot(-8,N)
Y = 1×3

   -1.5157   -2.0000   -0.1250

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

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

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

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, then N can be the same data type as X 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 efficient function for computing the roots of numbers, in cases where both real and complex roots exist, power returns only the complex roots. In these cases, use nthroot to obtain the real roots.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced before R2006a

expand all

See Also

|