Gaussian Elimination Calculator
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f8f9fa;
color: #333;
line-height: 1.6;
margin: 0;
padding: 20px;
}
.calculator-container {
max-width: 800px;
margin: 20px auto;
background-color: #ffffff;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
h1, h2 {
color: #004a99;
text-align: center;
margin-bottom: 20px;
}
.input-section, .output-section, .article-section {
margin-bottom: 30px;
padding: 20px;
border: 1px solid #dee2e6;
border-radius: 5px;
background-color: #fdfdfd;
}
.input-group {
margin-bottom: 15px;
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 10px;
}
.input-group label {
flex: 1 1 150px; /* Grow, shrink, basis */
font-weight: bold;
color: #004a99;
min-width: 120px;
}
.input-group input[type="text"],
.input-group select {
flex: 2 1 200px; /* Grow, shrink, basis */
padding: 10px 12px;
border: 1px solid #ced4da;
border-radius: 4px;
box-sizing: border-box;
font-size: 1rem;
}
button {
display: block;
width: 100%;
padding: 12px 20px;
background-color: #004a99;
color: white;
border: none;
border-radius: 5px;
font-size: 1.1rem;
cursor: pointer;
transition: background-color 0.3s ease;
margin-top: 10px;
}
button:hover {
background-color: #003b7f;
}
#result {
margin-top: 20px;
padding: 15px;
background-color: #e9ecef;
border-left: 5px solid #28a745;
font-size: 1.2rem;
font-weight: bold;
text-align: center;
border-radius: 5px;
word-break: break-all; /* For matrix display */
}
#result pre {
margin: 0;
padding: 0;
text-align: left; /* For better matrix readability */
white-space: pre-wrap; /* Wrap long lines */
word-wrap: break-word;
}
.article-section h2 {
text-align: left;
color: #004a99;
margin-bottom: 15px;
}
.article-section p, .article-section ul, .article-section li {
margin-bottom: 15px;
}
.article-section code {
background-color: #e9ecef;
padding: 2px 6px;
border-radius: 3px;
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
}
@media (max-width: 600px) {
.input-group {
flex-direction: column;
align-items: stretch;
}
.input-group label {
flex-basis: auto;
margin-bottom: 5px;
}
.input-group input[type="text"],
.input-group select {
flex-basis: auto;
width: 100%;
}
}
Gaussian Elimination Calculator
Solution
Enter your matrix and click 'Solve System'.
What is Gaussian Elimination?
Gaussian elimination, also known as the row reduction method, is a fundamental algorithm in linear algebra used to solve systems of linear equations, find the rank of a matrix, and calculate the inverse of a matrix. The core idea is to transform the system's augmented matrix into an equivalent form, called row echelon form or reduced row echelon form, using a sequence of elementary row operations. This transformed matrix allows for a much simpler method of solving the system, typically back-substitution.
Elementary Row Operations:
- Swapping two rows: Interchanging the position of two equations.
- Multiplying a row by a non-zero scalar: Multiplying an entire equation by a constant.
- Adding a multiple of one row to another row: Adding a scaled version of one equation to another equation.
The Process:
The algorithm proceeds column by column. For each column, the goal is to:
- Pivoting: Ensure that the element on the main diagonal (the pivot element) is non-zero. If it is zero, swap the current row with a row below it that has a non-zero element in that column.
- Normalization (Optional for Row Echelon Form, necessary for Reduced Row Echelon Form): Divide the pivot row by the pivot element to make the pivot element equal to 1.
- Elimination: Use the pivot row to make all other elements in the current column zero. This is done by adding appropriate multiples of the pivot row to the other rows.
After applying these steps to all columns, the matrix is in row echelon form. If the goal is to find the unique solution (for square systems with non-zero determinant), further steps can transform the matrix into reduced row echelon form, where each pivot is 1 and all other elements in the pivot column are 0, directly yielding the solution.
Use Cases:
- Solving systems of linear equations (e.g., in physics, engineering, economics).
- Finding the rank of a matrix.
- Calculating the inverse of a matrix.
- Determining if a matrix is invertible.
- Solving least squares problems.
function calculateGaussianElimination() {
var matrixInput = document.getElementById("matrixInput").value;
var resultDiv = document.getElementById("result");
resultDiv.innerHTML = ""; // Clear previous results
try {
var rows = matrixInput.split(';');
var matrix = [];
var numCols = 0;
for (var i = 0; i < rows.length; i++) {
var rowValues = rows[i].split(',');
if (i === 0) {
numCols = rowValues.length;
} else if (rowValues.length !== numCols) {
throw new Error("Inconsistent number of elements in rows.");
}
var row = [];
for (var j = 0; j < rowValues.length; j++) {
var value = parseFloat(rowValues[j]);
if (isNaN(value)) {
throw new Error("Invalid number entered: " + rowValues[j]);
}
row.push(value);
}
matrix.push(row);
}
var numRows = matrix.length;
if (numRows === 0 || numCols === 0) {
throw new Error("Matrix cannot be empty.");
}
if (numCols !== numRows + 1) {
console.warn("Warning: Matrix is not in the standard augmented form (n x n+1) for a unique solution. The system may have no solution, infinite solutions, or the result might need careful interpretation.");
}
var augmentedMatrix = matrix.map(function(row) { return row.slice(); }); // Deep copy
var solution = gaussianElimination(augmentedMatrix);
if (typeof solution === 'string') {
resultDiv.innerHTML = "" + solution + "";
} else {
resultDiv.innerHTML = "" + formatMatrix(solution) + "";
}
} catch (error) {
resultDiv.innerHTML = "
" + error.message + "";
}
}
function gaussianElimination(matrix) {
var numRows = matrix.length;
var numCols = matrix[0].length;
var lead = 0;
for (var r = 0; r < numRows; r++) {
if (numCols == lead) {
return "Matrix is singular or has issues.";
}
var i = r;
while (matrix[i][lead] === 0) {
i++;
if (i == numRows) {
i = r;
lead++;
if (numCols == lead) {
return "Matrix is singular or has issues.";
}
}
}
// Swap rows i and r
var temp = matrix[i];
matrix[i] = matrix[r];
matrix[r] = temp;
var val = matrix[r][lead];
if (val !== 0) {
// Normalize row r
for (var j = 0; j < numCols; j++) {
matrix[r][j] /= val;
}
}
// Eliminate other rows
for (var i = 0; i < numRows; i++) {
if (i !== r) {
var factor = matrix[i][lead];
for (var j = 0; j < numCols; j++) {
matrix[i][j] -= factor * matrix[r][j];
}
}
}
lead++;
}
return matrix; // Return the reduced row echelon form
}
function formatMatrix(matrix) {
var numRows = matrix.length;
var numCols = matrix[0].length;
var output = "";
for (var i = 0; i < numRows; i++) {
output += "[ ";
for (var j = 0; j < numCols; j++) {
// Format numbers to a reasonable precision
var formattedNum = matrix[i][j].toFixed(4);
// Remove trailing zeros after decimal point for cleaner look
formattedNum = formattedNum.replace(/\.0000$/, '').replace(/(\.\d*?[1-9])0+$/, '$1');
output += formattedNum + (j < numCols – 1 ? ", " : "");
}
output += " ]\n";
}
// Attempt to interpret the solution
var isUniqueSolution = true;
var uniqueSolution = [];
for (var i = 0; i < numRows; i++) {
var pivotCol = -1;
for (var j = 0; j colIdx === j || Math.abs(val) < 1e-9)) {
pivotCol = j;
break;
}
}
if (pivotCol !== -1) {
uniqueSolution[pivotCol] = matrix[i][numCols – 1];
} else {
// Check for inconsistency (0 = non-zero)
var allZeroCoeffs = true;
for (var j = 0; j 1e-9) {
allZeroCoeffs = false;
break;
}
}
if (allZeroCoeffs && Math.abs(matrix[i][numCols – 1]) > 1e-9) {
return output + "\nSystem has NO SOLUTION (Inconsistent equation detected).";
}
isUniqueSolution = false; // Could indicate infinite solutions or needs variable assignment
}
}
if (isUniqueSolution && uniqueSolution.length === numCols – 1) {
output += "\n\nUnique Solution:\n";
for (var i = 0; i < uniqueSolution.length; i++) {
output += "x" + (i + 1) + " = " + uniqueSolution[i].toFixed(4).replace(/\.0000$/, '').replace(/(\.\d*?[1-9])0+$/, '$1') + "\n";
}
} else if (!isUniqueSolution) {
output += "\n\nPossible Infinite Solutions or Dependent System. Further analysis required based on the Reduced Row Echelon Form.";
}
return output;
}