Skip to content

Commit

Permalink
Added Gauss-Jordan Elimination Calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
PDBharadwaj committed Aug 6, 2024
1 parent 060db21 commit 8a55bd3
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Calculators/Gauss-Jordan Calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gauss-Jordan Elimination Calculator</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Gauss-Jordan Elimination Calculator</h1>
<form id="matrixForm">
<label for="rows">Rows:</label>
<input type="number" id="rows" name="rows" min="1" required>
<label for="columns">Columns:</label>
<input type="number" id="columns" name="columns" min="1" required> <br/>
<button type="button" onclick="generateMatrix()">Generate Matrix</button>
<button type="button" onclick="resetMatrix()">Reset</button>
</form>
<div id="matrixContainer"></div>
<button id="solveButton" onclick="solveMatrix()" style="display:none;">Solve</button>
<div id="resultContainer"></div>
<script src="script.js"></script>
</body>
</html>
174 changes: 174 additions & 0 deletions Calculators/Gauss-Jordan Calculator/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
function generateMatrix() {
const rows = document.getElementById('rows').value;
const columns = document.getElementById('columns').value;
const matrixContainer = document.getElementById('matrixContainer');
matrixContainer.innerHTML = '';

for (let i = 0; i < rows; i++) {
const row = document.createElement('div');
for (let j = 0; j < columns; j++) {
const input = document.createElement('input');
input.type = 'number';
input.id = `matrix_${i}_${j}`;
input.required = true;
row.appendChild(input);
}
matrixContainer.appendChild(row);
}

document.getElementById('solveButton').style.display = 'block';
}

function resetMatrix() {
document.getElementById('matrixForm').reset();
document.getElementById('matrixContainer').innerHTML = '';
document.getElementById('resultContainer').innerHTML = '';
document.getElementById('solveButton').style.display = 'none';
}

function solveMatrix() {
const rows = document.getElementById('rows').value;
const columns = document.getElementById('columns').value;
let matrix = [];

for (let i = 0; i < rows; i++) {
matrix[i] = [];
for (let j = 0; j < columns; j++) {
matrix[i][j] = parseFloat(document.getElementById(`matrix_${i}_${j}`).value);
}
}

const result = gaussJordan(matrix);
displayResult(result);
}

function gaussJordan(matrix) {
const rows = matrix.length;
const columns = matrix[0].length;

for (let i = 0; i < rows; i++) {
let maxEl = Math.abs(matrix[i][i]);
let maxRow = i;
for (let k = i + 1; k < rows; k++) {
if (Math.abs(matrix[k][i]) > maxEl) {
maxEl = Math.abs(matrix[k][i]);
maxRow = k;
}
}

for (let k = i; k < columns; k++) {
let tmp = matrix[maxRow][k];
matrix[maxRow][k] = matrix[i][k];
matrix[i][k] = tmp;
}

for (let k = i + 1; k < rows; k++) {
let c = -matrix[k][i] / matrix[i][i];
for (let j = i; j < columns; j++) {
if (i === j) {
matrix[k][j] = 0;
} else {
matrix[k][j] += c * matrix[i][j];
}
}
}
}

for (let i = rows - 1; i >= 0; i--) {
for (let k = i - 1; k >= 0; k--) {
let c = -matrix[k][i] / matrix[i][i];
for (let j = 0; j < columns; j++) {
if (i === j) {
matrix[k][j] = 0;
} else {
matrix[k][j] += c * matrix[i][j];
}
}
}
}

for (let i = 0; i < rows; i++) {
let c = matrix[i][i];
for (let j = 0; j < columns; j++) {
matrix[i][j] = matrix[i][j] / c;
}
}

return matrix;
}

function displayResult(matrix) {
const resultContainer = document.getElementById('resultContainer');
resultContainer.innerHTML = '<h2>Result:</h2>';
matrix.forEach(row => {
const rowDiv = document.createElement('div');
row.forEach(value => {
const span = document.createElement('span');
span.textContent = `${value.toFixed(2)} `;
rowDiv.appendChild(span);
});
resultContainer.appendChild(rowDiv);
});

displaySolutions(matrix);
}

function displaySolutions(matrix) {
const resultContainer = document.getElementById('resultContainer');
const rows = matrix.length;
const columns = matrix[0].length;
let solutions = [];

for (let i = 0; i < rows; i++) {
let isZeroRow = true;
for (let j = 0; j < columns - 1; j++) {
if (matrix[i][j] !== 0) {
isZeroRow = false;
break;
}
}
if (isZeroRow && matrix[i][columns - 1] !== 0) {
resultContainer.innerHTML += '<p>No solution exists.</p>';
return;
}
}

for (let i = 0; i < columns - 1; i++) {
solutions[i] = 'Free variable';
}

for (let i = 0; i < rows; i++) {
for (let j = 0; j < columns - 1; j++) {
if (matrix[i][j] === 1) {
solutions[j] = matrix[i][columns - 1].toFixed(3);
break;
}
}
}

resultContainer.innerHTML += '<h2>Solutions:</h2>';
solutions.forEach((solution, index) => {
const solutionDiv = document.createElement('div');
solutionDiv.textContent = `x${index + 1} = ${solution}`;
resultContainer.appendChild(solutionDiv);
});
}

function fraction(num) {
let str = num.toString();
if (str.includes('.')) {
let len = str.split('.')[1].length;
let denominator = Math.pow(10, len);
let numerator = num * denominator;
let gcd = getGCD(numerator, denominator);
return `${numerator / gcd}/${denominator / gcd}`;
}
return num.toString();
}

function getGCD(a, b) {
if (!b) {
return a;
}
return getGCD(b, a % b);
}
31 changes: 31 additions & 0 deletions Calculators/Gauss-Jordan Calculator/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
body {
font-family: Arial, sans-serif;
margin: 20px;
text-align: center;
display: flex;
min-height: 100vh;
flex-direction: column;
align-items: center;
/* background-color: red; */
background-image: linear-gradient(to bottom right, rgb(247, 131, 151), rgb(255, 248, 197), rgb(0, 208, 255));
}

form, #matrixContainer, #resultContainer {
margin-top: 20px;
}

input[type="number"] {
width: 50px;
padding: 7px;
margin: 5px;
}

button {
margin-top: 10px;
padding: 5px 10px;
font-size: 16px;
border-radius: 20px;
border: 1px solid black;
color: rgb(255, 255, 255);
background-color: rgb(255, 129, 175);
}

0 comments on commit 8a55bd3

Please sign in to comment.