为什么我的 Javascript 高斯消元程序在我输入矩阵时不起作用?
Why is my Javascript Gauss Elimination program not working when I input the matrix?
我在 Javascript 中有一个程序可以执行高斯消元来求解方程组。我的问题是,当用户尝试输入系数矩阵和解向量时,程序根本无法运行。现在,我知道它有效,因为如果在代码中输入数据,例如
var A = [[1, 1, 1], [2, 1, 2], [1, 2, 3]];
var x = [6, 10, 14];
程序会正确输出答案。当手动输入数据时,我的问题就来了。老实说,我找不到它有什么问题,因为我已经检查过输入工作正常并且处理工作正常。
<html>
<head>
<title>
Solving a system of equations
</title>
</head>
<body>
<script type="text/javascript">
var abs = Math.abs;
function array_fill(i, n, v) {
var a = [];
for (; i < n; i++) {
a.push(v);
}
return a;
}
/**
* Gaussian elimination
* @param array A matrix
* @param array x vector
* @return array x solution vector
*/
function gauss(A, x) {
//Index variables
var i, k, j;
var A_copy = [];
var x_copy = [];
//Copy A
for (var i = 0; i < A.length; ++i) {
A_copy[i] = A[i].slice();
}
//Copy b
for (var i = 0; i < x.length; ++i) {
x_copy[i] = x[i];
}
// Just make a single matrix
for (i = 0; i < A_copy.length; i++) {
A_copy[i].push(x_copy[i]);
}
var n = A_copy.length;
for (i = 0; i < n; i++) {
// Search for maximum in this column
var maxEl = abs(A_copy[i][i]),
maxRow = i;
for (k = i + 1; k < n; k++) {
if (abs(A_copy[k][i]) > maxEl) {
maxEl = abs(A_copy[k][i]);
maxRow = k;
}
}
// Swap maximum row with current row (column by column)
for (k = i; k < n + 1; k++) {
var tmp = A_copy[maxRow][k];
A_copy[maxRow][k] = A_copy[i][k];
A_copy[i][k] = tmp;
}
// Make all rows below this one 0 in current column
for (k = i + 1; k < n; k++) {
var c = -A_copy[k][i] / A_copy[i][i];
for (j = i; j < n + 1; j++) {
if (i === j) {
A_copy[k][j] = 0;
} else {
A_copy[k][j] += c * A_copy[i][j];
}
}
}
}
// Solve equation Ax=b for an upper triangular matrix A
x_copy = array_fill(0, n, 0);
for (i = n - 1; i > -1; i--) {
x_copy[i] = A_copy[i][n] / A_copy[i][i];
for (k = i - 1; k > -1; k--) {
A_copy[k][n] -= A_copy[k][i] * x_copy[i];
}
}
return x_copy;
}
alert("Solving a System of equations")
var rows = prompt("Enter the number of variables of the system");
var A = [];
alert("Enter the equations´ coefficients");
for (var i = 0; i < rows; i++) {
A.push([]);
A[i].push(new Array(rows));
for (var j = 0; j < rows; j++) {
A[i][j] = prompt()
}
}
alert("Now enter the solutions of each respective equation");
x = new Array(rows)
for (i = 0; i < rows; i++) {
x[i] = prompt()
}
result = gauss(A, x);
document.write(result);
</script>
</body>
</html>
输出应该只是求解系统的浮点数,但是当我尝试手动输入矩阵时它会显示 NaN
问题是您从 prompt
函数输入的是一个字符串。您必须先将其转换为数值。最简单的方法是在 prompt
:
前面加上 +
A[i][j] = +prompt()
x[i] = +prompt()
我在 Javascript 中有一个程序可以执行高斯消元来求解方程组。我的问题是,当用户尝试输入系数矩阵和解向量时,程序根本无法运行。现在,我知道它有效,因为如果在代码中输入数据,例如
var A = [[1, 1, 1], [2, 1, 2], [1, 2, 3]];
var x = [6, 10, 14];
程序会正确输出答案。当手动输入数据时,我的问题就来了。老实说,我找不到它有什么问题,因为我已经检查过输入工作正常并且处理工作正常。
<html>
<head>
<title>
Solving a system of equations
</title>
</head>
<body>
<script type="text/javascript">
var abs = Math.abs;
function array_fill(i, n, v) {
var a = [];
for (; i < n; i++) {
a.push(v);
}
return a;
}
/**
* Gaussian elimination
* @param array A matrix
* @param array x vector
* @return array x solution vector
*/
function gauss(A, x) {
//Index variables
var i, k, j;
var A_copy = [];
var x_copy = [];
//Copy A
for (var i = 0; i < A.length; ++i) {
A_copy[i] = A[i].slice();
}
//Copy b
for (var i = 0; i < x.length; ++i) {
x_copy[i] = x[i];
}
// Just make a single matrix
for (i = 0; i < A_copy.length; i++) {
A_copy[i].push(x_copy[i]);
}
var n = A_copy.length;
for (i = 0; i < n; i++) {
// Search for maximum in this column
var maxEl = abs(A_copy[i][i]),
maxRow = i;
for (k = i + 1; k < n; k++) {
if (abs(A_copy[k][i]) > maxEl) {
maxEl = abs(A_copy[k][i]);
maxRow = k;
}
}
// Swap maximum row with current row (column by column)
for (k = i; k < n + 1; k++) {
var tmp = A_copy[maxRow][k];
A_copy[maxRow][k] = A_copy[i][k];
A_copy[i][k] = tmp;
}
// Make all rows below this one 0 in current column
for (k = i + 1; k < n; k++) {
var c = -A_copy[k][i] / A_copy[i][i];
for (j = i; j < n + 1; j++) {
if (i === j) {
A_copy[k][j] = 0;
} else {
A_copy[k][j] += c * A_copy[i][j];
}
}
}
}
// Solve equation Ax=b for an upper triangular matrix A
x_copy = array_fill(0, n, 0);
for (i = n - 1; i > -1; i--) {
x_copy[i] = A_copy[i][n] / A_copy[i][i];
for (k = i - 1; k > -1; k--) {
A_copy[k][n] -= A_copy[k][i] * x_copy[i];
}
}
return x_copy;
}
alert("Solving a System of equations")
var rows = prompt("Enter the number of variables of the system");
var A = [];
alert("Enter the equations´ coefficients");
for (var i = 0; i < rows; i++) {
A.push([]);
A[i].push(new Array(rows));
for (var j = 0; j < rows; j++) {
A[i][j] = prompt()
}
}
alert("Now enter the solutions of each respective equation");
x = new Array(rows)
for (i = 0; i < rows; i++) {
x[i] = prompt()
}
result = gauss(A, x);
document.write(result);
</script>
</body>
</html>
输出应该只是求解系统的浮点数,但是当我尝试手动输入矩阵时它会显示 NaN
问题是您从 prompt
函数输入的是一个字符串。您必须先将其转换为数值。最简单的方法是在 prompt
:
+
A[i][j] = +prompt()
x[i] = +prompt()