Czy wolisz polską wersję strony elektroda?
Nie, dziękuję Przekieruj mnie tamHow to compute inverse matrix in MATLAB
To compute the inverse of a matrix in MATLAB, use:
A_inv = inv(A);
Example:
A = [4 7; 2 6];
A_inv = inv(A);
Key points
A must be square and nonsingular.inv(A)*b; use:
x = A \ b;A_pinv = pinv(A);Matrix inversion in MATLAB is straightforward syntactically, but there is an important engineering distinction between:
These are not the same computational problem in practice.
inv()If you truly need the inverse matrix itself, MATLAB provides the built-in function:
A_inv = inv(A);
Example
A = [1 2; 3 4];
A_inv = inv(A)
You can verify the result by multiplying:
A * A_inv
A_inv * A
Ideally, both should equal the identity matrix:
eye(size(A))
Because MATLAB uses floating-point arithmetic, the result is usually close to the identity matrix, not mathematically exact.
inv() is often not the best practical choiceIn engineering workflows, especially in:
the usual task is to solve:
\[ Ax = b \]
Although mathematically one may write:
\[ x = A^{-1}b \]
in MATLAB the recommended method is:
x = A \ b;
This is preferred because it is:
Do this
x = A \ b;
Avoid this
x = inv(A) * b;
A matrix can be inverted only if it is:
You can check the matrix size:
[m,n] = size(A);
if m ~= n
error('Matrix must be square.');
end
A common mistake is using only the determinant to decide invertibility. In numerical computing, this is not very robust. Better diagnostics are:
r = rcond(A);
c = cond(A);
Interpretation:
rcond(A) close to zero: matrix is nearly singular.cond(A) very large: inversion may be numerically unreliable.Example
A = [1 2; 2 4];
rcond(A)
cond(A)
This matrix is singular because the second row is a multiple of the first.
pinv()If A is not invertible in the ordinary sense, use the Moore-Penrose pseudoinverse:
A_pinv = pinv(A);
Example
A = [1 2 3; 4 5 6];
A_pinv = pinv(A);
This is especially useful for:
Another frequent confusion:
A.^-1
or
1 ./ A
computes the reciprocal of each element, not the matrix inverse.
Example
A = [2 4; 5 10];
elemwise = 1 ./ A % element-wise reciprocal
A_inv = inv(A) % matrix inverse
These are entirely different operations.
You may also see:
A_inv = A^(-1);
This is equivalent to inv(A) for a square matrix, but inv(A) is usually clearer and more readable.
A reliable MATLAB workflow is:
A = [4 7; 2 6];
% Check size
[m,n] = size(A);
if m ~= n
error('Matrix must be square.');
end
% Check conditioning
if rcond(A) < 1e-12
warning('Matrix is singular or nearly singular.');
end
% Compute inverse
A_inv = inv(A);
% Verify
err = norm(A*A_inv - eye(size(A)), 'fro');
disp(A_inv)
fprintf('Verification error = %.3e\n', err);
This is better engineering practice than calling inv() blindly.
Current MATLAB usage guidance strongly favors the following pattern:
inv(A) only when you explicitly need the inverse matrix.A\b for solving systems.pinv(A) for singular or rectangular matrices.pageinv() when working with batches of matrices.Industry trend in numerical computing is to avoid explicit inversion whenever possible, because decomposition-based solvers are more stable and scalable.
In practical electronics and control applications:
all typically rely on factorization or linear solvers rather than explicit matrix inversion.
A = [4 7; 2 6];
A_inv = inv(A)
Expected result:
A_inv =
0.6000 -0.7000
-0.2000 0.4000
A = [4 7; 2 6];
b = [1; 0];
x = A \ b
This is better than:
x = inv(A) * b
A = [1 2 3; 4 5 6];
A_pinv = pinv(A)
If you use the Symbolic Math Toolbox:
syms a b c d
A = [a b; c d];
A_inv = inv(A)
This gives an algebraic expression rather than a floating-point numerical matrix.
For this topic, ethical and legal issues are limited, but a few engineering-quality considerations matter:
If inversion is used inside firmware, simulation, or embedded code generation, numerical robustness becomes a safety and compliance issue.
inv(A) only if you truly need the inverse matrix itself.A\b for solving \(Ax=b\).rcond(A) or cond(A) before inversion.pinv(A) for singular or rectangular matrices.norm(A*inv(A) - eye(size(A)))det(A) alone is not a reliable practical test for invertibility in floating-point computation.inv() may produce warnings or unreliable values for singular or nearly singular matrices.1./A, not inv(A).If you want to go beyond the basics, useful next topics are:
lu(A)qr(A)svd(A)cond(A), rcond(A)For electronics-related applications, it is especially useful to study:
To compute an inverse matrix in MATLAB, use:
A_inv = inv(A);
However:
A\b is usually the better choice for solving linear systems,pinv(A) is appropriate for singular or rectangular matrices,rcond(A) or cond(A) should be checked for numerical reliability.If you want, I can also give you:
inv(A) and A\b with output.