Matlab

Square root in MatLab

The following article explains how to calculate square roots in MATLAB®, the use of each function for this mathematical operation, and its main characteristics.

This topic includes practical examples, as well as some of the most common errors, their corresponding error messages, and how to fix them to make programming easier.

For these mathematical operations, there are three basic functions in MATLAB, realsqrt, sqrt and sqrtm. The realsqrt function computes only real numbers, the sqrt function computes the square root of numbers with positive and negative signs or complex numbers, and the sqrtm function computes the principal square root of a matrix.

MATLAB realsqrt function syntax

b = realsqrt (x)

Description and examples

The realsqrt() function returns in “b” the square root of each element of the array “x”. This function accepts only real values with a positive sign. The argument types for the input array “x” are vector, matrix, numeric scalar, or multidimensional array, and the data type supported by this function is single or double-sized.

How to get the square root of the vector with real values using the realsqrt function.

This example shows how to calculate the vector “x” square root using the MATLAB® function realsqrt. An ascending “x” vector will be created with positive-sign numbers from 1 to 5 and call the realsqrt() function, sending this vector as the input argument. This operation takes the square root of each “x” vector element, returns the result in “b”, and displays it on the screen.

x = [1 2 3 4 5];
b = realsqrt (x)

What is the message “Error using realsqrt (line n) Realsqrt produced complex output”?

Now we take the “x” vector from the previous example and replace the value of the last element with a value with a negative sign and try to get its square root.

x = [1 2 3 4 -5]
b = realsqrt (x)

In cases where any element of the input arguments is a value less than 0 or a complex number, a result is a complex number. In this case, realsqrt returns the following error message:

“Error using realsqrt (line n) Realsqrt produced complex output.”

This output is because the realsqrt() function accepts only real values. For operations with complex or negative values may not be correct, and the realsqrt() function should be replaced with the sqrt() function. The following figure shows the correct method for calculating square roots with negative values or complex numbers using the sqrt() function.

MATLAB sqrt function

Syntax

b = sqrt (x)

Description and examples

The sqrt function calculates the square root of each element in an array. The sqrt function must be called for this operation, sending in “x” the array whose square root is to be determined. As a result, sqrt() on “b” returns the square root of each element in that array. This function supports real values with positive, negative, and complex numbers. The data types accepted by sqrt() are single or double. The data types accepted by this function in its input arguments are array, numeric scalar, or multidimensional array.

How to obtain the square root of a vector in complex numbers using the sqrt function.

This example shows how to calculate the square root of a vector using the sqrt function in MATLAB®; to do this, we create the vector “x” with elements with negative and positive signs.

x = -3:3
b = sqrt (x)

As you can see in the figure, the sqrt() function returns complex numbers in “x”.

NOTE: for X= -0 sqrt() returns in MATLAB = 0. In IEEE = -0 and for X < 0 In MATLAB = 0+sqrt(-X)*i and In IEEE = NaN.

MATLAB sqrtm function

Syntax

A = sqrtm (X)

Description and examples

The sqrtm() function is used to calculate the principal square root of a matrix.
For this operation, the sqrtm() function must be called by specifying in “x” the matrix from which you want to take the square root. As a result, it returns in “b”, the main square root of this matrix. This function accepts only square arrays with elements of single and double size as input arguments. In cases where “a” has eigenvalues with negative real values, the sqrtm() function returns complex values.

Get the principal square root of a square matrix using the sqrtm function.

The following example shows how to calculate the principal square root of the magic square “x” using the sqrtm function.

a = magic (4);
x = realsqrt (a)

What does the message “Error using sqrtm (line n) Input matrix must be square” mean?

In the following example, we will try to calculate the main square root of an “x” matrix with a size of 5 x 4 elements.

x = [1 2 3 4 -5; 5 4 3 2 1; 1 2 3 4 5; 5 4 3 2 1]
b = sqrtm (x)

In this case, the sqrtm function returns the following error message:

“Error using sqrtm (line n) Input matrix must be square.”

This error message is because the sqrtm function only accepts square arrays as input arguments, i.e. n rows = n columns.

Is it possible to calculate the square root of a singular matrix?

When it comes to calculating the square root of a singular matrix, it may be that the matrix itself does not have a square root. In the following example, we will try to calculate the square root of a matrix known to be a singular matrix.

Conclusion

In this tutorial, I have explained how to use the various basic concepts of MATLAB to solve square root operations and have outlined the main features of each function. I have also given some practical examples that teach how to use these functions and the most common errors that occur with their respective messages to make your programming work easier. The input arguments for each function and the accepted data type have also been detailed. We hope that you found this MATLAB article useful. See other Linux Hint articles for more tips and information.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).