In linear algebra, the inverse of a matrix is a matrix that, when multiplied by the original matrix, yields the identity matrix. Not all matrices have inverses; a matrix must be square (same number of rows and columns) and its determinant must not equal zero.
The 2×2 Inverse Formula
For a 2×2 matrix A defined as:
A = [ [a, b], [c, d] ]
The inverse matrix, denoted as A-1, is calculated using the following formula:
A-1 = (1 / det(A)) * [ [d, -b], [-c, a] ]
Where det(A) is the determinant: (a * d) – (b * c).
Step-by-Step Calculation Process
Calculate the Determinant: Multiply the top-left and bottom-right elements (ad) and subtract the product of the top-right and bottom-left elements (bc).
Check Invertibility: If the determinant is 0, the matrix is "singular" and has no inverse.
Swap and Negate: Swap the positions of a and d, and change the signs of b and c.
Scalar Multiplication: Multiply every element in the new matrix by 1 divided by the determinant.
Example Calculation
Suppose you have the matrix:
a = 4, b = 7 c = 2, d = 6
Determinant: (4 * 6) – (7 * 2) = 24 – 14 = 10.
Swap/Negate: The matrix becomes [ [6, -7], [-2, 4] ].
Final Inverse: Multiply by 1/10:
[ [0.6, -0.7], [-0.2, 0.4] ]
function calculateInverse() {
var a = parseFloat(document.getElementById('cell_a').value);
var b = parseFloat(document.getElementById('cell_b').value);
var c = parseFloat(document.getElementById('cell_c').value);
var d = parseFloat(document.getElementById('cell_d').value);
var resultBox = document.getElementById('matrix-result-box');
var errorBox = document.getElementById('matrix-error-box');
var matrixDisplay = document.getElementById('inverse-matrix-display');
var detOutput = document.getElementById('determinant-output');
// Reset display
resultBox.style.display = 'none';
errorBox.style.display = 'none';
matrixDisplay.innerHTML = ";
// Validate inputs
if (isNaN(a) || isNaN(b) || isNaN(c) || isNaN(d)) {
errorBox.innerHTML = "Error: Please enter valid numbers in all four fields.";
errorBox.style.display = 'block';
return;
}
// Step 1: Calculate Determinant
var determinant = (a * d) – (b * c);
if (determinant === 0) {
errorBox.innerHTML = "Error: The determinant is 0. This matrix is singular and does not have an inverse.";
errorBox.style.display = 'block';
} else {
// Step 2 & 3: Swap and multiply by 1/det
var inv_a = d / determinant;
var inv_b = -b / determinant;
var inv_c = -c / determinant;
var inv_d = a / determinant;
// Formatting numbers to 4 decimal places if necessary
function formatVal(num) {
return Number.isInteger(num) ? num : parseFloat(num.toFixed(4));
}
detOutput.innerHTML = "Determinant (det A) = " + formatVal(determinant);
var cells = [inv_a, inv_b, inv_c, inv_d];
for (var i = 0; i < cells.length; i++) {
var cellDiv = document.createElement('div');
cellDiv.style.border = '1px solid #ccc';
cellDiv.style.padding = '10px';
cellDiv.style.textAlign = 'center';
cellDiv.style.minWidth = '60px';
cellDiv.style.background = '#fff';
cellDiv.innerText = formatVal(cells[i]);
matrixDisplay.appendChild(cellDiv);
}
resultBox.style.display = 'block';
}
}