/************************************* * 用追赶法求线性方程组 * * |- -| |- -| |- -| * | 2 -1 | | x1 | | 3 | * |-1 3 -2 | | x2 | | 1 | * | -2 4 -2 |*| x3 | = | 0 | * | -2 5 | | x4 | | -5 | * |- -| |- -| |- -| * **************************************/ #include<stdio.h> #include<math.h> #include<conio.h> #include<string.h> #include<stdlib.h> #define n 5 int main() { float a[n] = { 0, 0, -1, -2, -2 }; float b[n] = { 0, 2, 3, 4, 5 }; float c[n] = { 0, -1, -2, -2, 0 }; float d[n] = { 0, 3, 1, 0, -5 }; float x[n] = { 0, 0, 0, 0, 0 }; float r[n] = { 0, 0, 0, 0, 0 }; float y[n] = { 0, 0, 0, 0, 0 }; float q; int k; r[1] = c[1] / b[1]; y[1] = d[1] / b[1]; for (k = 2; k < n - 1; k++) { q = b[k] - r[k - 1] * a[k]; r[k] = c[k] / q; y[k] = (d[k] - y[k - 1] * a[k]) / q; } y[n - 1] = (d[n - 1] - y[n - 2] * a[n - 1]) / (b[n - 1] - r[n - 2] * a[n - 1]); x[n - 1] = y[n - 1]; for (k = n - 2; k >= 1; k--) x[k] = y[k] - r[k] * x[k + 1]; for (k = 1; k < n; k++) printf("x[%d] = %f\n", k, x[k]); return 0; }