Can someone write the code for Naïve Gauss Elimination and Gauss-Jordan in JavaS
ID: 3838543 • Letter: C
Question
Can someone write the code for Naïve Gauss Elimination and Gauss-Jordan in JavaScript but I have to save it as Html file and then run it in a browser to get the output .. ( I have to write it on notepad then save it as html file)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] Can someone write the code for Naïve Gauss Elimination and Gauss-Jordan in JavaScript but I have to save it as Html file and then run it in a browser to get the output .. ( I have to write it on notepad then save it as html file)
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]
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] 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] 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] 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] 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] 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] 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 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]
Explanation / Answer
function solveByGauss(matrix) {
var len = matrix.length;
for (var i=0; i<len; i++) {
// Search for maximum value in this column
var maxEl = Math.abs(matrix[i][i]);
var maxRow = i;
for(var k=i+1; k<len; k++) {
if (Math.abs(matrix[k][i]) > maxEl) {
maxEl = Math.abs(matrix[k][i]);
maxRow = k;
}
}
// Swap maximum row with current row
for (var k=i; k<len+1; k++) {
var tmp = matrix[maxRow][k];
matrix[maxRow][k] = matrix[i][k];
matrix[i][k] = tmp;
}
for (k=i+1; k<len; k++) {
var c = -matrix[k][i]/matrix[i][i];
for(var j=i; j<len+1; j++) {
if (i==j) {
matrix[k][j] = 0;
} else {
matrix[k][j] += c * matrix[i][j];
}
}
}
}
// Solve equation Ax=b for an upper triangular matrix
var x= new Array(len);
for (var i=len-1; i>-1; i--) {
x[i] = matrix[i][len]/matrix[i][i];
for (var k=i-1; k>-1; k--) {
matrix[k][len] -= matrix[k][i] * x[i];
}
}
return "["+x+"]";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.