A system of linear equations is a collection of two or more linear equations involving the same set of variables. This calculator specifically solves for systems with two variables, usually denoted as x and y. In matrix form, this is represented as AX = B, where A is the coefficient matrix, X is the variable matrix, and B is the constant matrix.
How to Solve Using Cramer's Rule
The calculator uses Cramer's Rule, which provides a solution in terms of determinants. For a system:
a1x + b1y = c1
a2x + b2y = c2
Calculate the Determinant (D): D = (a1 * b2) – (a2 * b1)
Calculate Dx: Dx = (c1 * b2) – (c2 * b1)
Calculate Dy: Dy = (a1 * c2) – (a2 * c1)
Find x: x = Dx / D
Find y: y = Dy / D
Note: If the determinant (D) is zero, the system either has no solution or infinitely many solutions, as the lines are parallel or coincident.
Practical Example
Consider the system:
2x + 3y = 8
1x – 1y = -1
a1 = 2, b1 = 3, c1 = 8
a2 = 1, b2 = -1, c2 = -1
D: (2 * -1) – (1 * 3) = -2 – 3 = -5
Dx: (8 * -1) – (-1 * 3) = -8 + 3 = -5
Dy: (2 * -1) – (1 * 8) = -2 – 8 = -10
x: -5 / -5 = 1
y: -10 / -5 = 2
The solution is (1, 2).
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 resultContainer = document.getElementById('matrix-result-container');
var solutionOutput = document.getElementById('solution-output');
var stepsOutput = document.getElementById('steps-output');
if (isNaN(a1) || isNaN(b1) || isNaN(c1) || isNaN(a2) || isNaN(b2) || isNaN(c2)) {
alert("Please enter valid numbers in all fields.");
return;
}
// Cramer's Rule
var D = (a1 * b2) – (a2 * b1);
var Dx = (c1 * b2) – (c2 * b1);
var Dy = (a1 * c2) – (a2 * c1);
resultContainer.style.display = 'block';
if (D === 0) {
if (Dx === 0 && Dy === 0) {
solutionOutput.innerHTML = "Infinite Solutions";
solutionOutput.style.color = "#f39c12";
stepsOutput.innerHTML = "Determinant D = 0 and Dx/Dy = 0. The equations represent the same line.";
} else {
solutionOutput.innerHTML = "No Solution";
solutionOutput.style.color = "#e74c3c";
stepsOutput.innerHTML = "Determinant D = 0 but Dx or Dy is non-zero. The lines are parallel and never intersect.";
}
} else {
var x = Dx / D;
var y = Dy / D;
// Formatting results to 4 decimal places if not integer
var xDisp = Number.isInteger(x) ? x : x.toFixed(4);
var yDisp = Number.isInteger(y) ? y : y.toFixed(4);
solutionOutput.innerHTML = "x = " + xDisp + "y = " + yDisp;
solutionOutput.style.color = "#2ecc71";
stepsOutput.innerHTML = "Step-by-step calculation:" +
"Determinant (D) = (" + a1 + " * " + b2 + ") – (" + a2 + " * " + b1 + ") = " + D + "" +
"Dx = (" + c1 + " * " + b2 + ") – (" + c2 + " * " + b1 + ") = " + Dx + "" +
"Dy = (" + a1 + " * " + c2 + ") – (" + a2 + " * " + c1 + ") = " + Dy + "" +
"x = Dx / D = " + Dx + " / " + D + " = " + xDisp + "" +
"y = Dy / D = " + Dy + " / " + D + " = " + yDisp;
}
// Scroll to result
resultContainer.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}