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