The square root is a mathematical operation that finds the value of a number that, when multiplied by itself, equals the same number. In simple words, the square root is the inverse operation of squaring a number denoted by the symbol √ and is commonly used in various fields of mathematics, science, engineering, and programming. In MATLAB, you can find the square root of a number through built-in functions.
Follow this guide to learn in detail how to perform square root in MATLAB.
Square Root in MATLAB
MATLAB offers several built-in functions that make it easier for you to calculate the square root of a specific value. Regardless of whether the variable is positive, negative, or even complex, the square root operation can be performed. With these built-in functions, you can easily and quickly find the square root in MATLAB.
There are three types of square root functions available in MATLAB; which are sqrt(), realsqrt(), and sqrtm(). These three functions follow a simple syntax that is given below:
y= realsqrt(x)
y = sqrtm(x)
Here:
-
- y=sqrt(x) yields the square root of the given argument that can be a real or complex number, vector, or matrix.
- y=realsqrt(x) yields the square root of the given argument that can be a nonnegative real number, vector, or matrix.
- y=sqrtm(x) yields the principal square root of the given argument which is a square matrix.
How to Use the Square Root Function in MATLAB
The following examples demonstrate the use of square root functions in MATLAB:
Example 1
The given MATLAB code finds a positive number square root using the sqrt() function.
y=sqrt(x)
Example 2
The given MATLAB code determines the square root of each vector element using sqrt() function.
y=sqrt(x)
Example 3
The given MATLAB code determines the square root of each entry contained in the matrix using sqrt() function.
y=sqrt(x)
Example 4
The given MATLAB code determines the square root of a positive number using realsqrt() function.
y=realsqrt(x)
Example 5
The given MATLAB code first determined the square root of each vector element using sqrt() function and then plots a graph of (x, realsqrt(x)).
y=realsqrt(x);
plot(x, y)
Example 6
The given MATLAB code determines the square root of a 4-by-4 matrix using sqrtm() function.
y=sqrtm(x)
Conclusion
The mathematical operation square root can be calculated by three built-in MATLAB functions which have different functionalities. These functions are sqrt(), realsqrt(), and, sqrtm(). This guide explored the working of these three MATLAB functions using simple examples.