【发布时间】:2015-09-24 10:44:21
【问题描述】:
此程序使用Gauss-Siedel 迭代过程求解线性方程组,其中初始近似为:
(x0, y0, z0) = (0, 0, 0)
但是当我运行它时,窗口在我输入矩阵输入后立即关闭,无法完成整个程序。
代码如下:
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main(){
float x1, x2, x3, x1new, x2new, x3new, sum;
float a[3][3], b[3], error[3];
x1 = x2 = x3 = 0;
x1new = x2new = x3new = 0;
cout <<"enter the coefficients of equation or matrix a \n";
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++)
cin >> a[i][j];
}
cout <<" \n enter the right side values of equation or matrix b ";
for(int i = 0; i < 3; i++)
cin >> b[i];
for(int i = 0; i < 3; i++){
error[i] = 1;
}
while((error[0]| | error[1]| | error[2]) > =0.00001){
x1 = (b[0] - (a[0][1] * x2 + a[0][2] * x3)) / a[0][0];
x2 = (b[1] - (a[1][0] * x1 + a[1][2] * x3)) / a[1][1];
x3 = (b[2] - (a[2][0] * x1 + a[2][1] * x2)) / a[2][2];
error[0] = abs(x1) - abs(x1new);
error[1] = abs(x2) - abs(x2new);
error[2] = abs(x3) - abs(x3new);
x1new = x1;
x2new = x2;
x3new = x3;
}
cout <<"\n the values of variables x1,x2 and x3 are:";
cout << x1 << x2 << x3;
}
问题:
我做错了什么?
【问题讨论】:
-
[4 -1 1;4 -8 1;-2 1 5] [7 -21 15]
-
您已经编辑了帖子,但当数组仅分配给 3 个元素时,我仍然看到在 b[3] 中对下标 3 的引用。这将使程序崩溃。 b[3] 中没有数据,即使您将分配扩展为 float b[4](3 是 b 中的第 4 个元素),您仍然不会在 b[3] 中放置任何内容。与 [3] 相同。那些不应该从 0 到 2 索引,就像其他所有东西一样,还是你认为它应该从 1 到 3 索引?
标签: c++ sparse-matrix