| by Arround The Web | No comments

The abs Function in MATLAB

This article explains how to use the MATLAB abs() function to obtain the absolute value or modulus of each element of a matrix.

We also explain the individual calling methods of this function and describe in detail its input and output arguments, as well as the accepted data types.

In addition, we explain various ways to obtain the absolute value of complex magnitudes using the various tools and functions that MATLAB provides us to solve this mathematical operation.

This article includes practical examples and images that explain each of the ways to use this function, which is one of the most used functions in the library of mathematical functions of this powerful programming language.

Syntax of the MATLAB abs() Function

a = abs (x)

Description and Examples

Matlab’s abs() function returns in “a” the absolute value of each value of the array sent in “x”.

The input arguments to this function can be the following:

For Real Values:

In cases where abs() is called with real values in “x”, this function returns the absolute value in “a”, the unsigned value of “x”. The type of input array for abs() can be vectors, scalars, matrices, or multidimensional arrays.

The data types accepted by input and output arrays are: single, double, int8, int16, int32, int64, uint8, uint16, uint32, uint64, or duration.

For Complex Values:

This function accepts complex numbers. In this case, the data type of the array must be single or double.

For complex numbers, abs() returns the complex magnitude or modulus of “x”. The complex magnitude can be calculated by taking the square root of the absolute value of the real part squared plus the absolute value of the imaginary part squared.

Next, we will see how to calculate the complex amount.

module =

How to Get the Absolute Value of a Scalar with the abs() Function

In the following example, we see how to obtain the absolute value of a scalar using the abs() function. Since the scalar in this case has a real value, abs() will return the unsigned real result of “x”.

a = abs(-58)

a = 58

As a result, abs() will return the absolute value of “x”. In this case, since it is a real number, the result in “a” will be the same magnitude as “x” but without a sign. In the following image, you can see this expression and its results applied in the MATLAB command console.

How to Get the Absolute Value of an Array

Now, we will see how to obtain the absolute values of the elements of an array. For this, we create an array “x” of 4×5 elements with values of positive and negative sign.

x = [ 12, 51, -84, 5, -6;

23, -9, -54, 21, 22;

25,-89, -74, 25, 2;

14, -7, -85, 66,-23];

a = abs(x)

a =

12 51 84 5 6

23 9 54 21 22

25 89 74 25 2

14 7 85 66 23

As a result, abs() returns an array containing the absolute values of each element of the array passed in its input arguments. As seen in the picture, the results in “a” are the unsigned values of “x”. In the following image, you can see this expression and its results applied in the MATLAB command console.

How to Get the Complex Magnitude of a Scalar Using MATLAB abs() Function

MATLAB abs() function supports complex numbers. The absolute value or modulus of a complex number is calculated by taking the square root of the real part squared plus the imaginary part squared. In this example, we will find the complex amount of 3.5653 + 14.2363i using the abs() function in MATLAB.

x = abs(3.5653 + 14.2363i)

x =

14.6760

% The calculation can also be done using the sqrt() function as follows:

x = sqrt( (3.5653.^2) + (14.2363.^2))

x =

14.6760

As seen in the following image, we have obtained the complex magnitude of 3.5653 + 14.2363i using two different ways, the first through the abs() function as shown below:

x = abs(3.5653 + 14.2363i);

The other way was to use the sqrt() function to get the square root of the sums of 3.5653 and 14.2363 squared.

x = sqrt((3.5653.^2) + (14.2363.^2))

In the following image, you can see this expression and its results applied in the MATLAB command console:

How to Get the Complex Magnitude of an Array with MATLAB’s abs() Function

In this example, we will see how to obtain the absolute values of an array of 5×5 elements containing real and complex magnitudes. To do this, we create the array “x” with these values and send it as an input argument in the call to the abs() function.

x= [ 12+54i, 5-23i, 16+64i, 88, -3;

8+21i, -57, -89+22i, -9, 2-40i;

5+54i, -99, 35+59i, 23, -124;

57-23i, -59, 3-87i, 23, -124;

11, 35+6i, 21, 27-17i, 9+95i];

a= abs(x)

a =

55.3173 23.5372 65.9697 88.0000 3.0000

22.4722 57.0000 91.6788 9.0000 40.0500

54.2310 99.0000 68.6003 23.0000 124.0000

61.4654 59.0000 87.0517 23.0000 124.0000

11.0000 35.5106 21.0000 31.9061 95.4254

As a result, abs() will return an array of the same size as “x” with the absolute values of each element. In the following image, you can see this expression and its results applied in the MATLAB command console:

Conclusion

In this article, we explained how to obtain absolute values using the MATLAB abs() function. We also show you several alternatives on how to solve this mathematical calculation using other functions in the MATLAB library. We have also included practical examples and images that use this function with different types of input, so you can better understand which methods to call in each case. We hope you found this MATLAB article useful. See other Linux Hint articles for more tips and information.

Share Button

Source: linuxhint.com

Leave a Reply