解:
Gauss消去法:
package shuzhifenxi;
/*Gauss消去求解方程组*/
import java.util.Scanner;
public class Exam611_Gauss {
static double a[][]; // 系数矩阵
static double b[];
static double x[]; // x的值
static int n;
static int n2; // 记录换行的次数
public static void main(String[] args) {
Scanner as = new Scanner(System.in);
System.out.print("输入方程组的元数:");
n = as.nextInt();
a = new double[n + 1][n + 1];
b = new double[n + 1];
x = new double[n + 1];
System.out.println("输入方程组的系数矩阵a:");
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
a[i][j] = as.nextDouble();
System.out.println("输入方程组矩阵b:");
for (int i = 1; i <= n; i++)
b[i] = as.nextDouble();
solveEquation();
back();
print1();
}
// 打印二维矩阵
public static void print2() {
System.out.println("增广矩阵为:");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
System.out.print(a[i][j] + " ");
System.out.print(b[i] + " ");
System.out.print("\n");
}
}
// 打印一维矩阵
public static void print1() {
System.out.println("方程组的根为:");
for (int i = 1; i <= n; i++)
System.out.println("x" + i + " = " + x[i]);
}
// 消元
public static void solveEquation() {
for (int k = 1; k <= n - 1; k++) {
Wrap(k);
for (int i = k + 1; i <= n; i++) {
double l = a[i][k] / a[k][k];
for (int j = k ; j <= n; j++)
a[i][j] = a[i][j] - l * a[k][j];
b[i] = b[i] - l * b[k];
}
System.out.println("第" + k + "次消元后:");
print2();
}
}
public static void Wrap(int k) {// 换行
double max = Math.abs(a[k][k]);
int n1 = k; // 记住要交换的行
for (int i = k + 1; i <= n; i++) // 找到要交换的行
{
if (Math.abs(a[i][k]) > max) {
n1 = i;
max = Math.abs(a[i][k]);
}
}
if (n1 != k) {
n2++;
System.out.println("当k=" + k + "时,要交换的行是:" + k + "和" + n1);
for (int j = k; j <= n; j++) // 交换a的行
{
double x1;
x1 = a[k][j];
a[k][j] = a[n1][j];
a[n1][j] = x1;
}
double b1; // 交换b的行
b1 = b[k];
b[k] = b[n1];
b[n1] = b1;
System.out.println("交换后:");
print2();
}
}
public static void back() {
double sum = 0.0;
x[n] = b[n] / a[n][n];
for (int i = n - 1; i >= 1; i--) {
x[i] = (b[i] - jisuan(i)) / a[i][i];
}
}
public static double jisuan(int i) {
double he = 0.0;
for (int j = i + 1; j <= n; j++)
he = he + x[j] * a[i][j];
return he;
}
}
程序运行结果:
输入方程组的元数:5
输入方程组的系数矩阵a:
2 -1 0 0 0
-1 2 -1 0 0
0 -1 2 -1 0
0 0 -1 2 -1
0 0 0 -1 2
输入方程组矩阵b:
1 0 0 0 1
第1次消元后:
增广矩阵为:
2.0 -1.0 0.0 0.0 0.0 1.0
0.0 1.5 -1.0 0.0 0.0 0.5
0.0 -1.0 2.0 -1.0 0.0 0.0
0.0 0.0 -1.0 2.0 -1.0 0.0
0.0 0.0 0.0 -1.0 2.0 1.0
第2次消元后:
增广矩阵为:
2.0 -1.0 0.0 0.0 0.0 1.0
0.0 1.5 -1.0 0.0 0.0 0.5
0.0 0.0 1.3333333333333335 -1.0 0.0 0.3333333333333333
0.0 0.0 -1.0 2.0 -1.0 0.0
0.0 0.0 0.0 -1.0 2.0 1.0
第3次消元后:
增广矩阵为:
2.0 -1.0 0.0 0.0 0.0 1.0
0.0 1.5 -1.0 0.0 0.0 0.5
0.0 0.0 1.3333333333333335 -1.0 0.0 0.3333333333333333
0.0 0.0 0.0 1.25 -1.0 0.24999999999999994
0.0 0.0 0.0 -1.0 2.0 1.0
第4次消元后:
增广矩阵为:
2.0 -1.0 0.0 0.0 0.0 1.0
0.0 1.5 -1.0 0.0 0.0 0.5
0.0 0.0 1.3333333333333335 -1.0 0.0 0.3333333333333333
0.0 0.0 0.0 1.25 -1.0 0.24999999999999994
0.0 0.0 0.0 0.0 1.2 1.2
方程组的根为:
x1 = 1.0
x2 = 1.0
x3 = 0.9999999999999999
x4 = 1.0
x5 = 1.0
LU分解法:
package shuzhifenxi;
//LU分解法求解方程组
import java.util.Scanner;
public class Exam6122_LU {
static double[][] a;
static int n;
static double[][] l;
static double[][] u;
static double[] y;
static double[] x;
static double[] b;
public static void main(String[] args) {
inPut();
luResolve();
System.out.println("y的值为:");
backSolvey();
System.out.println("x的值为:");
backSolvex();
}
// lu分解法
public static void luResolve() {
for (int j = 0; j < n; j++) {
u[0][j] = a[0][j];
}
for (int i = 0; i < n; i++) {
l[i][0] = a[i][0] / u[0][0];
l[i][i] = 1;
}
for (int r = 1; r < n; r++) {
for (int i = r; i < n; i++) {
u[r][i] = a[r][i] - jisuan1(r, i);
}
for (int i = r + 1; i < n; i++) {
l[i][r] = (a[i][r] - jisuan2(r, i)) / u[r][r];
}
}
System.out.println("U的值为:");
print2(u);
System.out.println("L的值为:");
print2(l);
}
private static void inPut() {
Scanner as = new Scanner(System.in);
System.out.print("输入方程组的元数:");
n = as.nextInt();
a = new double[n][n];
b = new double[n];
l = new double[n][n];
u = new double[n][n];
y = new double[n];
x = new double[n];
System.out.println("输入方程组的系数矩阵a:");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i][j] = as.nextDouble();
System.out.println("输入方程组矩阵b:");
for (int i = 0; i < n; i++)
b[i] = as.nextDouble();
}
// 回代求出x的值
private static void backSolvex() {
x[n - 1] = y[n - 1] / u[n - 1][n - 1];
for (int i = n - 2; i >= 0; i--) {
x[i] = (y[i] - jisuanx(i)) / u[i][i];
}
print1(x);
}
private static double jisuanx(int i) {
double sum = 0;
for (int k = i + 1; k < n; k++) {
sum = sum + u[i][k] * x[k];
}
return sum;
}
// 回代求出y的值
private static void backSolvey() {
y[0] = b[0] / l[0][0];
for (int i = 1; i < n; i++) {
y[i] = (b[i] - jisuany(i)) / l[i][i];
}
print1(y);
System.out.println();
}
private static double jisuany(int i) {
double sum = 0;
for (int k = 0; k < i; k++) {
sum = sum + l[i][k] * y[k];
}
return sum;
}
// 打印一维矩阵
private static void print1(double[] y) {
for (int i = 0; i < n; i++) {
System.out.print(y[i] + " ");
}
}
// 打印二维矩阵
public static void print2(double array1[][]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
System.out.print(array1[i][j] + " ");
System.out.print("\n");
}
}
private static double jisuan1(int r, int i) {
double sum = 0;
for (int k = 0; k <= r - 1; k++) {
sum = sum + l[r][k] * u[k][i];
}
return sum;
}
private static double jisuan2(int r, int i) {
double sum = 0;
for (int k = 0; k <= r; k++) {
sum = sum + l[i][k] * u[k][r];
}
return sum;
}
}
程序运行结果:
输入方程组的元数:5
输入方程组的系数矩阵a:
2 -1 0 0 0
-1 2 -1 0 0
0 -1 2 -1 0
0 0 -1 2 -1
0 0 0 -1 2
输入方程组矩阵b:
1 0 0 0 1
U的值为:
2.0 -1.0 0.0 0.0 0.0
0.0 1.5 -1.0 0.0 0.0
0.0 0.0 1.3333333333333335 -1.0 0.0
0.0 0.0 0.0 1.25 -1.0
0.0 0.0 0.0 0.0 1.2
L的值为:
1.0 0.0 0.0 0.0 0.0
-0.5 1.0 0.0 0.0 0.0
0.0 -0.6666666666666666 1.0 0.0 0.0
0.0 0.0 -0.7499999999999999 1.0 0.0
0.0 0.0 0.0 -0.8 1.0
y的值为:
1.0 0.5 0.3333333333333333 0.24999999999999994 1.2
x的值为:
1.0 1.0 0.9999999999999999 1.0 1.0
追赶法(只能用来解三对角线方程):
package shuzhifenxi;
/*追赶法*/
import java.util.Scanner;
public class Exam6123_zhuigan {
static double[][] a;
static int n;
static double[][] l;
static double[][] u;
static double[] y;
static double[] x;
static double[] b;
public static void main(String[] args) {
inPut();
luResolve();
System.out.println("y的值为:");
backSolvey();
System.out.println("x的值为:");
backSolvex();
}
private static void inPut() {
Scanner as = new Scanner(System.in);
System.out.print("输入方程组的元数:");
n = as.nextInt();
a = new double[n][n];
b = new double[n];
l = new double[n][n];
u = new double[n][n];
y = new double[n];
x = new double[n];
System.out.println("输入方程组的系数矩阵a:");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i][j] = as.nextDouble();
System.out.println("输入方程组矩阵b:");
for (int i = 0; i < n; i++)
b[i] = as.nextDouble();
}
public static void luResolve() {
u[0][0] = a[0][0];
for (int i = 1; i < a.length; i++) {
u[i - 1][i] = a[i - 1][i];
}
for (int i = 0; i < a.length; i++) {
l[i][i] = 1;
}
for (int i = 1; i < l.length; i++) {
l[i][i - 1] = a[i][i - 1] / u[i - 1][i - 1];
u[i][i] = a[i][i] - l[i][i - 1] * u[i - 1][i];
}
}
private static void backSolvex() {
x[n - 1] = y[n - 1] / u[n - 1][n - 1];
for (int i = n - 2; i >= 0; i--) {
x[i] = (y[i] - jisuanx(i)) / u[i][i];
}
print1(x);
}
private static double jisuanx(int i) {
double sum = 0;
for (int k = i + 1; k < n; k++) {
sum = sum + u[i][k] * x[k];
}
return sum;
}
private static void backSolvey() {
y[0] = b[0] / l[0][0];
for (int i = 1; i < n; i++) {
y[i] = (b[i] - jisuany(i)) / l[i][i];
}
print1(y);
System.out.println();
}
private static double jisuany(int i) {
double sum = 0;
for (int k = 0; k < i; k++) {
sum = sum + l[i][k] * y[k];
}
return sum;
}
private static void print1(double[] y) {
for (int i = 0; i < y.length; i++) {
System.out.print(y[i] + " ");
}
}
public static void print2(double array1[][]) {
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[i].length; j++)
System.out.print(array1[i][j] + " ");
System.out.print("\n");
}
}
}
程序运行结果:
输入方程组的元数:5
输入方程组的系数矩阵a:
2 -1 0 0 0
-1 2 -1 0 0
0 -1 2 -1 0
0 0 -1 2 -1
0 0 0 -1 2
输入方程组矩阵b:
1 0 0 0 1
y的值为:
1.0 0.5 0.3333333333333333 0.24999999999999994 1.2
x的值为:
1.0 1.0 0.9999999999999999 1.0 1.0