Matlab

How to Take Square Root in MATLAB

MATLAB helps users with data analysis, model development, and creating new algorithms. MATLAB can also perform several different mathematical calculations including square root calculation. MATLAB has several built-in functions which help to calculate different mathematical values. One such function is sqrt, which calculates the square root of any number in MATLAB. This article covers how we can take square roots in MATLAB.

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.

% Using the sqrt function
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:

numbers = [9, 16, 25, 36];
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:

% Using element-wise operations
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]).

% Using matrix operations
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.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.