Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone please create a very basic javascript program of the Naive Gauss Eli

ID: 3831232 • Letter: C

Question

Can someone please create a very basic javascript program of the Naive Gauss Elimination and Gauss-Jordan using the following instructions:

As a personal preference I don't like the idea of Gauss elimination with back substitution. So I'm not going to make any of you go through that pain either.

basically I want you to write a program that takes a set of linear equations like this:

5w+2x+2y+3z = 15

-11w-3x+5y-7z = -2

2w+4x-6y = -21

w+2y+2z = 4

Although I will probably not give you any more than ten equations in ten unknowns your program has to be able to solve any simultaneous set of linear equations.

start the program out by giving it the coefficient matrix

c=[[5,2,2,3,15],[-11,-3,5,-7,-2],[2,4,-6,0,-21],[1,0,2,2,4]]

and have it print out the solution vector in the form:

[5, -2.5, 3.5, -4]

remember to try testing the program with various input.

for example, what if you wanted to solve:

3x=12

input

[[3,12]]

output

[4]

Also, the program that is created shall be saved as an ".html" which can be opened with a browser to yield an ouput per instructions given.

Explanation / Answer

Gauss-Jordan:-

var array = [[2, 1, -1, 8],
[-3, -1, 2, -11],
[-2, 1, 2, -3]];

var numRows = array.length;
var numCols = array[0].length; // very bad wayy of getting number of columns

for (var i = 0; i < numRows; i++) {
var constantRow = array[i];
for (var j = i + 1; j < numRows; j++) {
var currentRow = array[j];
var rowMultiplier = -constantRow[i] / currentRow[i]; // handle the case where denominator is 0
for (var k = i; k < currentRow.length; k++) {
currentRow[k] = constantRow[k] + currentRow[k] * rowMultiplier;
}
}
}

for (var i = numRows - 1; i > -1 ; i--) {
var constantRow = array[i];
for (var j = i - 1; j > -1; j--) {
var currentRow = array[j];
var rowMultiplier = -constantRow[i] / currentRow[i]; // handle the case where denominator is 0
for (var k = 0; k < currentRow.length; k++) {
currentRow[k] = constantRow[k] + currentRow[k] * rowMultiplier;
}
}
}

for (var g = 0; g < numRows; g++) {
console.log(array[g][numCols - 1] / array[g][g]);
}

Naive Gauss Elimination :

function gauss(A) {
var n = A.length;

for (var i=0; i<n; i++) {
// Search for maximum in this column
var maxEl = Math.abs(A[i][i]);
var maxRow = i;
for(var k=i+1; k<n; k++) {
if (Math.abs(A[k][i]) > maxEl) {
maxEl = Math.abs(A[k][i]);
maxRow = k;
}
}

// Swap maximum row with current row (column by column)
for (var k=i; k<n+1; k++) {
var tmp = A[maxRow][k];
A[maxRow][k] = A[i][k];
A[i][k] = tmp;
}

// Make all rows below this one 0 in current column
for (k=i+1; k<n; k++) {
var c = -A[k][i]/A[i][i];
for(var j=i; j<n+1; j++) {
if (i==j) {
A[k][j] = 0;
} else {
A[k][j] += c * A[i][j];
}
}
}
}

// Solve equation Ax=b for an upper triangular matrix A
var x= new Array(n);
for (var i=n-1; i>-1; i--) {
x[i] = A[i][n]/A[i][i];
for (var k=i-1; k>-1; k--) {
A[k][n] -= A[k][i] * x[i];
}
}
return x;
}