System of Equations Matrix Solver
Coefficient Matrix (A) and Constant Vector (B)
Enter the coefficients for your system of linear equations. The system is represented as Ax = B.
Solution:
"; if (n === 2) { solutionHTML += "x = " + X[0].toFixed(6) + ""; solutionHTML += "y = " + X[1].toFixed(6) + ""; } else { // n === 3 solutionHTML += "x = " + X[0].toFixed(6) + ""; solutionHTML += "y = " + X[1].toFixed(6) + ""; solutionHTML += "z = " + X[2].toFixed(6) + ""; } resultDiv.innerHTML = solutionHTML; } // Function to calculate determinant function calculateDeterminant(matrix, n) { if (n === 2) { return matrix[0][0] * matrix[1][1] – matrix[0][1] * matrix[1][0]; } else if (n === 3) { return matrix[0][0] * (matrix[1][1] * matrix[2][2] – matrix[1][2] * matrix[2][1]) – matrix[0][1] * (matrix[1][0] * matrix[2][2] – matrix[1][2] * matrix[2][0]) + matrix[0][2] * (matrix[1][0] * matrix[2][1] – matrix[1][1] * matrix[2][0]); } return 0; // Should not happen with n=2 or n=3 } // Function to calculate inverse matrix function calculateInverse(matrix, n, det) { var inv = []; if (n === 2) { inv = [ [matrix[1][1] / det, -matrix[0][1] / det], [-matrix[1][0] / det, matrix[0][0] / det] ]; } else if (n === 3) { var c = []; // Cofactor matrix c[0] = [ (matrix[1][1] * matrix[2][2] – matrix[1][2] * matrix[2][1]), -(matrix[1][0] * matrix[2][2] – matrix[1][2] * matrix[2][0]), (matrix[1][0] * matrix[2][1] – matrix[1][1] * matrix[2][0]) ]; c[1] = [ -(matrix[0][1] * matrix[2][2] – matrix[0][2] * matrix[2][1]), (matrix[0][0] * matrix[2][2] – matrix[0][2] * matrix[2][0]), -(matrix[0][0] * matrix[2][1] – matrix[0][1] * matrix[2][0]) ]; c[2] = [ (matrix[0][1] * matrix[1][2] – matrix[0][2] * matrix[1][1]), -(matrix[0][0] * matrix[1][2] – matrix[0][2] * matrix[1][0]), (matrix[0][0] * matrix[1][1] – matrix[0][1] * matrix[1][0]) ]; // Inverse is (1/det) * adjugate (transpose of cofactor matrix) inv = []; for (var i = 0; i < n; i++) { inv[i] = []; for (var j = 0; j < n; j++) { inv[i][j] = c[j][i] / det; // Transpose: c[j][i] instead of c[i][j] } } } return inv; } // Function to multiply a matrix by a vector function multiplyMatrixVector(matrix, vector, n) { var result = []; for (var i = 0; i < n; i++) { result[i] = 0; for (var j = 0; j < n; j++) { result[i] += matrix[i][j] * vector[j]; } } return result; }Understanding Systems of Linear Equations and Matrix Solutions
A system of linear equations is a collection of two or more linear equations involving the same set of variables. For example, a 2×2 system might look like:
2x + y = 10
x + 3y = 15
Solving such a system means finding the values for the variables (x, y, and potentially z for a 3×3 system) that satisfy all equations simultaneously.
The Matrix Approach: Ax = B
Matrices provide a powerful and systematic way to represent and solve systems of linear equations. Any system of linear equations can be written in the matrix form:
Ax = B
- A is the coefficient matrix, containing the numerical coefficients of the variables.
- x is the variable vector, containing the variables (e.g., [x, y] or [x, y, z]).
- B is the constant vector, containing the constants on the right-hand side of the equations.
For the 2×2 example above:
A = [[2, 1],
[1, 3]]
x = [x, y]
B = [10, 15]
How the Calculator Solves the System
This calculator uses the inverse matrix method to find the solution. If the coefficient matrix A is invertible (meaning its determinant is not zero), then the solution vector x can be found by:
x = A⁻¹B
Where A⁻¹ is the inverse of matrix A.
- Input Collection: You enter the coefficients of your equations into the matrix A and the constants into vector B.
- Determinant Calculation: The calculator first computes the determinant of matrix A. If the determinant is zero, the matrix is singular, and a unique solution does not exist (the system either has no solution or infinitely many solutions).
- Inverse Matrix Calculation: If the determinant is non-zero, the calculator proceeds to compute the inverse of matrix A (A⁻¹). For 2×2 matrices, this is straightforward. For 3×3 matrices, it involves calculating the cofactor matrix, transposing it to get the adjugate matrix, and then dividing by the determinant.
- Matrix-Vector Multiplication: Finally, the inverse matrix A⁻¹ is multiplied by the constant vector B to yield the solution vector x, which contains the values for your variables.
Example Usage:
2×2 System:
Consider the system:
2x + y = 10
x + 3y = 15
Input these values into the calculator:
- a11 = 2, a12 = 1, b1 = 10
- a21 = 1, a22 = 3, b2 = 15
The calculator will output:
x = 3.000000
y = 4.000000
You can verify this: 2(3) + 4 = 6 + 4 = 10, and 3 + 3(4) = 3 + 12 = 15.
3×3 System:
Consider the system:
2x + y + z = 10
x + 3y + 2z = 15
x + 2y + 3z = 20
Input these values into the calculator:
- a11 = 2, a12 = 1, a13 = 1, b1 = 10
- a21 = 1, a22 = 3, a23 = 2, b2 = 15
- a31 = 1, a32 = 2, a33 = 3, b3 = 20
The calculator will output:
x = 1.250000
y = 2.500000
z = 5.000000
This calculator is a useful tool for students, engineers, and anyone needing to quickly solve systems of linear equations without manual calculation.