
Calculating square and nth roots on Matlab
In this tutorial, I'll be teaching you how to compute both square roots and nth roots on Matlab.
Square root
To calculate the square root of a positive real number, use the pre-defined function sqrt()
sqrt(n)
The argument n is the radicand of the square root.
$$ \sqrt{n} $$
Here's some practical examples.
Example 1
Calculate the square root of 9.
$$ \sqrt{9} $$
Type the function sqrt(9) and press enter.
>> sqrt(9)
The function outputs the number 3.
ans = 3
Example 2
Alternatively, to calculate the square root, you can also use a power with exponent 1/2.
$$ 9^{\frac{1}{2}} $$
Type the expression 9^(1/2) and press enter.
>> 9^(1/2)
ans = 3
The result is always the same, it's the square root of 9.
Nth root
To calculate the nth root of a number, use the function nthroot()
nthroot(x,n)
The nthroot() function has two parameters.
The first parameter (x) is the radicand (under the root).
The second parameter (n) is the index of the nth root.
$$ \sqrt[n]{x} $$
Let me give you some practical examples.
Example 1
Calculate the cube root or third root of 8.
$$ \sqrt[3]{8} $$
To perform this calculation on Matlab, type nthroot(8,3)
>> nthroot(8,3)
The function returns the number 2 as the result.
ans = 2
because
$$ 2^3 = 2 \cdot 2 \cdot 2 = 8 $$
Example 2
Calculate the fourth root of 81.
$$ \sqrt[4]{81} $$
Type the function nthroot(81,4)
>> nthroot(81,4)
The result is the number 3.
ans = 3
because
$$ 3^4 = 3 \cdot 3 \cdot 3 \cdot 3 = 81 $$
Example 3
Alternatively, you can calculate the fourth root using a power with exponent 1/4
$$ 81^{\frac{1}{4}} $$
In this case, you need to write 81^(1/4) on the Octave command line.
>> 81^(1/4)
ans = 3
The final result is always the same.
You can calculate any nth root by raising the radicand to the power of 1/n.
Now you know how to calculate square and nth roots on Matlab.