0
0
MATLABdata~5 mins

Matrix inverse (inv) in MATLAB

Choose your learning style9 modes available
Introduction

Matrix inverse helps you find a matrix that "undoes" the effect of the original matrix when multiplied together. It is useful to solve equations and reverse transformations.

When you want to solve a system of linear equations like Ax = b.
When you need to reverse a transformation applied by a matrix.
When calculating coefficients in linear regression or other models.
When you want to find the matrix that reverses another matrix's effect.
Syntax
MATLAB
B = inv(A)

A must be a square matrix (same number of rows and columns).

If A is not invertible (singular), MATLAB will give a warning or error.

Examples
Calculate the inverse of a 2x2 matrix A.
MATLAB
A = [1 2; 3 4];
B = inv(A);
The inverse of the identity matrix is itself.
MATLAB
I = eye(3);
B = inv(I);
Inverse of a diagonal matrix is a diagonal matrix with reciprocal elements.
MATLAB
A = [2 0 0; 0 3 0; 0 0 4];
B = inv(A);
Sample Program

This program calculates the inverse of matrix A, then multiplies A by its inverse to show the identity matrix.

MATLAB
A = [4 7; 2 6];
B = inv(A);
disp('Matrix A:');
disp(A);
disp('Inverse of A:');
disp(B);
product = A * B;
disp('Product of A and its inverse (should be identity):');
disp(product);
OutputSuccess
Important Notes

Using inv is not always the best way to solve equations; sometimes using \ (backslash) operator is more efficient and accurate.

Only square matrices that are not singular have inverses.

Summary

The inv function finds the inverse of a square matrix.

Multiplying a matrix by its inverse gives the identity matrix.

Use inv carefully; for solving equations, prefer \.