Matlab

How to Work with Powers and Exponentials in MATLAB

Taking powers and exponentials of any scalar, vector, or multidirectional array is a basic and widely used mathematical operation. We can compute the power of any scalar or array in MATLAB using the ^ operator; however, the exponentials of a scalar or an array can be calculated using the built-in exp() function.

This tutorial is going to discuss different examples of how to work with powers and exponentials of a scalar, matrix, vectors, or an array in MATLAB.

1: How to Take Power in MATLAB

Taking power in MATLAB is a useful task regardless of whether you are taking the power of a scalar, array, or matrix, all and all you have to use the ^ operator for this operation. The ^ operator computes any positive, negative, and fractional powers of a number within no time. The ^ operator is a piecewise operator which means it is applied to each element of an array individually using the .^ operator instead. The same case applies to calculating the power in matrices, vectors, and multidirectional arrays as well.

Example 1: How to Calculate the Power of a Scalar in MATLAB?

This MATLAB code computes the positive, negative, and fractional powers of the given number using the ^ operator.

num = 64;
ans1 = num^2
ans2 = num^(-2)
ans3 = num^(1/2)

Example 2: How to Calculate the Power of a Square Matrix in MATLAB?

In this example, we calculate the positive, negative, fractional, and element-wise powers of the given square matrix.

A = magic(2);
ans1 = A^3
ans2 = A^(-3)
ans3 = A^(1/3)
ans4 = A.^3

2: How to Use Exponentials in MATLAB

Taking exponentials is another common task widely used in MATLAB programming. We can calculate the exponentials of a scalar, vector, matrix, or multidimensional array using the built-in exp() function. This function takes a number or an array as an input argument and provides its calculated exponential in return.

Syntax

In MATLAB, you can use the exp() function in the following way:

exp(X)

Example 1: How to Calculate the Exponential of a Scalar in MATLAB?

In this MATLAB code, we use the exp() function to compute the exponential of the given number.

num = 7.9;
exp(num)

Example 2: How to Calculate the Exponential of an Array in MATLAB?

In this example, we calculate the exponential of the given array using the exp() function in MATLAB.

X = randi(100,4,7,2);
exp(X)

For more details in our guide; how to use the exp() function to find exponential in MATLAB.

Conclusion

MATLAB is a beneficial programming tool that allows us to perform many tasks. It enables us to compute the powers and exponentials of a scalar or an array using the ^ operator and exp() function, respectively. This guide has provided beginner-level instruction to help users calculate the powers and exponentials in MATLAB for a matrix, vectors, or multi-dimensional arrays.

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.