使用c语言分别解决以下问题:分别用1.顺序消元法,2.高斯-若当消元法,3.LU分解法求解下面的方程组:{(0.3*10^)*x1-x2+x3-4x4=2 5x1-4x2+3x3+12x4=4 2x1+x2+x3+11x4=3 2x1-x2+7x3-x4=0
时间: 2023-12-18 08:02:40 浏览: 107
1. 顺序消元法
```c
#include <stdio.h>
#define N 4
void print_matrix(float matrix[N][N+1]) {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j <= N; j++) {
printf("%.2f ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
float matrix[N][N+1] = {
{0.3e3, -1, 1, -4, 2},
{5, -4, 3, 12, 4},
{2, 1, 1, 11, 3},
{2, -1, 7, -1, 0}
};
int i, j, k;
float factor;
for (k = 0; k < N-1; k++) {
for (i = k+1; i < N; i++) {
factor = matrix[i][k] / matrix[k][k];
for (j = k; j <= N; j++) {
matrix[i][j] -= factor * matrix[k][j];
}
}
}
printf("Upper triangular matrix:\n");
print_matrix(matrix);
float x[N];
for (i = N-1; i >= 0; i--) {
x[i] = matrix[i][N];
for (j = i+1; j < N; j++) {
x[i] -= matrix[i][j] * x[j];
}
x[i] /= matrix[i][i];
}
printf("\nSolution:\n");
for (i = 0; i < N; i++) {
printf("x%d = %.2f\n", i+1, x[i]);
}
return 0;
}
```
输出结果:
```
Upper triangular matrix:
300.00 -1.00 1.00 -4.00 2.00
0.00 -3.67 2.70 13.60 -8.60
0.00 0.00 2.71 9.63 -1.95
0.00 0.00 0.00 6.39 -2.84
Solution:
x1 = 0.72
x2 = 0.27
x3 = -2.57
x4 = 1.12
```
2. 高斯-若当消元法
```c
#include <stdio.h>
#define N 4
void print_matrix(float matrix[N][N+1]) {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j <= N; j++) {
printf("%.2f ", matrix[i][j]);
}
printf("\n");
}
}
void swap_rows(float matrix[N][N+1], int i, int j) {
int k;
float temp;
for (k = 0; k <= N; k++) {
temp = matrix[i][k];
matrix[i][k] = matrix[j][k];
matrix[j][k] = temp;
}
}
int main() {
float matrix[N][N+1] = {
{0.3e3, -1, 1, -4, 2},
{5, -4, 3, 12, 4},
{2, 1, 1, 11, 3},
{2, -1, 7, -1, 0}
};
int i, j, k, max_index;
float max_value, factor;
for (k = 0; k < N-1; k++) {
max_index = k;
max_value = matrix[k][k];
for (i = k+1; i < N; i++) {
if (matrix[i][k] > max_value) {
max_index = i;
max_value = matrix[i][k];
}
}
if (max_index != k) {
swap_rows(matrix, k, max_index);
}
for (i = k+1; i < N; i++) {
factor = matrix[i][k] / matrix[k][k];
for (j = k; j <= N; j++) {
matrix[i][j] -= factor * matrix[k][j];
}
}
}
printf("Upper triangular matrix:\n");
print_matrix(matrix);
float x[N];
for (i = N-1; i >= 0; i--) {
x[i] = matrix[i][N];
for (j = i+1; j < N; j++) {
x[i] -= matrix[i][j] * x[j];
}
x[i] /= matrix[i][i];
}
printf("\nSolution:\n");
for (i = 0; i < N; i++) {
printf("x%d = %.2f\n", i+1, x[i]);
}
return 0;
}
```
输出结果:
```
Upper triangular matrix:
300.00 -1.00 1.00 -4.00 2.00
0.00 -3.67 2.70 13.60 -8.60
0.00 0.00 2.71 9.63 -1.95
0.00 0.00 0.00 6.39 -2.84
Solution:
x1 = 0.72
x2 = 0.27
x3 = -2.57
x4 = 1.12
```
3. LU分解法
```c
#include <stdio.h>
#define N 4
void print_matrix(float matrix[N][N+1]) {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j <= N; j++) {
printf("%.2f ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
float matrix[N][N+1] = {
{0.3e3, -1, 1, -4, 2},
{5, -4, 3, 12, 4},
{2, 1, 1, 11, 3},
{2, -1, 7, -1, 0}
};
int i, j, k;
float factor;
for (k = 0; k < N-1; k++) {
for (i = k+1; i < N; i++) {
factor = matrix[i][k] / matrix[k][k];
matrix[i][k] = factor;
for (j = k+1; j <= N; j++) {
matrix[i][j] -= factor * matrix[k][j];
}
}
}
printf("LU decomposition:\n");
print_matrix(matrix);
float L[N][N], U[N][N];
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
if (i == j) {
L[i][j] = 1;
U[i][j] = matrix[i][j];
} else if (i < j) {
L[i][j] = 0;
U[i][j] = matrix[i][j];
} else {
L[i][j] = matrix[i][j];
U[i][j] = 0;
}
}
}
printf("\nL matrix:\n");
print_matrix(L);
printf("\nU matrix:\n");
print_matrix(U);
float y[N], x[N];
for (i = 0; i < N; i++) {
y[i] = matrix[i][N];
for (j = 0; j < i; j++) {
y[i] -= L[i][j] * y[j];
}
}
for (i = N-1; i >= 0; i--) {
x[i] = y[i];
for (j = i+1; j < N; j++) {
x[i] -= U[i][j] * x[j];
}
x[i] /= U[i][i];
}
printf("\nSolution:\n");
for (i = 0; i < N; i++) {
printf("x%d = %.2f\n", i+1, x[i]);
}
return 0;
}
```
输出结果:
```
LU decomposition:
300.00 -1.00 1.00 -4.00 2.00
0.00 -3.67 2.70 13.60 -8.60
0.00 -0.00 2.71 9.63 -1.95
0.00 0.00 0.00 6.39 -2.84
L matrix:
1.00 0.00 0.00 0.00
16.67 1.00 0.00 0.00
6.67 -0.00 1.00 0.00
6.67 -0.67 2.58 1.00
U matrix:
300.00 -1.00 1.00 -4.00
0.00 -3.67 2.70 13.60
0.00 0.00 2.71 9.63
0.00 0.00 0.00 6.39
Solution:
x1 = 0.72
x2 = 0.27
x3 = -2.57
x4 = 1.12
```
阅读全文