This topic shows how to compute matrix powers and exponentials using a variety of methods.

If `A`

is a square matrix and `p`

is a positive integer, then `A^p`

effectively multiplies `A`

by itself `p-1`

times. For example:

A = [1 1 1 1 2 3 1 3 6]; A^2

`ans = `*3×3*
3 6 10
6 14 25
10 25 46

If `A`

is square and nonsingular, then `A^(-p)`

effectively multiplies `inv(A)`

by itself `p-1`

times.

A^(-3)

`ans = `*3×3*
145.0000 -207.0000 81.0000
-207.0000 298.0000 -117.0000
81.0000 -117.0000 46.0000

MATLAB® calculates `inv(A)`

and `A^(-1)`

with the same algorithm, so the results are exactly the same. Both `inv(A)`

and `A^(-1)`

produce warnings if the matrix is close to being singular.

isequal(inv(A),A^(-1))

`ans = `*logical*
1

Fractional powers, such as `A^(2/3)`

, are also permitted. The results using fractional powers depend on the distribution of the eigenvalues of the matrix.

A^(2/3)

`ans = `*3×3*
0.8901 0.5882 0.3684
0.5882 1.2035 1.3799
0.3684 1.3799 3.1167

The `.^`

operator calculates element-by-element powers. For example, to square each element in a matrix you can use `A.^2`

.

A.^2

`ans = `*3×3*
1 1 1
1 4 9
1 9 36

The `sqrt`

function is a convenient way to calculate the square root of each element in a matrix. An alternate way to do this is `A.^(1/2)`

.

sqrt(A)

`ans = `*3×3*
1.0000 1.0000 1.0000
1.0000 1.4142 1.7321
1.0000 1.7321 2.4495

For other roots, you can use `nthroot`

. For example, calculate `A.^(1/3)`

.

nthroot(A,3)

`ans = `*3×3*
1.0000 1.0000 1.0000
1.0000 1.2599 1.4422
1.0000 1.4422 1.8171

These element-wise roots differ from the matrix square root, which calculates a second matrix $\mathit{B}$ such that $\mathit{A}=\mathrm{BB}$. The function `sqrtm(A)`

computes `A^(1/2)`

by a more accurate algorithm. The `m`

in `sqrtm`

distinguishes this function from `sqrt(A)`

, which, like `A.^(1/2)`

, does its job element-by-element.

B = sqrtm(A)

`B = `*3×3*
0.8775 0.4387 0.1937
0.4387 1.0099 0.8874
0.1937 0.8874 2.2749

B^2

`ans = `*3×3*
1.0000 1.0000 1.0000
1.0000 2.0000 3.0000
1.0000 3.0000 6.0000

In addition to raising a matrix to a power, you also can raise a scalar to the power of a matrix.

2^A

`ans = `*3×3*
10.4630 21.6602 38.5862
21.6602 53.2807 94.6010
38.5862 94.6010 173.7734

When you raise a scalar to the power of a matrix, MATLAB uses the eigenvalues and eigenvectors of the matrix to calculate the matrix power. If `[V,D] = eig(A)`

, then ${2}^{\mathit{A}}={\mathit{V}\text{\hspace{0.17em}}\mathrm{2}}^{\mathit{D}}{\text{\hspace{0.17em}}\mathit{V}}^{-1}$.

[V,D] = eig(A); V*2^D*V^(-1)

`ans = `*3×3*
10.4630 21.6602 38.5862
21.6602 53.2807 94.6010
38.5862 94.6010 173.7734

The matrix exponential is a special case of raising a scalar to a matrix power. The base for a matrix exponential is Euler's number `e = exp(1)`

.

e = exp(1); e^A

ans =3×310^{3}× 0.1008 0.2407 0.4368 0.2407 0.5867 1.0654 0.4368 1.0654 1.9418

The `expm`

function is a more convenient way to calculate matrix exponentials.

expm(A)

ans =3×310^{3}× 0.1008 0.2407 0.4368 0.2407 0.5867 1.0654 0.4368 1.0654 1.9418

The matrix exponential can be calculated in a number of ways. See Matrix Exponentials for more information.

The MATLAB functions `log1p`

and `expm1`

calculate $\mathrm{log}\left(1+\mathit{x}\right)$ and ${\mathit{e}}^{\mathit{x}}-1$ accurately for very small values of $\mathit{x}$. For example, if you try to add a number smaller than machine precision to 1, then the result gets rounded to 1.

log(1+eps/2)

ans = 0

However, `log1p`

is able to return a more accurate answer.

log1p(eps/2)

ans = 1.1102e-16

Likewise for ${\mathit{e}}^{\mathit{x}}-1$, if $\mathit{x}$ is very small then it is rounded to zero.

exp(eps/2)-1

ans = 0

Again, `expm1`

is able to return a more accurate answer.

expm1(eps/2)

ans = 1.1102e-16

`exp`

| `expm`

| `expm1`

| `mpower`

| `nthroot`

| `power`

| `sqrt`

| `sqrtm`