Square root and nth root in Octave
In this lesson I will explain how to calculate the square root and the nth root in Octave with a practical example.
The square root
The square root of a number can be calculated using the built-in function sqrt()
sqrt(n)
The parameter n is the radicand of the root.
$$ \sqrt{n} $$
I'll give you some practical examples.
Example 1
Calculate the square root of 9
$$ \sqrt{9} $$
Type sqrt(9) on the command line
>> sqrt(9)
The output result is the number 3
ans = 3
Example 2
Alternatively, you can also calculate the square root using powers, indicating 1/2 as the exponent
$$ 9^{\frac{1}{2}} $$
In this case you have to type 9^(1/2) on the Octave command line.
>> 9^(1/2)
ans = 3
The result is always the same
The nth root
To calculate the nth root of a number you have to use the function nthroot()
nthroot(n,i)
This function has two parameters.
The first parameter (n) is the radicand, the second parameter (i) is the index of the radical.
$$ \sqrt[i]{n} $$
I'll give you some practical examples.
Example 1
Calculate the cube root or third root of 8
$$ \sqrt[3]{8} $$
Type nthroot(8,3) on the command line
>> nthroot(8,3)
The result is number 2
ans = 2
because
$$ 2^3 = 2 \cdot 2 \cdot 2 = 8 $$
Example 2
Calculate the fourth root of 81.
$$ \sqrt[4]{81} $$
Type nthroot(81,4) on the command line
>> nthroot(81,4)
The result is number 3
ans = 3
because
$$ 3^4 = 3 \cdot 3 \cdot 3 \cdot 3 = 81 $$
Example 3
Alternatively, you can also calculate the fourth root using a power with exponent 1/4
$$ 81^{\frac{1}{4}} $$
In this case you have to write 81^(1/4) on the Octave command line
>> 81^(1/4)
ans = 3
The result is the same.
So, you can calculate any nth root using Octave.