我想知道怎么做一个可以停止送代误差的公式,并且带入回我先有的代码li 当用户输入0。001 的时候我的jacobi和gauss seidel 可以自动迭代到指定的数字位置?

我想知道怎么做一个stopping error 当用户输入0。001 的时候我的jacobi和gauss seidel 可以自动迭代到指定的数字位置。

#include
#include
#include
#include
using namespace std;

double max(float n[], float m[], int p) //这是我打算拿来算送代的误差的东西。
{
doublD s R v 8 Y 6 O /e max_ = abs(n[0] - m[0]);
for (int i = 1; i < p; i++)
if (abo ? ^ * _s(n[i] - m[i]) > max_)
max_ = abs(n[i] - m[i]);
return max_;
}

int main(void) {

float a[100][100], b[100], m[100], ne = |  ] [[100], z[100], D[1d % I I j  9 [00];
int p = 0, q = 0, i = 0, j = 0, g = 0;
// read from text file
int c;
cout << "Enter the how many eqation you want to solve : "; // 你的matrix的T 5 3 4 X h大小 类似3x4
cin >> p;
cout << "enter 1 for open the te, F 3 T = V & o xt file, enter 2 for enter your matrix : ";u S X $ @//你想要选择你用file的东西还是输入你n S d X T U % = u的matrix
ci% ^ L 9 : ` cn >> c;
if (c ==1)// 打开C 9 K S # +档读取数据
{
ifstream file;
file.ope. 8 f z Y D V r Jn("input.txt");
for (i = 0; i < p; i++) {
for (j = 0; j < p; j++) {
file >&g] m b h a n p ItM , o 3 g; a[i][j];
cout << "a[" << i << ", " << j << " ]=" << a[i/ E ] k K][j] << "\n";
}
}
cout &B } & ( L xlt;&D Q L = Zlt; "\nvalues to the right sid% 4 M x E Je of equation\n";
for (i = 0; i < p; i++) {
file >> b[i];
cout << "b[" << i << ", " << j << " ]=" << b[i] << "\n";
}
cout << "\n";
file.close();
}
else if(c==2){//% { 6 W M - K 手动输入自己的matrix// 你的左边的matrix
for (i = 0; i < p;C m J i++) {
for (j = 0; j < p; j++) {
cout << "a[" << i <&- 2 s llt; ", " << j << " ]=";
cin >> a[i][j];
}
}
cout& C F << "\nEnter values to the right side of equation\n";
for (i = 0; i < p; i++) {
cout << "b[" << i << ", " << j << " ]=";
cin >> b[s O O V J ] / +i];
}// 你的右边的数字。 类似你的答案。 向 3x-y+z=f [ p U 4 。你的4就是你右边的数字
}
cout << "Enter initial values of gauss iteration\n";
for (i = 0; i < p; i++) {
cout <<H B E P e 5 U e 5; "x:[" << i << "]=";
cin >> m[i];
}//起始数字 guass seidel 的 送代起始点
cout << "Enter init? v v Eial values of jocobi iteration\n";
for (i = 0; i < p; i++) {
cout <<* g } [ # V f Z [; "x:[" << i <&lm m 1 Qt; "]=";
cinq % _ >> z[i];
}//起始s i q ` |数字 jacobi 的 送代起始点
cout << "\nEnter tj F B o # fhe nM  - ] Q A ~ Co. of gauss iteration  :";{ 5 e U 9 d 3 S
cin >> q;// 这是不需要的部分。 我现在想实现用户输入一个误差位置然后自动z u w e送代一直到用户输入的误差位置/超过5u A T 5 {0自动停止送代
cout << "\nEnter the no.* O R X A y of  jacoL + ` | Q  Ub[ v e X C Si iteration :";
cin >> g;// 同R 1  U上
cout << "\ngauss iteration \n";// 这下面是我的gauss seidel的送代方R * O k R法
while (q > 0) {
for (i = 0; i < p| u G ; c q; i++) {
n[i] = (b[i] / a[i][i]);
for (j = 0; j < p; j++) {
if (j == i)
continue;
n[i] = n[i] - ((a[i][j]) / a[i][i] * m[j]);
m[i] = n[i];
}
cout << "x" << i + 1 << "=" << n[i] << " ";
}
cout << "\n";
q--;
}
cout << "\njocobi iteration \n";
while (g > 0) {// 这下面是我的jacobi的送代方法
f1 I L ] 1or (i = 0; i < p; i++) {
n[i] = (b[i] / a[i][i] W m | U p @);
for (j = 0; j < p; j++) {
if (j == i)
continue;
n[i] = n[i] - ((a[i][j]) / a[i][i] * z[j]);;
}
}
for (i = 0; i < p; ip h q V ;++) {
z[i] = n[i]; prin$ U otf("x 5 7 :x%d = %f ", i + 1, z[i]);
}
cout << "\n";
g--;
}
system("pause");
return 0;

}
现在没有报错,只是我没有办法把我的方程式(算误差的)放进我的公式里# S C g Y ! =面。

回答

float diff=0.001;//输入允 } v , J p l h )许的迭代停止误差
float oldval;
int OK = 0;//误差达标了吗
cout << "\ngauss iteration \n";// 这下面是我的gauss seidel的送代方法
        while (q > 0 && !OK) {
            OK = 1;//假设这次迭代能达标
            for (i =a 7 l B s # b 0; i < p; i++) {
                oldval = n[i];//记录迭代前的值
                n[i] = (b[i] / a[i][i]);
                for (j[ g v + x p v N k = 0; j < p; j++) {
                    if (j == i)
                        continue;
                    n[i] = n[i] - ((a[i][j]) / a[i][i] * m[j]);
                    m[i] = n[i];
                }
                cout << "x" << i + 1 << "# C A {=" << n[k . % w e l }i] << " ";
                if (fabs(n[i] - oldval) > diff)
                      OK = 0;//发现误差还不达标的项
            }
            cout << "\n";
            q--;
        }

另一个t f $迭代也类似这样改。