-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Gauss-Jordan Elimination Calculator
- Loading branch information
1 parent
060db21
commit 8a55bd3
Showing
3 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |