数值计算练习 LU分解(杜里特尔和克洛特分解)

 数值计算练习 LU分解(杜里特尔和克洛特分解)_第1张图片

Code:(C++实现)

杜里特尔分解:

#include
#include
#include
#include
#include
#include
#include
#define y1 AC
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define E 1e-12
typedef long long ll;
const double pi = acos(-1);
const int MAX_N = 1005;
using namespace std;
double L[MAX_N][MAX_N];
double U[MAX_N][MAX_N];
double x[MAX_N], y[MAX_N];
double a[MAX_N][MAX_N], b[MAX_N];
int n, m;
int main(){
    //int n, m;
    printf("-----杜里特尔分解-----\n");
    printf("请输入矩阵大小:");
    while(scanf("%d", &n) != EOF){
        printf("请输入系数矩阵:\n");
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                scanf("%lf", &a[i][j]);
            }
        }
        printf("请输入行列式:\n");
        for(int i = 1; i <= n; i++){
            scanf("%lf", &b[i]);
        }
        for(int i = 1; i <= n; i++){
            L[i][i] = 1;
            U[1][i] = a[1][i];
        }
        for(int i = 1; i <= n - 1; i++){
            for(int j = i + 1; j <= n; j++){
                double sum = 0;
                for(int k = 1; k <= n; k++){
                    if(k != i)  sum += L[j][k] * U[k][i];
                }
                L[j][i] = (a[j][i] - sum) / U[i][i];
            }
            for(int j = i + 1; j <= n; j++){
                double sum = 0;
                for(int k = 1; k <= n; k++){
                    if(k != i + 1)  sum +=  L[i + 1][k] * U[k][j];
                }
                U[i + 1][j] = a[i + 1][j] - sum;
            }
        }
        printf("L矩阵:\n");
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= i; j++){
                printf("%.3f ", L[i][j]);
            }
            printf("\n");
        }
        printf("\nU矩阵:\n");
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if(j < i)   printf("      ");
                else printf("%.3f ", U[i][j]);
            }
            printf("\n");
        }
        for(int i = 1; i <= n; i++){
            if(i == 1)  y[i] = b[i];
            else {
                double sum = 0;
                for(int j = 1; j <= i - 1; j++){
                    sum += L[i][j] * y[j];
                }
                y[i] = (b[i] - sum) / L[i][i];
            }
        }
        for(int i = n; i >= 1; i--){
            if(i == n)  x[i] = y[i] / U[i][i];
            else{
                double sum = 0;
                for(int j = i + 1; j <= n; j++){
                    sum += x[j] * U[i][j];
                }
                x[i]= (y[i] - sum) / U[i][i];
            }
        }
        printf("\n方程组的解:\n");
        for(int i = 1; i <= n; i++){
            printf("y(%d) = %.3f\n", i, y[i]);
        }
        printf("\n");
        for(int i = 1; i <= n; i++){
            printf("x(%d) = %.3f\n", i, x[i]);
        }
    }
    return 0;
}

结果展示:数值计算练习 LU分解(杜里特尔和克洛特分解)_第2张图片

 

克洛特分解:

#include
#include
#include
#include
#include
#include
#include
#define y1 AC
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define E 1e-12
typedef long long ll;
const double pi = acos(-1);
const int MAX_N = 1005;
using namespace std;
double L[MAX_N][MAX_N];
double U[MAX_N][MAX_N];
double x[MAX_N], y[MAX_N];
double a[MAX_N][MAX_N], b[MAX_N];
int n, m;
int main(){
    //int n, m;
    printf("-----克洛特分解-----\n");
    printf("请输入矩阵大小:");
    while(scanf("%d", &n) != EOF){
        printf("请输入系数矩阵:\n");
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                scanf("%lf", &a[i][j]);
            }
        }
        printf("请输入行列式:\n");
        for(int i = 1; i <= n; i++){
            scanf("%lf", &b[i]);
        }
        for(int i = 1; i <= n; i++){
            L[i][1] = a[i][1];
            U[i][i] = 1;
        }
        for(int k = 1; k <= n; k++){
            for(int i = k; i <= n; i++){
                double sum = 0;
                for(int j = 1; j < k; j++){
                    sum += L[i][j] * U[j][k];
                }
                L[i][k] = a[i][k] - sum;
            }
            for(int j = k + 1; j <= n; j++){
                double sum = 0;
                for(int i = 1; i < k; i++){
                    sum +=  L[k][i] * U[i][j];
                }
                U[k][j] = (a[k][j] - sum) / L[k][k];
            }
        }
        printf("L矩阵:\n");
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= i; j++){
                printf("%.3f ", L[i][j]);
            }
            printf("\n");
        }
        printf("\nU矩阵:\n");
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if(j < i)   printf("      ");
                else printf("%.3f ", U[i][j]);
            }
            printf("\n");
        }
        for(int i = 1; i <= n; i++){
            if(i == 1)  y[i] = b[i] / L[i][i];
            else {
                double sum = 0;
                for(int j = 1; j < i; j++){
                    sum += L[i][j] * y[j];
                }
                y[i] = (b[i] - sum) / L[i][i];
            }
        }
        for(int i = n; i >= 1; i--){
            if(i == n)  x[i] = y[i] / U[i][i];
            else{
                double sum = 0;
                for(int j = i + 1; j <= n; j++){
                    sum += x[j] * U[i][j];
                }
                x[i]= (y[i] - sum) / U[i][i];
            }
        }
        printf("\n方程组的解:\n");
        for(int i = 1; i <= n; i++){
            printf("y(%d) = %.3f\n", i, y[i]);
        }
        printf("\n");
        for(int i = 1; i <= n; i++){
            printf("x(%d) = %.3f\n", i, x[i]);
        }
    }
    return 0;
}

结果展示:数值计算练习 LU分解(杜里特尔和克洛特分解)_第3张图片

你可能感兴趣的:(数值计算练习 LU分解(杜里特尔和克洛特分解))