Matrix Solution Calculator (2×2 System Solver)
Solve a system of linear equations of the form: ax + by = c
Understanding Matrix Solutions via Cramer's Rule
A system of linear equations can be represented as a matrix equation AX = B. When solving a 2×2 system, we use the coefficients of the variables to create the coefficient matrix (A) and use its determinant to find the values of unknowns (x and y).
How the Calculation Works
This calculator uses Cramer's Rule, which follows these mathematical steps:
- Find the Determinant (D): Calculate (a1 * b2) – (a2 * b1). If D = 0, the system may have no solution or infinitely many solutions.
- Find the X-Determinant (Dx): Replace the 'x' column with the constants (c1, c2) and calculate the determinant.
- Find the Y-Determinant (Dy): Replace the 'y' column with the constants (c1, c2) and calculate the determinant.
- Calculate Variables: x = Dx / D and y = Dy / D.
Example Calculation
Consider the system:
1x + 1y = 5
2x – 1y = 4
- D = (1 * -1) – (2 * 1) = -1 – 2 = -3
- Dx = (5 * -1) – (4 * 1) = -5 – 4 = -9
- Dy = (1 * 4) – (2 * 5) = 4 – 10 = -6
- x = -9 / -3 = 3
- y = -6 / -3 = 2
When is a Matrix Unsolvable?
A unique solution only exists if the determinant (D) is not equal to zero. If D = 0, the lines represented by the equations are either parallel (no solution) or overlapping (infinitely many solutions).
function solveMatrix() {
var a1 = parseFloat(document.getElementById('a1').value);
var b1 = parseFloat(document.getElementById('b1').value);
var c1 = parseFloat(document.getElementById('c1').value);
var a2 = parseFloat(document.getElementById('a2').value);
var b2 = parseFloat(document.getElementById('b2').value);
var c2 = parseFloat(document.getElementById('c2').value);
var outputDiv = document.getElementById('solution-output');
var stepsDiv = document.getElementById('steps-content');
var resultDiv = document.getElementById('final-result');
if (isNaN(a1) || isNaN(b1) || isNaN(c1) || isNaN(a2) || isNaN(b2) || isNaN(c2)) {
alert("Please enter valid numerical values for all coefficients.");
return;
}
// Calculate Determinants
var detMain = (a1 * b2) – (a2 * b1);
var detX = (c1 * b2) – (c2 * b1);
var detY = (a1 * c2) – (a2 * c1);
outputDiv.style.display = 'block';
var stepsHtml = "
1. Main Determinant (D): (" + a1 + " * " + b2 + ") – (" + a2 + " * " + b1 + ") = " + detMain + "";
if (detMain === 0) {
stepsHtml += "
Determinant is zero!";
if (detX === 0 && detY === 0) {
resultDiv.innerHTML = "Result: Infinitely Many Solutions (Dependent System)";
resultDiv.style.color = "#f39c12";
} else {
resultDiv.innerHTML = "Result: No Unique Solution (Inconsistent System)";
resultDiv.style.color = "#e74c3c";
}
} else {
var x = detX / detMain;
var y = detY / detMain;
stepsHtml += "
2. Determinant X (Dx): (" + c1 + " * " + b2 + ") – (" + c2 + " * " + b1 + ") = " + detX + "";
stepsHtml += "
3. Determinant Y (Dy): (" + a1 + " * " + c2 + ") – (" + a2 + " * " + c1 + ") = " + detY + "";
stepsHtml += "
4. Calculating Unknowns:x = Dx / D = " + detX + " / " + detMain + "y = Dy / D = " + detY + " / " + detMain + "";
resultDiv.innerHTML = "Solution Found: x = " + Number(x.toFixed(4)) + ", y = " + Number(y.toFixed(4));
resultDiv.style.color = "#27ae60";
}
stepsDiv.innerHTML = stepsHtml;
outputDiv.scrollIntoView({ behavior: 'smooth' });
}
function resetCalculator() {
document.getElementById('a1').value = "1";
document.getElementById('b1').value = "1";
document.getElementById('c1').value = "5";
document.getElementById('a2').value = "2";
document.getElementById('b2').value = "-1";
document.getElementById('c2').value = "4";
document.getElementById('solution-output').style.display = 'none';
}