# How to Compute Condition Number in MATLAB with Examples

The condition number of a matrix is a measure of how sensitive its eigenvalues and eigenvectors are to small changes in the matrix. A high condition number means that small changes in the matrix can lead to large changes in its eigenvalues and eigenvectors, which can make it difficult to solve problems involving the matrix.

MATLAB provides a function called `cond` to compute the condition number of a matrix. The `cond` function takes two arguments: the matrix to be computed and the norm to be used. The norm can be one of the following:

• `2`: The 2-norm, which is the largest singular value of the matrix.
• `1`: The 1-norm, which is the sum of the absolute values of the elements of the matrix.
• `Inf`: The infinity norm, which is the largest absolute value of any element of the matrix.
• `fro`: The Frobenius norm, which is the square root of the sum of the squares of all the elements of the matrix.

For example, to compute the 2-norm condition number of the matrix `A`, you would use the following code:

#### Code snippet

``cond(A, 2)``

## What is Condition Number?

The condition number of a matrix is a measure of how sensitive its eigenvalues and eigenvectors are to changes in the matrix’s elements. A matrix with a high condition number is said to be “ill-conditioned,” meaning that even small changes in the matrix can cause large changes in its eigenvalues and eigenvectors. This can make it difficult to solve linear systems involving the matrix, or to perform other numerical operations on it.

## How MATLAB Computes Condition Number

MATLAB computes the condition number of a matrix using the following formula:

#### Code snippet

``````cond(A) = ||A|| * ||inv(A)||
``````

where:

• `A` is the matrix whose condition number is being computed
• `||A||` is the norm of matrix `A`
• `||inv(A)||` is the norm of the inverse of matrix `A`

The norm of a matrix can be any of the following:

• 1-norm: The sum of the absolute values of the elements in the matrix
• 2-norm: The square root of the sum of the squares of the elements in the matrix
• Infinity norm: The maximum absolute value of any element in the matrix

The choice of norm will affect the value of the condition number. In general, a matrix with a higher norm will have a higher condition number.

##### Examples:

Here are some examples of how to compute the condition number of a matrix in MATLAB:

#### Code snippet

``````A = [1 2; 3 4];

% Compute the 2-norm condition number
cond(A)

% Compute the 1-norm condition number
cond(A, 1)

% Compute the infinity norm condition number
cond(A, inf)
``````

The output of these commands will be the condition number of the matrix `A` in the specified norm.

##### Another Example:

The `cond` function in MATLAB uses the singular value decomposition (SVD) to compute the condition number of a matrix. The SVD decomposes a matrix into three matrices:

• The left singular matrix`U`, contains the eigenvectors of the matrix.
• The diagonal matrix of singular values`S`, contains the singular values of the matrix.
• The right singular matrix`V`, contains the corresponding eigenvalues of the matrix.

The condition number of the matrix is then given by the ratio of the largest singular value to the smallest singular value.

###### Examples:

Here are some examples of how to use the `cond` function in MATLAB:

#### Code snippet

``````A = rand(3, 3);

% Compute the 2-norm condition number of A
cond(A, 2)

% Compute the 1-norm condition number of A
cond(A, 1)

% Compute the Frobenius norm condition number of A
cond(A, 'fro')``````

## How to Use Condition Number

The condition number can be used to assess the stability of a matrix. A matrix with a high condition number is more likely to be unstable, meaning that small changes in the matrix can cause large changes in its eigenvalues and eigenvectors. This can make it difficult to solve linear systems involving the matrix, or to perform other numerical operations on it.

If you are working with a matrix that has a high condition number, you may need to take steps to improve its stability. This may involve using a different numerical method, or using a more robust algorithm.

## Conclusion:

The condition number is a useful tool for assessing the stability of a matrix. By understanding the condition number of a matrix, you can better understand how sensitive it is to changes in its elements. This can help you to avoid problems when working with matrices in MATLAB.

In addition to the information provided in this article, you can find more information about condition number in the MATLAB documentation. The documentation includes a detailed explanation of the condition number formula, as well as examples of how to compute the condition number of a matrix in MATLAB.

The `cond` function in MATLAB is a powerful tool for computing the condition number of a matrix. The condition number can be used to assess the sensitivity of a matrix to small changes, and can help to identify matrices that may be difficult to solve problems with.

• MATLAB documentation for the `cond` function: Mathworks