解线性方程组的直接方法——高斯消去和LU消去法

高斯消去法:


/**
 * 
 * 高斯消元算法
 * @author luzi
 *
 */
public class guess {
	
	double[] x2 = {0,0,0,0,0,0,0,0};
	
	
	
	//列主元高斯消去法
	public void guess1(double[][] A,double[] b,int n,int target){	
		//选主元,消元
		double max = Double.MIN_VALUE;
		for(int k = 0; k < n - 1; k++){
			int t = k;
			int p = k;
			//选主元
			for(; t < n;t++){
				if(A[t][k] > max){
					max = A[t][k];
					p = t;
				}
			}
			//主元交换
			if(p != k){
				double temp = 0;
				for(int i = 0;i < n;i++){
					temp = A[p][i];
					A[p][i] = A[k][i];
					A[k][i] = temp;
				}
				temp = b[p];
				b[p] = b[k];
				b[k] = temp;
			}
			for(int i = k + 1; i < n; i++){
				double tp = A[i][k]/A[k][k];
				 b[i] = b[i] - tp*b[k];
				 //A[i][k] = 0;
				 for(int j = k + 1;j < n;j++){
					 A[i][j] = A[i][j] - tp*A[k][j];
				 }
			}
		}
		
		//回代
		x2[n - 1] = b[n - 1]/A[n - 1][n - 1];
		for(int i = n-2;i >= 0;i--){
			double sum = 0;
			for(int j = i + 1; j < n;j++){
				sum += A[i][j]*x2[j];
			}
			x2[i] = (b[i] - sum)/A[i][i];
		}
		for(int i = 0; i < n; i++)
			System.out.println(x2[i]);
	}
	
	//顺序高斯消去法
	public double[] guess2(double[][] A,double[] b,int n,int target){
		double[] x = new double[n];
		for(int k = 0; k < n - 1; k++){
			for(int i = k + 1; k < n; k++){
				double tp = A[i][k]/A[k][k];
				b[i] = b[i] - tp*b[k];
				
				for(int j = k ; j < n; j++){
					A[i][j] = A[i][j] - tp*A[k][j];
				}
			}			
		}
		
		x[n - 1] = b[n - 1]/A[n - 1][n - 1];
		
		for(int i = n - 2; i >= 0; i--){
			double sum = 0;
			for(int j = i + 1; j < n; j++){
				sum += A[i][j]*x[j];
			}
			x[i] = (b[i] - sum)/A[i][i];			
		}	
		return x;
	}
}

直接LU消去法:


package jisuanfangfashiyan;

/**
 * 
 * @author luzi
 *
 */

public class LU {
	
	
	//直接lU分解消去法
	public double[] getLU(double[][] A,double[] x,double[] b,int n){
		
		double[][] u = new double[n][n];
		double[][] l = new double[n][n];
		double[] y = new double[n];
		
		
		double sum = 0;
		double temp = 0;
		
		for(int k = 0; k < n; k++){
			
			//先求  u 的第  k 行的值
			for( int j = k; j < n; j++){
				for(int m = 0; m < k; m++){
					sum += (l[k][m] * u[m][j]);
				}
				u[k][j] = A[k][j] - sum;
				sum = 0;
			}
			
			//求 l  的第 k 的值
			for(int i = k + 1; i < n; i++){
				for(int m = 0; m < k; m++){
					sum += (l[i][m] * u[m][k]);
				}
				l[i][k] = ( A[i][k] - sum ) /u[k][k];
				sum = 0;
			}			
		}
		
		y[0] = b[0];
		for(int i = 1; i < n; i++){
			for(int j = 0; j < i; j++){
				sum += (l[i][j] * y[j]);
			}
			y[i] = b[i] - sum;
			sum = 0;
		}
		
		x[n - 1] = y[n - 1]/u[n - 1][n - 1];
		for(int i = n - 2; i >= 0; i--){
			for(int j = i + 1; j < n; j++){
				sum += (u[i][j] * x[j]);
			}
			x[i] = (y[i] - sum)/u[i][i];
			sum = 0; 
		}			
		return x;
	}
	
	
	public static void main(String args[]){
		
		double[][] A =  {
				{2,-3,2},
				{-4,2,-6},
				{2,2,4}
				};
		double[] x = new double[3];
		double[] b = {5,14,8};
		
		LU lu = new LU();
		
		x = lu.getLU(A, x, b, 3);
		for(double ele : x)
			System.out.println(ele);	
	}

}
LU消去法例子运行结果:




你可能感兴趣的:(数学与编程)