How to Calculate Square Roots in MATLAB
In MATLAB, there are multiple methods available to compute the square root of a number:
Computing Square Root Using MATLAB sqrt() Function
The primary function for computing square roots in MATLAB is sqrt(). It takes a single input argument and returns the square root of that number. For instance, to find the square root of 9, run the below code.
x = sqrt(9)
% x = 3
The sqrt() function can also operate on arrays or matrices, allowing us to compute square roots for multiple numbers simultaneously. This is particularly useful when working with datasets or performing element-wise calculations. Consider the following example:
roots = sqrt(numbers);
roots
In this case, the roots variable will contain the values [3, 4, 5, 6].
Computing Square Root Using Element-Wise Operations
Element-wise operations perform calculations on each element of an array or matrix individually. The .^ operator is used inside the MATLAB code to find each element square root in an array or matrix using element-wise operations.
Following is a MATLAB example code that calculates square root element-wise:
y = [1,4,9].^0.5
% y = [1,2,3]
Computing Square Root of Matrices Using MATLAB sqrtm() Function
Matrix operations perform calculations on entire matrices at once. To calculate the square root of a matrix using matrix operations, use the sqrtm function like this: sqrtm([1,4;9,16]).
z = sqrtm([1,4;9,16])
% z = [1,2;3,4]
Conclusion
MATLAB is a computational programming software that uses high-level programming to perform mathematical calculations. This article covers how one can calculate the square roots of numbers in MATLAB using different techniques. MATLAB has a square root function that performs square root calculations. We can also calculate element-wise square root calculations using this function, for further details read the article.