【问题标题】:Can someone explain the last part of this code?有人可以解释这段代码的最后一部分吗?
【发布时间】:2021-10-24 02:28:14
【问题描述】:

我是 C++ 的新手。在过去的 2 个小时里,我一直在编写一些代码。这是关于寻找三对角系统的解决方案。

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
    float mat[100][100];
    int n;
    cout << "Enter the dimention of the matrix: ";
    cin >> n;
    cout << "Enter the matrix: ";
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cin >> mat[i][j];
        }
    }
    float d[100];
    cout << "Enter the values of di's: ";
    for (int i = 0; i < n; i++)
    {
        cin >> d[i];
    }
    for (int i = 0; i < n; i++)
    {
        cout << "The value of d" << i + 1 << " is" << d[i];
    }
    cout << " \n";

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)

            cout << mat[i][j] << " ";
        {
            cout << endl;
        }

    }

    float a[100], b[100], c[100];
    for (int i = 0; i < n + 1; i++)
    {
        b[i] = mat[i][i];
    }
    for (int l = 0; l < n; l++)
    {
        cout << "The value of b" << l + 1 << " is " << b[l] << " " << endl;
    }
    cout << "\n";

    for (int j = 0; j < n; j++)
    {
        a[j] = mat[j][j - 1];
    }

    for (int i = 1; i < n - 1; i++)
    {
        cout << "The value of a" << i + 1 << " is: " << a[i] << " " << endl;
    }
    cout << "\n";
    for (int k = 0; k < n - 1; k++)
    {
        c[k] = mat[k][k + 1];
    }
    for (int i = 0; i < n - 1; i++)
    {
        cout << "The value of c" << i + 1 << " is: " << c[i] << " " << endl;
    }
//to find alpha
    cout << "\n";
    float alpha[100];
    alpha[0] = b[0];
    for (int i = 1; i < n; i++)
    {
        alpha[i] = b[i] - ((a[i] * c[i - 1]) / alpha[i - 1]);
    }
    for (int i = 0; i < n; i++)
    {
        cout << "The value of alpha" << i + 1 << " is: " << alpha[i] << endl;
    }
    cout << "\n";
//to find beta
    float beta[100];
    beta[0] = (d[0] / b[0]);
    for (int i = 1; i < n; i++)
    {
        beta[i] = ((d[i] - a[i] * beta[i - 1]) / alpha[i]);
    }
    for (int i = 0; i < n; i++)
    {
        cout << " The value of beta" << i + 1 << " is: " << beta[i] << "\n";
    }

    //finding the solutions
    float x[100];
    x[n - 1] = beta[n - 1];
    for (int i = n - 2; i > 0; i--)
    {
        x[i] = beta[i] - ((c[i] * x[i + 1]) / alpha[i]);
    }
    cout << "the solutions are ";
    for (int i = 0; i < n - 1; i++)
    {
        cout << x[i] << "\t";
    }
    cout << beta[n - 1] << "\t";
}

输出:

PS C:\Users\jitub\desktop> ./a.exe
Enter the dimention of the matrix: 4                                                                           
Enter the matrix: 3 -1 0 0 -1 3 -1 0 0 -1 3 -1 0 0 -1 3
Enter the values of di's: -5 10 -15 15
The value of d1 is-5The value of d2 is10The value of d3 is-15The value of d4 is15 
3 -1 0 0 
-1 3 -1 0 
0 -1 3 -1 
0 0 -1 3 
The value of b1 is 3 
The value of b2 is 3 
The value of b3 is 3 
The value of b4 is 3 

The value of a2 is: -1
The value of a3 is: -1 

The value of c1 is: -1
The value of c2 is: -1
The value of c3 is: -1

The value of alpha1 is: 3
The value of alpha2 is: 2.66667
The value of alpha3 is: 2.625
The value of alpha4 is: 2.61905

 The value of beta1 is: -1.66667
 The value of beta2 is: 3.125
 The value of beta3 is: -4.52381
 The value of beta4 is: 4
 the solutions are 0     2       -3      4

使用托马斯算法,我必须找到解决方案。如果我手动执行此操作,我得到的解决方案是{-1, 2, -3, 4}。但是我得到的输出总是x1=0,这不是真的。

算法说beta4=x4,xi 的其他值在for 循环中。我尝试了很多方法来打印该值。尽管如此,我还是找不到任何可行的方法。

另外,ai 的值应该从a2 开始。在这个程序的最后一部分,我需要从后面打印出xi 的值。

【问题讨论】:

  • 我应用了一致的缩进方案,并且至少一个可能的错误立即显现出来。在一个程序中,异常通常是一个错误,所以你的代码越有规律,你就越容易发现异常。
  • 程序员的秘密武器是调试器。使用调试器,您可以以不同的粒度逐步执行代码,并查看编译器对您的代码的构成。典型的用法是快速前进到您认为问题出在断点的地方,然后单步执行代码以查找意外情况,例如采用错误的路径或存储错误的值。通常,您最终不得不多次回溯和回溯这些步骤,以查看导致可见错误的确切原因,但这几乎总是比在黑暗中摸索或提出 SO 问题要快。

标签: c++ numerical-methods linear-equation


【解决方案1】:

我认为错误在于您执行以下公式之一:

尤其是这个:

beta[i] = ((d[i] - a[i] * beta[i - 1]) / alpha[i]);

正确的表达应该是(继续阅读另一个问题):

beta[i] = d[i] - (a[i] * beta[i - 1] / alpha[i]);

这个也是错的:

x[i] = beta[i] - ((c[i] * x[i + 1]) / alpha[i]);

应该是:

x[i] = (beta[i] - c[i] * x[i + 1]) / alpha[i];

注意我是如何重新排列括号的。

修复后,您可能还需要检查其他内容。我认为您没有使用正确的索引。这可能只是您的解决方案的一些特殊性,但我的理解是表达式应该是:

// using a[i-1] instead of a[i]
alpha[i] = b[i] - (a[i-1] * c[i - 1] / alpha[i - 1]);

// using a[1-1] instead of a[i]
beta[i] = d[i] - (a[i-1] * beta[i - 1]) / alpha[i]);

// just for completeness, didn't change the indices
x[i] = (beta[i] - c[i] * x[i + 1]) / alpha[i];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多