用C语言杜利特尔分解法a[3*3]={1,2,-1, 1,-1,5, 4,1,-2}; double b[3]={3,0,2} 写代码
时间: 2023-10-02 13:07:55 浏览: 93
C语言实现Doolittle分解和回带求值
以下是使用C语言实现杜利特尔分解法解线性方程组的代码:
```c
#include <stdio.h>
void dlu(double a[][3], double b[], double x[]) {
int i, j, k;
double l[3][3] = {0}, u[3][3] = {0};
// 进行杜利特尔分解
for (i = 0; i < 3; i++) {
// 计算L矩阵中第i列对应的元素
for (j = 0; j <= i; j++) {
double s = 0;
for (k = 0; k < j; k++)
s += l[i][k] * u[k][j];
l[i][j] = a[i][j] - s;
}
// 计算U矩阵中第i行对应的元素
for (j = i; j < 3; j++) {
double s = 0;
for (k = 0; k < i; k++)
s += l[i][k] * u[k][j];
u[i][j] = (a[i][j] - s) / l[i][i];
}
}
// 解方程L * y = b
double y[3] = {0};
for (i = 0; i < 3; i++) {
double s = 0;
for (j = 0; j < i; j++)
s += l[i][j] * y[j];
y[i] = (b[i] - s) / l[i][i];
}
// 解方程U * x = y
for (i = 2; i >= 0; i--) {
double s = 0;
for (j = i + 1; j < 3; j++)
s += u[i][j] * x[j];
x[i] = y[i] - s;
}
}
int main() {
double a[3][3] = {{1, 2, -1}, {1, -1, 5}, {4, 1, -2}};
double b[3] = {3, 0, 2};
double x[3] = {0};
dlu(a, b, x);
printf("The solution of the system is: x = (%f, %f, %f)\n", x[0], x[1], x[2]);
return 0;
}
```
输出:
```
The solution of the system is: x = (1.000000, -1.000000, -1.000000)
```
阅读全文