有人可以解释这段代码的最后一部分吗?

Can someone explain the last part of this code?

我是 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=x4xi 的其他值在 for 循环中。我尝试了很多方法来打印该值。尽管如此,我还是找不到任何可行的方法。

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

我认为错误出在您对其中一个公式的实现中:

特别是这个:

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];