首页 > 其他分享 >高斯消去

高斯消去

时间:2022-09-18 10:55:29浏览次数:54  
标签:int double ++ System WrapRowIndex static 高斯消

import java.util.Scanner;

public class GaoSi {
/**
* 列主元高斯消去法
*/
static double A[][];
static double b[];
static double x[];

static int n; //n表示未知数的个数
static int n_2; //记录换行的次数

public static void main(String[] args) {
System.out.println("--------------输入方程组未知数的个数---------------");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();

A = new double[n][n];
b = 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] = sc.nextDouble();
}
}

System.out.println("--------------输入方程组的常量向量b:---------------");
for(int i = 0; i < n; i++) {
b[i] = sc.nextDouble();
}

Elimination();
BackSubstitution();
PrintRoot();
}


//消元法
public static void Elimination() {
PrintA();
for(int k = 0; k < n; k++) {
WrapRow(k);
for(int i = k+1; i < n; i++) {
double l = A[i][k] / A[k][k];
A[i][k] = 0;

for(int j = k+1; j < n; j++) {
A[i][j] = A[i][j] - l * A[k][j];
}
b[i] = b[i] - l * b[k];
}
//System.out.println("第" + k + "次消元后:");
//PrintA();
}
}

//回代法
public static void BackSubstitution() {
x[n-1] = b[n-1] / A[n-1][n-1];
for(int i = n - 2; i >= 0; i--) {
x[i] = (b[i] - solve(i)) / A[i][i];
}
}

public static double solve(int i) {
double result = 0.0;
for(int j = i; j < n; j++)
result += A[i][j] * x[j];
return result;
}


//输出方程组的根
public static void PrintRoot() {
System.out.println("--------------方程组的根为---------------");
for(int i = 0; i < n; i++) {
System.out.println("x" + (i+1) + " = " + x[i]);
}
}

//交换Swap函数???
public static void Swap(double[] ar, int x, int y) {
Double tmp = ar[x];
ar[x] = ar[y];
ar[y] = tmp;
}

public static void PrintA() { //输出A的增广矩阵
//System.out.println("--------------增广矩阵---------------");
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
System.out.print(A[i][j] + " ");
}
System.out.println(b[i]);
}
}

//交换矩阵的行
public static void WrapRow(int k) { //k表示第k+1轮消元
double maxElement = Math.abs(A[k][k]);

int WrapRowIndex = k; // 记住要交换的行
for(int i = k + 1; i < n; i++) {
if (Math.abs(A[i][k]) > maxElement) {
WrapRowIndex = i;
maxElement = A[i][k];
}
}
if (WrapRowIndex != k) { //交换求得最大主元
n_2 += 1;
System.out.println("k = " + k + "时," + "要交换的行为" + k + "和"+ WrapRowIndex);

//先交换A
for(int j = k; j < n; j++) {
double[] arr = {A[k][j], A[WrapRowIndex][j]};
Swap(arr, 0, 1);
A[k][j] = arr[0]; A[WrapRowIndex][j] = arr[1];
// double tmp = A[k][j];
// A[k][j] = A[WrapRowIndex][j];
// A[WrapRowIndex][j] = tmp;
}

//再交换b
double[] arr = {b[k], b[WrapRowIndex]};
Swap(arr, 0, 1);
b[k] = arr[0]; b[WrapRowIndex] = arr[1];
// double tmp = b[k];
// b[k] = b[WrapRowIndex];
// b[WrapRowIndex] = tmp;
System.out.println("--------------交换后---------------");
PrintA();
}
}
}

标签:int,double,++,System,WrapRowIndex,static,高斯消
From: https://www.cnblogs.com/a111l/p/16704376.html

相关文章

  • 高斯消去法(Gauss-Jordan方法)的Python实现
    高斯消去法的改进形式为Gauss-JordanEliminationMethod,要求每一行的主元素所在列元素全部消去为0,除了主元素本身。区别如下:代码实现如下:#-*-coding:utf-8-*-#@......
  • 数论---高斯消元
    1#include<iostream>2#include<algorithm>3#include<cstring>4#include<cmath>5usingnamespacestd;6constdoubleac=1e-8;7constintN=1......
  • 高斯消元
    writtenon2022-08-13高斯消元主要用于解决\(n\)元一次方程组相关的问题。一般时间复杂度\(O(n^3)\),此过程需要用到矩阵的初等行变换。接下来我们来看一下高斯消元......