To complement this, we will go over the conditions under which a function or expression returns this type of value as a result and when this is the product of an overflow due to extensive data.
We will also see what functions are available in MATLAB to determine if an array contains this value.
Below, we have prepared some practical examples with code snippets and screenshots showing how to work with infinities in MATLAB.
Representation of Infinity in MATLAB
MATLAB inf() Function Syntax
x = Inf
x = Inf(n)
x = Inf(n…. n1)
x = Inf(zn)
x = Inf(___,typename)
x = Inf(___,’like’,p)
MATLAB inf() Function Description
The inf() creates “x” scalars, vectors, or arrays with infinities in all their elements. This function provides flexibility in size, shape, dimension, and data type of the array output. As you can see in the previous syntaxes, the inputs to set these parameters are the same as most functions that create arrays in MATLAB. Next, we look at each of these inputs and the parameters they set in the output array.
n: This input specifies the size of the square matrix created by inf().
z1…. zn: This input specifies the matrix size created by inf().
typename: This input sets the data type that the output array should have.
like: When the inf() function is called with this flag, the data type in the output array will be equal to the data type of “p“.
p: This is a prototype. When the inf() function is called with the ‘like’ flag, the data type in the output array will be equal to the data type of “p“.
In the following examples, you will find code snippets and images that show how to implement this function in its different calling modes and with different types of output arrays.
How To Create an Infinite Scalar With the inf() Function of MATLAB
In this example, we will see how to create an infinite scalar in x using this function. This is the easiest way to call at inf() since it goes out empty and returns an infinite scalar in x, as we see in the following code snippet:
How To Create an Infinity Values Square Matrix With n Rows x n Columns With the “n” Input of the MATLAB inf() Function
In this example, we use the input n to create a square matrix of n rows by n columns at “x”. In this case, the matrix x will be 5 by 5. Therefore, we call the function by putting the number 5 in the input n, as we see in the following fragment:
As shown in the figure below, the input “n” determines the number of rows and columns the square output matrix x will have:
How To Set the Number of Rows and Columns in an Infinite Values Array Created With the inf() Function
Now, let us see how we set the number of rows and columns of the array “x” with the input n….n1 of this function. To do this, we need to call the function and set the number of columns and rows the output array should have in this input. The following code snippet shows how to create an array with 2 rows and 5 columns using the MATLAB function inf():
In the following image, we can see the result in the MATLAB command console:
How To Set the Data Type of the Elements of the Output Array With the “typename”, “like”, and “p” Input of the MATLAB inf Function
When we use the inf() function, we can choose what data type the output array should be. We do this by specifying the data type as a character string. The types that can be given are “single” or “double”.
In the following fragment, we see how to create a 3 x 3 array of infinities of type “single”:
class ( x )
As we see in the following image, the class() function shows the data type of the array elements. In this case, we have created an array with singles.
The inputs “like” and “p” provide the ability to create an array with the same data type as a given scalar, vector, or matrix in “p”. To do this, call the inf() function and enable this option by sending the string “like” and, separated by a comma, the name of the scalar, vector, or prototype matrix that we want the output matrix to have the same data type as. The following example shows how to create the vector “v” with the same data type as the scalar “e”:
x = inf ( 1, 5, 'like', e )
class ( x )
What Conditions Generate Infinite Results in MATLAB?
Not only is MATLAB capable of generating these values arbitrarily with the inf() function, but it can also return infinities when you try to divide a number by zero.
It is also given when we want the exponential value of 1000 or the logarithm of 0.
x = log ( 0 )
MATLAB can also return an infinite overflow if the result of a function or operation is extremely large. This is similar to calculators that return an “e” result.
MATLAB also provides the isinf() function to determine whether the values contained in a scalar, vector, or matrix are infinite.
How To Determine if a Value is Infinity With the MATLAB isinf() Function
The MATLAB function isinf() determines whether the elements of an array are infinite. Inf() returns in “x” the logical result 1 if the element’s value is infinite and 0 if it is not.
Now, we will see how we can use this function to determine if a scalar has an infinite value. To do this, we create the scalar “a” with a finite value and the scalar “b” with infinity and call each function to show the result returned for each.
b = inf;
x = isinf ( a )
x = isinf ( b )
In the following figure, we see that the result for “a” is a logical 0, while for “b”, which has the value inf “x”, there is a 1:
In this article, we have explained everything you need to know about infinity in MATLAB. We have shown how this value is represented and its syntax in this language. We also went into some of the conditions that can give infinite results and showed you how to use the isinf() function to determine the presence of these values in a scalar, vector, or array and how to create arrays of infinities using the inf () function. We hope you found this MATLAB article helpful. See other Linux Hint articles for more tips and information.