Gauss-Jordan消去法中完全选主元法求解线性方程组

本文介绍了一种使用完全主元法(Gauss-Jordan消去法的一种变体)来求解线性方程组的方法。通过在每一步选择系数矩阵中绝对值最大的元素作为主元,并通过初等行变换将其置于主对角线上,可以有效地避免数值计算中的误差累积。文章详细展示了算法步骤,并给出具体实例,最后提供了完整的Java实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gauss-Jordan消去法中完全选主元法求解线性方程组

分类: 数值分析 1245人阅读 评论(0) 收藏 举报
算法名称: 完全主元法
 
算法描述:
 
1.       在系数矩阵A 中找到绝对值最大的数作为主元,并记录它的列号col ,行号row ,将第col 行与第row 行交换,即将此主元经过一次初等行变换放到主对角线上。
2.       移动过后,主元所在行每个元除以主元的值,使主元所在位置值为1
3.       进行初等行变换,使得主元所在列的其他元为0 ,主元所在行不变。
4.       返回1 进行迭代,迭代总次数恰好为A 的行数。
 
:每次初等行变换都要求增广阵 b 同时进行相应变换。
 
实际例子
 
Coefficient matrix:
 | 0.0 2.0 0.0 1.0 | 0.0 |
 | 2.0 2.0 3.0 2.0 | -2.0 |
 | 4.0 -3.0 0.0 1.0 | -7.0 |
 | 6.0 1.0 -6.0 -5.0 | 6.0 |
-----------------------------------------------
After 1 time(s) elimination:
 | 0.0 2.0 0.0 1.0 | 0.0 |
 | 5.0 2.5 0.0 -0.5 | 1.0 |
 | -1.0 -0.16666666666666666 1.0 0.8333333333333333 | -1.0 |
 | 4.0 -3.0 0.0 1.0 | -7.0 |
-----------------------------------------------
After 2 time(s) elimination:
 | 1.0 0.5 0.0 -0.1 | 0.2 |
 | 0.0 2.0 0.0 1.0 | 0.0 |
 | 0.0 0.33333333333333337 1.0 0.7333333333333333 | -0.8 |
 | 0.0 -5.0 0.0 1.4 | -7.8 |
-----------------------------------------------
After 3 time(s) elimination:
 | 1.0 0.0 0.0 0.03999999999999998 | -0.5800000000000001 |
 | -0.0 1.0 -0.0 -0.27999999999999997 | 1.56 |
 | 0.0 0.0 1.0 0.8266666666666667 | -1.3200000000000003 |
 | 0.0 0.0 0.0 1.56 | -3.12 |
-----------------------------------------------
After 4 time(s) elimination:
 | 1.0 0.0 0.0 0.0 | -0.5000000000000001 |
 | 0.0 1.0 0.0 0.0 | 1.0 |
 | 0.0 0.0 1.0 0.0 | 0.33333333333333304 |
 | 0.0 0.0 0.0 1.0 | -2.0 |
-----------------------------------------------
The final solution matrix:
 | 1.0 0.0 0.0 0.0 | -0.5000000000000001 |
 | 0.0 1.0 0.0 0.0 | 1.0 |
 | 0.0 0.0 1.0 0.0 | 0.33333333333333304 |
 | 0.0 0.0 0.0 1.0 | -2.0 |
-----------------------------------------------

代码

package  com.nc4nr.chapter02.guassj;

public   class  GuassJ  {
    
    
// 4 * 4 coefficient matrix a
    double[][] a = {
            
{0.02.00.01.0},
            
{2.02.03.02.0},
            
{4.0-3.00.01.0},
            
{6.01.0-6.0-5.0}
    }
;
    
    
// 4 * 1 coefficient matrix b
    double[][] b = {
            
{0.0},
            
{-2.0},
            
{-7.0},
            
{6.0}
    }
;
    
    
private void gaussj(double[][] a, int anrow, 
                        
double[][] b, int bncol) {
        
int icol = 0, irow = 0;
        
double big, dum, pivinv;
        
int n = anrow;
        
int m = bncol;
        
int[] indxr = new int[n], 
                indxc 
= new int[n], 
                ipiv 
= new int[n];
        System.out.println(
"Coefficident matrix :");
        output(a,
4,b,1);
        
for (int i = 0; i < n; i++) ipiv[i] = 0;
        
for (int i = 0; i < n; i++{
            big 
= 0.0;
            
for (int j = 0; j < n; j++{
                
if (ipiv[j] != 1{
                    
for (int k = 0; k < n; k++{
                        
if (ipiv[k] == 0{
                            
if (Math.abs(a[j][k]) >= big) {
                                big 
= Math.abs(a[j][k]);
                                irow 
= j;
                                icol 
= k;
                            }

                        }

                    }

                }

            }

            ipiv[icol]
= ipiv[icol] + 1;
            
if (irow != icol) {
                
for (int l = 0; l < n; l++{
                    
double mid = a[irow][l];
                    a[irow][l] 
= a[icol][l];
                    a[icol][l] 
= mid;
                }

                
for (int l = 0; l < m; l++{
                    
double mid = b[irow][l];
                    b[irow][l] 
= b[icol][l];
                    b[icol][l] 
= mid;
                }

            }

            indxr[i] 
= irow;
            indxc[i] 
= icol;
            
if (a[icol][icol] == 0.0) System.out.println("gaussj: Singular Matrix");
            pivinv 
= 1.0 / a[icol][icol];
            a[icol][icol] 
= 1.0;
            
for (int l = 0; l < n; l++{
                
if (l != icol) {
                    a[icol][l] 
*= pivinv;
                }

            }

            
for (int l = 0; l < m; l++) b[icol][l] *= pivinv;
            
for (int ll = 0; ll < n; ll++{
                
if (ll != icol) {
                    dum 
= a[ll][icol];
                    a[ll][icol]
=0.0;
                    
for (int l = 0; l < n; l++{
                        
if (l != icol) {
                            a[ll][l] 
-= a[icol][l]*dum;
                        }

                    }

                    
for (int l = 0; l < m; l++) b[ll][l] -= b[icol][l]*dum;
                }

            }

            System.out.println(
"After " + (i + 1+ " time(s) elimination:");
            output(a,
4,b,1); 
        }
                    
        
for (int l=n-1;l>=0;l--{
            
if (indxr[l] != indxc[l]) {
                
for (int k = 0; k < n; k++{
                    
double mid = a[k][indxr[l]];
                    a[k][indxr[l]] 
= a[k][indxc[l]];
                    a[k][indxc[l]] 
= mid;
                }

            }

        }

        System.out.println(
"The final solution matrix:");
        output(a,
4,b,1);
    }

    
    
private void output(double a[][], int anrow, double b[][], int bncol) {
        
for (int i = 0; i < anrow; i++{
            System.out.println(
" | " + a[i][0+ " " + 
                    a[i][
1+ " " + 
                    a[i][
2+ " " + 
                    a[i][
3+ " | " + b[i][0+ " | ");
        }

        System.out.println(
"-----------------------------------------------");
    }

    
    
public GuassJ() {
        gaussj(a, 
4, b, 1);
    }

    
    
public static void main(String[] args) {
        
new GuassJ();
    }


}

 

### 带有列元的高斯消去法C++实现 带有列元的高斯消去法是一种用于解线性方程组的有效方法。该方法通过择每一步中的最大绝对值作为元来提高数值稳定性。 #### 高斯消去法概述 高斯消去法的核心思想是通过对增广矩阵进行一系列初等变换,将其转换成上三角矩阵,从而简化求解过程。为了减少舍入误差的影响,在每次元时,会择当前列中绝对值最大的元素作为元[^1]。 #### 列元策略 在标准的高斯消去过程中加入列元意味着对于每一行操作之前先扫描这一列的所有未处理过的元素,找出具有最大绝对值得那个位置,并将它所在的两行互换使得这个大数位于对角线上参与后续运算。这样做可以有效降低由于浮点数精度不足带来的累积错误风险。 以下是采用列元技术的高斯消去法的一个简单C++实现: ```cpp #include <iostream> #include <vector> #include <cmath> using namespace std; void swapRows(vector<vector<double>>& matrix, int rowA, int rowB){ if(rowA != rowB){ for(size_t col = 0; col < matrix[rowA].size(); ++col){ double temp = matrix[rowA][col]; matrix[rowA][col] = matrix[rowB][col]; matrix[rowB][col] = temp; } } } // Perform Gaussian Elimination with partial pivoting. bool gaussianEliminationWithPartialPivoting(const vector<vector<double>>& A, const vector<double>& b, vector<double>& result) { size_t n = A.size(); // Create augmented matrix [A|b]. vector<vector<double>> Ab(n, vector<double>(n + 1)); for (size_t i = 0; i < n; ++i) { copy(A[i].begin(), A[i].end(), Ab[i].begin()); Ab[i][n] = b[i]; } try{ for (int k = 0; k < static_cast<int>(n); ++k) { // Find the maximum element in column 'k'. int maxRowIdx = k; for(int r=k+1;r<n;++r){ if(fabs(Ab[r][k])>fabs(Ab[maxRowIdx][k])){ maxRowIdx=r; } } // Swap rows to bring largest magnitude entry into diagonal position. swapRows(Ab,k,maxRowIdx); // Check whether current pivot is zero or not. if(abs(Ab[k][k])<1e-14){throw runtime_error("Singular Matrix");} // Apply elementary operations on remaining part of matrix below this point. for(int j=k+1;j<n;++j){ double factor=-Ab[j][k]/Ab[k][k]; for(int c=k;c<=static_cast<int>(n);++c){ Ab[j][c]+=factor*Ab[k][c]; } } } // Back substitution phase starts here... result.resize(n); for(int i=n-1;i>=0;--i){ double sum=0.; for(int j=i+1;j<n;++j){ sum+=result[j]*Ab[i][j]; } result[i]=(Ab[i][n]-sum)/Ab[i][i]; } return true; }catch(exception& e){ cerr << "Error during computation: "<<e.what()<<endl; return false; } } ``` 此函数接受系数矩阵`A`及其对应的右侧向量`b`, 并返回解向量`x`. 如果输入的是奇异矩阵,则会抛出异常并打印相应的错误消息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值