| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 784 人关注过本帖
标题:求助,高斯消元法解线性方程组结果不对
只看楼主 加入收藏
zjuyx
Rank: 1
来 自:Hangzhou
等 级:新手上路
帖 子:73
专家分:0
注 册:2008-12-16
结帖率:100%
收藏
 问题点数:0 回复次数:0 
求助,高斯消元法解线性方程组结果不对
程序运行没有任何错误,但解出来的结果却不对,请各位指教!
下面是代码,谢谢!
/* @author :young
 * @date :2009
 * @version :0.0
 * this class is used to solve linear equation group,the parameter
 * you need to input is the number of equation n,
 * the coefficient matrix A, constant vector b
 * the algorithm is Gauss elimination
 */
package computation;
import java.util.Scanner;
public class linearVersion3 {
    private int n;
    private double[][] A;
    private double[] b;
    private Scanner input;
    public linearVersion3(){
        super();
        input=new Scanner(System.in);
        System.out.println("Input n,e");
        n=input.nextInt();        
        A=new double[n][n];
        b=new double[n];
        System.out.println("input matrix A");//initialize coefficient matrix A
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                A[i][j]=input.nextDouble();
        System.out.println("input vector b");//initialize vector b
        for(int i=0;i<n;i++)
            b[i]=input.nextDouble();   
        for(int k=0;k<n-1;k++){ //elimination coefficient matrix A to make sure matrix A is upper triangle matrix            
            double max=A[k][k];//initialize max
            int inc=0;
            for(int i=k+1;i<n;i++)    //find max               
                if(A[i][k]>max){
                    max=A[i][k];
                    inc++;
                }//end for            
            if(inc>0){//change row k with row k+inc
                for(int j=k;j<n;j++)                    
                    exchange(A[k][j],A[k+inc][j]);               
                exchange(b[k],b[k+inc]);               
            }//end if
            for(int i=k+1;i<n;i++){
                double m=0;               
                m=A[i][k]/A[k][k];
                for(int j=k;j<n;j++)
                    A[i][j]-=m*A[k][j];
                b[i]-=m*b[k];               
            }//end for
        }
        b[n-1]/=A[n-1][n-1];//backwards to calculate the roots and store then in vector b
        for(int i=n-2;i>=0;i--){
            double sum=0;
            for(int j=i+1;j<n;j++)
                sum+=A[i][j]*b[j];
            b[i]=(b[i]-sum)/A[i][i];
        }        
        for(int i=0;i<n;i++)//print the result
            System.out.printf("x[%d]=%f  ", i+1,b[i]);
    }
    public void exchange(double a,double b){
        double temp=a;
        a=b;
        b=temp;
    }
    public static void main(String args[]){
        new linearVersion3();
    }
}

[ 本帖最后由 zjuyx 于 2009-12-19 21:00 编辑 ]
搜索更多相关主题的帖子: 结果 线性方程 高斯 
2009-12-19 20:59
快速回复:求助,高斯消元法解线性方程组结果不对
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.069685 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved