Solve System of Equations Matrix Calculator

System of Equations Matrix Solver

2×2 3×3

Coefficient Matrix (A) and Constant Vector (B)

Enter the coefficients for your system of linear equations. The system is represented as Ax = B.

x + y + z =
x + y + z =
x + y + z =
.calculator-container { font-family: Arial, sans-serif; max-width: 600px; margin: 20px auto; padding: 20px; border: 1px solid #ccc; border-radius: 8px; background-color: #f9f9f9; } .input-group, .matrix-inputs { margin-bottom: 15px; } .input-group label { display: block; margin-bottom: 5px; font-weight: bold; } .matrix-row { display: flex; align-items: center; margin-bottom: 10px; } .matrix-row input[type="number"] { width: 60px; padding: 8px; margin: 0 5px; border: 1px solid #ddd; border-radius: 4px; text-align: center; } .matrix-row span { margin: 0 5px; } button { background-color: #007bff; color: white; padding: 10px 15px; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; } button:hover { background-color: #0056b3; } .calculator-result { margin-top: 20px; padding: 15px; border: 1px solid #e0e0e0; border-radius: 5px; background-color: #e9ecef; min-height: 50px; } .calculator-result p { margin: 5px 0; font-size: 1.1em; } .error { color: red; font-weight: bold; } .matrix-3×3-only { display: none; /* Hidden by default, controlled by JS */ } // Helper function to parse float and handle NaN function parseFloatOrDefault(value, defaultValue) { var parsed = parseFloat(value); return isNaN(parsed) ? defaultValue : parsed; } // Function to toggle input fields based on matrix size function toggleMatrixInputs() { var size = document.getElementById("matrixSize").value; var elements3x3 = document.getElementsByClassName("matrix-3×3-only"); for (var i = 0; i < elements3x3.length; i++) { elements3x3[i].style.display = (size === "3×3") ? "flex" : "none"; } // Set default values for 2×2 if switching from 3×3 if (size === "2×2") { document.getElementById("a11").value = "2"; document.getElementById("a12").value = "1"; document.getElementById("b1").value = "10"; document.getElementById("a21").value = "1"; document.getElementById("a22").value = "3"; document.getElementById("b2").value = "15"; } else { // 3×3 document.getElementById("a11").value = "2"; document.getElementById("a12").value = "1"; document.getElementById("a13").value = "1"; document.getElementById("b1").value = "10"; document.getElementById("a21").value = "1"; document.getElementById("a22").value = "3"; document.getElementById("a23").value = "2"; document.getElementById("b2").value = "15"; document.getElementById("a31").value = "1"; document.getElementById("a32").value = "2"; document.getElementById("a33").value = "3"; document.getElementById("b3").value = "20"; } } // Initial call to set up inputs correctly on page load window.onload = function() { toggleMatrixInputs(); }; // Main function to solve the system function solveSystem() { var resultDiv = document.getElementById("result"); resultDiv.innerHTML = ""; // Clear previous results var size = document.getElementById("matrixSize").value; var n = (size === "2×2") ? 2 : 3; var A = []; var B = []; // Read matrix A and vector B try { for (var i = 0; i < n; i++) { A[i] = []; for (var j = 0; j < n; j++) { var val = parseFloatOrDefault(document.getElementById("a" + (i + 1) + (j + 1)).value, NaN); if (isNaN(val)) throw new Error("Please enter valid numbers for all matrix coefficients."); A[i][j] = val; } var bVal = parseFloatOrDefault(document.getElementById("b" + (i + 1)).value, NaN); if (isNaN(bVal)) throw new Error("Please enter valid numbers for all constant values."); B[i] = bVal; } } catch (error) { resultDiv.innerHTML = "" + error.message + ""; return; } var detA = calculateDeterminant(A, n); if (Math.abs(detA) < 1e-9) { // Check if determinant is close to zero resultDiv.innerHTML = "The determinant of the coefficient matrix is zero or very close to zero. This system either has no unique solution or infinitely many solutions (or is ill-conditioned)."; return; } var invA = calculateInverse(A, n, detA); var X = multiplyMatrixVector(invA, B, n); var solutionHTML = "

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.

  1. Input Collection: You enter the coefficients of your equations into the matrix A and the constants into vector B.
  2. 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).
  3. 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.
  4. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *