解:
Gauss迭代:
package shuzhifenxi;
import java.util.Scanner;
public class Jacobi_Gauss {
/**
* @Jacobi迭代法求线性方程组
*/
static double a[][];
static double b[];
static double x[];
static double x0[];
static boolean th = true;
static int count = 0;
static int n = 0;
static double e =0;
public static void main(String[] args) {
inPut();
iteration(e);
}
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];
x0 = 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();
System.out.println("输入精度10^(-e):");
e=as.nextDouble();
}
private static void iteration(double n) {
double sum = 0;
for (int i = 0; i < x0.length; i++) {
x0[i] = 1; //初值都为1
}
while (th) {
count++;
for (int i = 0; i < x0.length; i++) {
sum = 0;
for (int j = 0; j < x0.length; j++) {
if (i < j) {
sum = sum + a[i][j] * x0[j];
}
else if(i>j){
sum = sum + a[i][j] * x[j];
}
}
x[i] = (b[i] - sum) / a[i][i];
}
System.out.println("第" + count + "次迭代的值");
print1(x);
if (jisuan() < Math.pow(10, -n)) {
th = false;
}
for (int i = 0; i < a.length; i++) {
x0[i] = x[i];
}
}
}
/*
* 求出前一次迭代和后一次迭代对应x的最大误差值max, 如果max小于精度要求则所求的x值都满足要求
*/
public static double jisuan() {
double max = 0.0;
for (int i = 0; i < x.length; i++) {
double x3 = Math.abs(x[i] - x0[i]);
if (x3 > max)
max = x3;
}
return max;
}
private static void print1(double[] y) {
for (int i = 0; i < y.length; i++) {
System.out.println("x[" + i + "]=" + y[i]);
}
}
}
输入方程组的元数:3
输入方程组的系数矩阵a:
8 -2 0
-2 8 -2
0 -2 8
输入方程组矩阵b:
1 2 1
输入精度10^(-e):
5
第1次迭代的值
x[0]=0.375
x[1]=0.59375
x[2]=0.2734375
第2次迭代的值
x[0]=0.2734375
x[1]=0.38671875
x[2]=0.2216796875
第3次迭代的值
x[0]=0.2216796875
x[1]=0.36083984375
x[2]=0.2152099609375
第4次迭代的值
x[0]=0.2152099609375
x[1]=0.35760498046875
x[2]=0.2144012451171875
第5次迭代的值
x[0]=0.2144012451171875
x[1]=0.35720062255859375
x[2]=0.21430015563964844
第6次迭代的值
x[0]=0.21430015563964844
x[1]=0.3571500778198242
x[2]=0.21428751945495605
第7次迭代的值
x[0]=0.21428751945495605
x[1]=0.357143759727478
x[2]=0.2142859399318695
第8次迭代的值
x[0]=0.2142859399318695
x[1]=0.35714296996593475
x[2]=0.2142857424914837
Jacobi迭代:
package shuzhifenxi;
import java.util.Scanner;
public class Jacobi {
/**
* @Jacobi迭代法求线性方程组
*/
static double a[][];
static double b[];
static double x[];
static double x0[];
static boolean th = true;
static int count = 0;
static int n = 0;
static double e =0;
public static void main(String[] args) {
inPut();
iteration(e);
}
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];
x0 = 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();
System.out.println("输入精度10^(-e):");
e=as.nextDouble();
}
private static void iteration(double n) {
double sum = 0;
for (int i = 0; i < x0.length; i++) {
x0[i] = 1; //初值都为1
}
while (th) {
count++;
for (int i = 0; i < x0.length; i++) {
sum = 0;
for (int j = 0; j < x0.length; j++) {
if (i!=j) {
sum = sum + a[i][j] * x0[j];
}
}
x[i] = (b[i] - sum) / a[i][i];
}
System.out.println("第" + count + "次迭代的值");
print1(x);
if (jisuan() < Math.pow(10, -n)) {
th = false;
}
for (int i = 0; i < a.length; i++) {
x0[i] = x[i];
}
}
}
/*
* 求出前一次迭代和后一次迭代对应x的最大误差值max, 如果max小于精度要求则所求的x值都满足要求
*/
public static double jisuan() {
double max = 0.0;
for (int i = 0; i < x.length; i++) {
double x3 = Math.abs(x[i] - x0[i]);
if (x3 > max)
max = x3;
}
return max;
}
private static void print1(double[] y) {
for (int i = 0; i < y.length; i++) {
System.out.println("x[" + i + "]=" + y[i]);
}
}
}
程序运行结果:
输入方程组的元数:3
输入方程组的系数矩阵a:
8 -2 0
-2 8 -2
0 -2 8
输入方程组矩阵b:
1 2 1
输入精度10^(-e):
5
第1次迭代的值
x[0]=0.375
x[1]=0.75
x[2]=0.375
第2次迭代的值
x[0]=0.3125
x[1]=0.4375
x[2]=0.3125
第3次迭代的值
x[0]=0.234375
x[1]=0.40625
x[2]=0.234375
第4次迭代的值
x[0]=0.2265625
x[1]=0.3671875
x[2]=0.2265625
第5次迭代的值
x[0]=0.216796875
x[1]=0.36328125
x[2]=0.216796875
第6次迭代的值
x[0]=0.2158203125
x[1]=0.3583984375
x[2]=0.2158203125
第7次迭代的值
x[0]=0.214599609375
x[1]=0.35791015625
x[2]=0.214599609375
第8次迭代的值
x[0]=0.2144775390625
x[1]=0.3572998046875
x[2]=0.2144775390625
第9次迭代的值
x[0]=0.214324951171875
x[1]=0.35723876953125
x[2]=0.214324951171875
第10次迭代的值
x[0]=0.2143096923828125
x[1]=0.3571624755859375
x[2]=0.2143096923828125
第11次迭代的值
x[0]=0.21429061889648438
x[1]=0.35715484619140625
x[2]=0.21429061889648438
第12次迭代的值
x[0]=0.21428871154785156
x[1]=0.3571453094482422
x[2]=0.21428871154785156
SORT迭代:
先计算出Jacobi迭代矩阵:
package shuzhifenxi;
/*
*由方程组稀疏矩阵,计算Jacobi迭代矩阵B,由Matlab计算B的特征值,最大特征值为m
*则w=2/(1+(1-m^2))^(1/2) */
import java.util.Scanner;
class Jacobi_Sort_Help {
static double a[][];
static double D[][];
static double L[][];
static double U[][];
static double LU[][];
static double B[][];
static int n;
public static void main(String[] args) {
inPut();
polyeig();
System.out.println("B:");
print2(B);
}
private static void inPut() {
Scanner as = new Scanner(System.in);
System.out.print("输入方程组的元数:");
n = as.nextInt();
a = new double[n][n];
D = new double[n][n];
L = new double[n][n];
U = new double[n][n];
LU = new double[n][n];
B = new double[n][n];
System.out.println("输入方程组的系数矩阵a:");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i][j] = as.nextDouble();
}
private static void polyeig() {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (i > j) {
L[i][j] = a[i][j];
} else if (i < j) {
U[i][j] = a[i][j];
} else if (i == j) {
D[i][j] = a[i][j];
}
LU[i][j] = L[i][j] + U[i][j];
}
}
jiuji();
}
private static void jiuji() {
for (int k = 0; k < a.length; k++) {
for (int i = 0; i < a.length; i++) {
double sum = 0;
for (int j = 0; j < a.length; j++) {
sum = sum - D[k][j] * LU[j][i]/Math.pow(D[j][j], 2);
}
B[k][i] = sum;
}
}
}
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");
}
}
}
运行结果:
输入方程组的元数:3
输入方程组的系数矩阵a:
8 -2 0
-2 8 -2
0 -2 8
B:
0.0 0.25 0.0
0.25 0.0 0.25
0.0 0.25 0.0
在matlab中计算出B的特征值,将最大的特征值赋值给m,在JSort中输入m的值:
>> b=[0 0.25 0;0.25 0 0.25;0 0.25 0];
>> p=poly(b);
>> r=roots(p)
r =
0.3536
-0.3536
-0.0000
在已知最大特征值m的情况下:
package shuzhifenxi;
/*
*由方程组稀疏矩阵,在Jacobi_Sort_Help中,计算Jacobi迭代矩阵B,由Matlab计算B的特征值,最大特征值为m
*则w=2/(1+(1-m^2))^(1/2) */
import java.util.Scanner;
public class JSort {
/**
* @SORT迭代法求线性方程组
*/
static double a[][];
static double b[];
static double x[];
static double x0[];
static boolean th = true;
static int count = 0;
static int n ;
static double e ;
static double m ;
static double w;
public static void main(String[] args) {
inPut();
iteration(e);
}
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];
x0 = 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();
System.out.println("输入精度10^(-e):");
e = as.nextDouble();
System.out.println("输入最大特征值m:");
m = as.nextDouble();
w =2.0/(1+Math.pow(1-Math.pow(m, 2), 1.0/2));
}
private static void iteration(double n) {
double sum = 0;
for (int i = 0; i < x0.length; i++) {
x0[i] = 1; //x的初始值都设为1
}
while (th) {
for (int i = 0; i < x0.length; i++) {
sum = 0;
for (int j = 0; j < x0.length; j++) {
if (i < j) {
sum = sum + a[i][j] * x0[j];
} else if (i > j) {
sum = sum + a[i][j] * x[j];
}
}
x[i] = (1 - w) * x0[i] + w * (b[i] - sum) / a[i][i];
}
System.out.println(w);
count++;
System.out.println("第" + count + "次迭代的值");
print1(x);
if (jisuan() < Math.pow(10, -n)) {
th = false;
}
for (int i = 0; i < a.length; i++) {
x0[i] = x[i];
}
}
}
/*
* 求出前一次迭代和后一次迭代对应x的最大误差值max, 如果max小于精度要求则所求的x值都满足要求
*/
public static double jisuan() {
double max = 0.0;
for (int i = 0; i < x.length; i++) {
double x3 = Math.abs(x[i] - x0[i]);
if (x3 > max)
max = x3;
}
return max;
}
private static void print1(double[] y) {
for (int i = 0; i < y.length; i++) {
System.out.println("x[" + i + "]=" + y[i]);
}
}
}
运行结果:
输入方程组的元数:3
输入方程组的系数矩阵a:
8 -2 0
-2 8 -2
0 -2 8
输入方程组矩阵b:
1 2 1
输入精度10^(-e):
5
输入最大特征值m:
0.3536
第1次迭代的值
x[0]=0.3541375876659387
x[1]=0.5747997327999863
x[2]=0.24428923954638337
第2次迭代的值
x[0]=0.2658480362778789
x[1]=0.37094962711148777
x[2]=0.2168511103243652
第3次迭代的值
x[0]=0.21613148071342334
x[1]=0.35782159071060604
x[2]=0.21437542912554006
第4次迭代的值
x[0]=0.2143994502610141
x[1]=0.3571727616052679
x[2]=0.21429044528423766
第5次迭代的值
x[0]=0.21428964346210488
x[1]=0.3571440962486809
x[2]=0.21428587648239777
第6次迭代的值
x[0]=0.21428590324710808
x[1]=0.35714290650159974
x[2]=0.21428572162319437