Please I need help. I need the tridiagonal algorithm in the C language. I need i
ID: 3846482 • Letter: P
Question
Please I need help. I need the tridiagonal algorithm in the C language. I need it to have the columns inputed through the keyboard. I can't get my code to run. my suboutine is not working properly. the correct results are....
28.8103
-30.8044
-3.8742
9.4136
-0.4181
void thomas(double aa[5], double bb[5], double cc[5], double dd[5], double u[5], int size1)
{
int imax = size1;
for (int i = 3; i <= imax-1; i++)
{
cc[i] = cc[i] - ((cc[i - 1] * bb[i]) / dd[i - 1]);
dd[i] = dd[i] - ((bb[i]* aa[i - 1]) / dd[i - 1]);
}
u[4] = cc[4] / dd[4];
for (int i = imax - 2; i >= 0; i--)
{
u[i] = (cc[i] - (aa[i] * u[i + 1])) / dd[i];
}
Explanation / Answer
void TridiagonalSolve(const double *a, const double *b, double *c, double *d, double *x, unsigned int
int i;
//Modify the coefficients.
c[0] = c[0]/b[0]; //Division by zero risk.
d[0] = d[0]/b[0];
double id;
for(i = 1; i != n; i++){
id = 1.0/(b[i] - c[i - 1]*a[i]); //Division by zero risk.
c[i] = c[i]*id; //Last value calculated is redundant.
d[i] = (d[i] - a[i]*d[i - 1])*id;
}
//Now back substitute.
x[n - 1] = d[n - 1];
for(i = n - 2; i != -1; i--)
x[i] = d[i] - c[i]*x[i + 1];
}