Gauss-Seidel方法求解非线性方程组

时间:2014-04-25 20:09:37

标签: c++ linear-algebra equation linear

[解答谢谢]我在C ++中使用Gauss-Seidel方法开发了下面的代码来解决线性方程式,但是在填充数组时我似乎遇到了一个问题,我无法弄清楚。这是我的代码......

#include<stdio.h>
int main(void)
{
    float a[10][10],b[10],x[10],y[10];
    int n=0,m=0,i=0,j=0;
    printf("Enter size of 2d array(Square matrix) : ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("Enter values no. %d %d :",i,j);
            scanf("%f",&a[i][j]);
        }
    }
    printf("\nEnter Values to the right side of equation\n");
    for(i=0;i<n;i++)
    {
            printf("Enter values no. %d :",i,j);
            scanf("%f",&b[i]);
    }
    printf("Enter initial values of x\n");
    for(i=0;i<n;i++)
    {
        printf("Enter values no. %d :",i);
        scanf("%f",&x[i]);
    }
    printf("\nEnter the no. of iteration : " );
    scanf("%d",&m);
    while(m>0)
    {
        for(i=0;i<n;i++)
        {
            y[i]=(b[i]/a[i][i]);
            for(j=0;j<n;j++)
            {
                if(j==i)
                    continue;
                y[i]=y[i]-((a[i][j]/a[i][i])*x[j]);
                x[i]=y[i];
            }
            printf("x%d = %f    ",i+1,y[i]);
        }
        printf("\n\n");
        m--;
    }
    return 0;

提前致谢:)

3 个答案:

答案 0 :(得分:1)

您没有为EquationHolder分配第二个维度。由于是2D矩阵,您还必须分配第二维。将您的双for循环更改为以下内容:

float ** EquationHolder=new float *[3];
for (int i=0; i<NumEquations; i++)
{
    EquationHolder[i] = new float[3];
    cout<<"Please Enter The Information Of Equation ("<<i+1<<")...\n";
    for (int j=0; j<NumEquations; j++)
    {
        cout<<"X"<<j+1<<": ";
        cin>>EquationHolder[i][j];
    }
}

但是我建议使用std::vector<std::vector<double>>代替C原始数组,它更安全。

答案 1 :(得分:1)

第一件事:

为所有阵列分配存储空间:

int NumEquations=3;
// Equation Holder...
float ** EquationHolder= new float *[3];
for (int i=0; i<NumEquations; i++)
{
    EquationHolder[ i] = new float[3];
     // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    cout<<"Please Enter The Information Of Equation ("<<i+1<<")...\n";
    for (int j=0; j<NumEquations; j++)
    {
        cout<<"X"<<j+1<<": ";
        cin>>EquationHolder[i][j];
    }
}

此外,主程序中存在错误:

#include <iostream>
using namespace std;

void GaussSeidel(int Iterations, float **EquationHolder, 
                                   float *EquationResultHolder) {
    int InitialGuess = 0;
    float x1 = 0, x2 = 0, x3 = 0;
    while (InitialGuess < Iterations) {
        x1 = (1 / EquationHolder[0][0])* (EquationResultHolder[0]-
                ((EquationHolder[0][1]) * x2)-((EquationHolder[0][2]) * x3));
        x2 = (1 / EquationHolder[1][1])*(EquationResultHolder[1]-
                ((EquationHolder[1][0]) * x1)-((EquationHolder[1][2]) * x3));
        x3 = (1 / EquationHolder[2][2])*((EquationResultHolder[2]-
               ((EquationHolder[2][0]) * x1)-((EquationHolder[2][1]) * x2)));
        InitialGuess += 1;
        cout << "---------------------Iteration #" << InitialGuess 
                                     << "---------------------" << std::endl;
        cout << "X1: " << x1 << "\t" << x2 << "\t" << x3 << std::endl;
    }
}

用法:

int main() {
    int NumEquations = 3;
    // Equation Holder...
    float ** EquationHolder = new float *[3];
    for (int i = 0; i < NumEquations; i++) {
        EquationHolder[ i] = new float[3];
        cout << "Please Enter The Information Of Equation(" << i + 1 << ")...\n";
        for (int j = 0; j < NumEquations; j++) {
            cout << "X" << j + 1 << ": ";
            cin >> EquationHolder[i][j];
        }
    }
    //... as before

    for ( int i = 0; i < NumEquations; i++) {   // deallocate storage
        delete [] EquationHolder[ i];
    }
    delete [] EquationHolder;

    return 0;
}

输出:

(...)

请输入所需的迭代次数:9

--------------------- Iteration#1 ---------------------

X1:4 -2 1.42857

--------------------- Iteration#2 ---------------------

X1:4.14286 -2.83333 2.10204

(...)

--------------------- Iteration#9 ---------------------

X1:3.81631 -3.03054 2.36438

答案 2 :(得分:0)

您没有初始化EquationHolder的行。加上这个:

float ** EquationHolder = new float *[3];
for(int i=0; i<NumEquations; i++)     // ADD
    EquationHolder[i] = new float[3]; // ADD

此外,我建议您使用 double 而不是 float double 更精确,更不容易受到数字错误的影响)。< / p>