目的要求:用高斯消元法或者解线性方程组的直接三角分解法求解线性方程组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) {
/* double [][] A = {
{7, 8, 11},
{5, 1, -3},
{1, 2, 3}
};
int n = 3;
double []B = {-3, -4, 1};
double []X = new double[3];
int[] X2 = new int[3];
for (int i = 0; i < 3 - 1; i++) { //i是被减去的
for (int j = i + 1; j < 3; j++) { //j是当前处理的行
double temp = A[j][i] / A[i][i];
B[j] -= temp * B[i];
for (int k = i; k < 3; k++){ //k是当前处理行的列
A[j][k] -= A[i][k] * temp;
}
}
}
X[n-1] = B[n-1] / A[n-1][n-1];
for (int k = n - 2; k >= 0; k--) {
double temp = 0;
for (int j = k+1; j < n; j++) {
temp += A[k][j] * X[j];
}
X[k] = (B[k] - temp)/ A[k][k];
}
for (int i = 0; i < n; i++) {
X2[i] = (int)Math.round(X[i]);
}
funcDao.displayTwo(A,3);
funcDao.displayOne(B,3);
funcDao.displayOne(X2,3);*/
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();
}
}