Matlab

Square root in MatLab

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= sqrt(x)
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.

x= 81;
y=sqrt(x)

 

Example 2

The given MATLAB code determines the square root of each vector element using sqrt() function.

x= [1 4 9 16 25 36 49 64 81 100];
y=sqrt(x)

 

Example 3

The given MATLAB code determines the square root of each entry contained in the matrix using sqrt() function.

x= [-1:-3:-20; 7 5 87 91 144 256 10; -75 34 93 -86 36 169 78];
y=sqrt(x)

 

Example 4

The given MATLAB code determines the square root of a positive number using realsqrt() function.

x= 25;
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)).

x= [1 4 9 16 25 36 49 64 81 100];
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.

x= [1:3:10; 87 91 144 256; 75 34 86 169; 1024 787 26 657];
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.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.