LoginSignup
1
0

More than 3 years have passed since last update.

乱数行列をPivotGauss消去法とGauss消去法で解き、精度比較する際のコード

Last updated at Posted at 2018-03-07

学校の課題を提出したので、コードを記事にして保存しました.
肝心のPivotGauss消去法とGauss消去法のコードは別記事です.
条件:
ax=bを解く.
aは500*500の乱数行列(成分は1~9)
(b-ax)の2ノルムを誤差としてerr1(Gauss),err2(PivotGauss)に放り込む.
・言語はJava.
ソース全体

    public static void main(String[] args) {
        // TODO 自動生成されたメソッド・スタブ

        long t0;
        long time;
        int n =500;
        int m=100;
        double [][]a =new double[n][n];
        double []b =new double[n];
        double []x1 =new double[n];
        double []x2 =new double[n];
        double err1=0.0;
    //  double []err2 =new double[n];
    //  double []err1 = new double[n];
        double err2 =0.0;
        //err1をだす
        t0 = System.currentTimeMillis();
        for(int s=0;s<m;s++){
            for(int j=0;j<n;j++){
                for(int i=0;i<n;i++){
                    a[i][j]=Math.abs((Math.random()));  
                    b[i]  =Math.abs((Math.random()));
                }
            }

            x1 =Calc.Gauss(a, b);
            err1 = Calc.vecNorm2(Calc.subVec(b,Calc.matVec(a, x1)));
            x2 =Calc.pivotGauss(a, b);
            err2 =Calc.vecNorm2(Calc.subVec(b,Calc.matVec(a, x2)));
            System.out.println(err1);
            System.out.println(err2);
        //System.out.println("\n"+err2);
        }
        //System.out.println(Math.abs((Math.random())));
        time = System.currentTimeMillis()-t0;
        System.out.println("\n処理時間:"+time);
        //Calc.printVec2(err1);
    }
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0