计算方法(数值分析)实验:解线性方程组的高斯消元和LU直接方法实现 java

计算方法(数值分析)实验:解线性方程组的高斯消元和LU直接方法实现 java,第1张

计算方法(数值分析)实验:解线性方程组的高斯消元和LU直接方法实现 java

目的要求:用高斯消元法或者解线性方程组的直接三角分解法求解线性方程组Ax=b,式中A为n阶非奇异方阵,x,b为n阶列向量。要求编写实验程序(语言不限),给出实验结果。

题目1 例5.2

题目2 练习5.2

import dao.funcDao;
import service.FuncService;

import java.util.Scanner;

public class main {
    public static void main(String[] args) {
       
        FuncService.go();

    }

}

package service;

import dao.funcDao;

import java.util.Scanner;

public class FuncService {
    public static void go(){
        Scanner s = new Scanner(System.in);
        System.out.print("请输入矩阵行/列数:");
        int n = s.nextInt();
        int[][] A = new int[n][n];
        int[][] L = new int[n][n];
        int[][] U = new int[n][n];

        int[] B = new int[n];
        int[] Y = new int[n];
        int[] X = new int[n];

        System.out.println("输入A矩阵的值:");
        for (int i = 0; i < n; i++){
            for(int j = 0; j < n; j ++){
                A[i][j] = s.nextInt();
            }
        }

        System.out.println("输入B矩阵的值:");
        for (int i = 0; i < n; i++){
            B[i] = s.nextInt();
        }
        //对U的第一行 和 L的第一列计算
        for (int i = 0; i < n; i++){
            U[0][i] = A[0][i];
            L[i][0] = A[i][0] / U[0][0];
        }

        //计算矩阵U L
        for (int r = 0; r < n; r++){
            for(int i = r; i < n; i++){
                int tempU = 0;
                int tempL = 0;
                for (int k = 0; k < r; k++){
                    tempU += L[r][k] * U[k][i];
                    tempL += L[i][k] * U[k][r];
                }
                U[r][i] = A[r][i] - tempU;
                L[i][r] = (A[i][r] - tempL) /  U[r][r];
            }
        }

        //计算Y的值
        Y[0] = B[0];
        for (int i = 1; i < n; i++){
            int tempY = 0;
            for (int k = 0; k < i; k++){
                tempY += L[i][k] * Y[k];
            }
            Y[i] = B[i] - tempY;
        }

        X[n-1] = Y[n-1] / U[n-1][n-1];
        for (int i = n-2; i >= 0; i--){
            int tempX = 0;
            for (int k = i + 1; k < n; k++){
                tempX += U[i][k] * X[k];
            }
            X[i] = (Y[i] - tempX) / U[i][i];
        }

        System.out.println("U矩阵如下:");
        funcDao.displayTwo(U,n);
        System.out.println("L矩阵如下:");
        funcDao.displayTwo(L,n);
        System.out.println("Y矩阵如下:");
        funcDao.displayOne(Y,n);
        System.out.println("X矩阵如下:");
        funcDao.displayOne(X,n);
    }

}

package dao;

public class funcDao {
    public static void displayOne(int arr[], int n){
        System.out.println("-------------------");
        for (int i : arr) {
            System.out.print(i + " ");
        }
        System.out.println();
    }
    public static void displayOne(double arr[], int n){
        System.out.println("-------------------");
        for (double i : arr) {
            System.out.print(i + " ");
        }
        System.out.println();
    }


    public static void displayTwo(int arr[][], int n){
        System.out.println("-------------------");
        for (int[] ints : arr) {
            for (int anInt : ints) {
                System.out.print(anInt + " ");
            }
            System.out.println();
        }
        System.out.println();
    }

    public static void displayTwo(double arr[][], int n){
        System.out.println("-------------------");
        for (double[] ints : arr) {
            for (double anInt : ints) {
                System.out.print(anInt + " ");
            }
            System.out.println();
        }
        System.out.println();
    }


}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5697451.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存