线性方程系统Thomas算法Java

发布于 2025-01-22 07:04:38 字数 992 浏览 4 评论 0原文

我已经改编了托马斯算法来求解三角矩阵问题,即在= b的方程式中找到向量t的值,其中a和b是已知的。

下面我提供了代码和正在输入的向量。对于以下方程式,输入是(应该是正确的):

W + 2X + 3Y + 4Z = 10

8W + 9X + 6Y + Z = 13   

2X + 3Y + 5Z = 20

5Z = 50

使用线性方程式求解器的在线系统,我希望答案向量(x)看起来像{0,12.6,-18.4,10} ){-11.2,10.6,-10,10}。

我将其与其他解决方案进行了比较,并且代码似乎是一样的,我无法弄清楚问题所在。

double[] a = {8, 2, 0, 0}; // Super diag of A
double[] b = {1, 9, 3, 5}; // Main diag of A
double[] c = {2, 6, 5, 0}; // Sub diag of A
double[] d = {10, 13, 20, 50}; // Column vector b

public static double[] thomasAlgo(double[] a, double[] b, double[] c, double[] d) {
  int n = d.length;
  double temp;
  c[0] = c[0] / b[0];
  d[0] = d[0] / b[0];

  for (int i = 1; i<n; i++) {
    temp = 1.0 / (b[i] - a[i] * c[i - 1]);
    c[i] = c[i] * temp;
    d[i] = (d[i] - a[i] * d[i - 1]) * temp;
  }

  double[] x = new double[n];
  x[n - 1] = d[n - 1];

  for (int i = n-2; i >= 0; i--) {
    x[i] = d[i] - c[i] * x[i+1];
  }
  return x;
}

I have adapted the Thomas Algorithm for solving the tridiagonal matrix problem i.e. finding the value of the vector T in the equation AT = b where A and b are known.

Below I have provided the code and the vectors I am inputting. The input is (should be correct) for the following equations:

W + 2X + 3Y + 4Z = 10

8W + 9X + 6Y + Z = 13   

2X + 3Y + 5Z = 20

5Z = 50

Using an online system of linear equations solver I expect the answer vector (x) to look like {0, 12.6, -18.4, 10}, however, it is returning (rounded) {-11.2, 10.6, -10, 10}.

I have compared this to other solutions and the code seems the same, I cannot work out what the issue lies.

double[] a = {8, 2, 0, 0}; // Super diag of A
double[] b = {1, 9, 3, 5}; // Main diag of A
double[] c = {2, 6, 5, 0}; // Sub diag of A
double[] d = {10, 13, 20, 50}; // Column vector b

public static double[] thomasAlgo(double[] a, double[] b, double[] c, double[] d) {
  int n = d.length;
  double temp;
  c[0] = c[0] / b[0];
  d[0] = d[0] / b[0];

  for (int i = 1; i<n; i++) {
    temp = 1.0 / (b[i] - a[i] * c[i - 1]);
    c[i] = c[i] * temp;
    d[i] = (d[i] - a[i] * d[i - 1]) * temp;
  }

  double[] x = new double[n];
  x[n - 1] = d[n - 1];

  for (int i = n-2; i >= 0; i--) {
    x[i] = d[i] - c[i] * x[i+1];
  }
  return x;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文