gpt4 book ai didi

c++ - Gauss-Seidel 方法计算 3 个线性方程组

转载 作者:太空宇宙 更新时间:2023-11-04 14:19:49 25 4
gpt4 key购买 nike

我的任务是开发一个程序来计算 3 个线性方程组:该程序必须允许用户输入系数和常数、迭代次数和可接受的误差水平。我似乎无法同时包含迭代次数和错误级别作为参数来停止循环并显示变量的最终值。这是我到目前为止所拥有的:

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
cout<<"Welcome. This is Problem 1. "<<endl;
cout<<"computing systems of three linear equations through gauss-seidel method"<<endl;

float coefEqxn1[3];
for (int x=0; x<3;)
{
for ( int eq1=1; eq1<=3; eq1++)
{
cout<<"Please enter Coefficient " <<eq1<< " of equation 1 : ";
cin>>coefEqxn1[x];
x++;
}
}

float coefEqxn2[3];
for (int x=0; x<3;)
{
for ( int eq2=1; eq2<=3; eq2++)
{
cout<<"Please enter Coefficient " <<eq2<<" of equation 2 :" ;
cin>>coefEqxn2[x];
x++;
}
}

float coefEqxn3[3];
for (int x=0; x<3;)
{
for ( int eq3=1; eq3<=3; eq3++)
{
cout<<"Please enter Coefficient "<<eq3<<" of equation 3 :";
cin>>coefEqxn3[x];
x++;
}
}

float constants[3];
for (int y=0; y<3;)
{
for (int con=1; con<=3; con++)
{
cout<<"Please enter the contant of equation "<<con<<" : ";
cin>>constants[y];
y++;
}
}

cout<<"Calculating through Cramer's Rule..."<<endl;

int iteration=0;
cout<<"enter # iteration"<<endl;
cin>>iteration;

int stopC=0;
cout<<"enter level of error"<<endl;
cin>>stopC;

float matrixArray[3][4];
{
for ( int y=0; y<3;)
{
for (int x=0; x<=3;x++)
matrixArray[0][y]=coefEqxn1[y];
y++;
}

matrixArray[0][3]=constants[0];

for ( int y=0; y<3;)
{
for (int x=0; x<=3;x++)
matrixArray[1][y]=coefEqxn2[y];
y++;
}

matrixArray[1][3]=constants[1];
for ( int y=0; y<3;)
{
for (int x=0; x<=3;x++)
matrixArray[2][y]=coefEqxn3[y];
y++;
}

matrixArray[2][3]=constants[2];
}

for(int a=0; a<3; a++)
{
for(int b=0; b<=3; b++)
cout<<"matrixArray["<<a<<"]["<<b<<"]: "<<matrixArray[a][b]<<endl;
}

float valueOfX[100], valueOfY[100], valueOfZ[100];

for( int i=1; i<iteration; )
{
valueOfX[0]=0, valueOfY[0]=0, valueOfZ[0]=0;

valueOfX[i]=(matrixArray[0][3]-(matrixArray[0][2]*valueOfZ[i-1]+matrixArray[0][1]*valueOfY[i-1]))/matrixArray[0][0];
valueOfY[i]=(matrixArray[1][3]-(matrixArray[1][2]*valueOfZ[i-1]+matrixArray[1][0]*valueOfX[i]))/matrixArray[1][1];
valueOfZ[i]=(matrixArray[2][3]-(matrixArray[2][1]*valueOfY[i]+matrixArray[2][0]*valueOfX[i]))/matrixArray[2][2];

float reX=0, reY=0, reZ=0;

reX=((valueOfX[i+1]-valueOfX[i])/valueOfX[i+1])*100;
reY=((valueOfY[i+1]-valueOfY[i])/valueOfY[i+1])*100;
reX=((valueOfZ[i+1]-valueOfZ[i])/valueOfZ[i+1])*100;

if (reX<=inputErrorLevel)
break;

if (reY<=inputErrorLevel)
break;

if (reZ<=inputErrorLevel)
break;

cout<<"reX = "<<reX<<endl;
cout<<"reY = "<<reY<<endl;
cout<<"reY = "<<reX<<endl;

i++;
}

cout<<"x = "<<valueOfX[iteration-1]<<endl;
cout<<"y = "<<valueOfY[iteration-1]<<endl;
cout<<"z = "<<valueOfZ[iteration-1]<<endl;
}

最佳答案

根据你的需要,你应该有类似的东西

double inputErrorLevel; //Read from user input

// The for loop guarantees that the loop will run at max iteration times.
// Noticed I changed it to start from zero otherwise it will only run
// iteration -1 times
for( int i=0; i<iteration; ++i )
{

//Do heavy computation stuff.

double currentErrorLevel = ComputeErrorAtCurrentIteration();
if ( currentErrorLevel < inputErrorLevel )
break; // break will ensure more iterations are not done
// if error level is acceptable
}

典型的编程实践是为了

for ( int i = 0 ; i < 5; ++i )
{
}

代替

for ( int i = 0 ; i < 5; )
{
++i;
}

关于c++ - Gauss-Seidel 方法计算 3 个线性方程组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8478007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com