0

我究竟如何格式化我的代码,以便它需要

1x+2y+3z+4w=5e
6x+7y+8z+9w=10e
11x+12y+13z+14w=15e
16x+17y+18z+19w=20e

并返回x,y,z,w

我试图遵循这个答案中描述的格式,目前我拥有的是以下代码,它IllegalArgumentException: "java.lang.IllegalArgumentException: Can't solve for wide systems. More variables than equations.在尝试计算 4 个结果时抛出:

                    double[20] args = {1,2,3... ,20};
                    SimpleMatrix A = new SimpleMatrix(4,5);
                    SimpleMatrix b = new SimpleMatrix(4,1);
                    int val=0;
                    for(int i =0;i<4;i++){
                        for(int j=0;j<5;j++){
                            A.setRow(i, j, args[val]);
                            val++;
                        }
                        b.setRow(i,0, args[val-1]);
                    }
                    double[] result = new double[4];  //results for x y z w
                    try {
                        SimpleMatrix solution = A.solve(b);   //throws IllegalArgumentException!
                        for(int i=0;i<solution.getNumElements();i++) {
                            result[i] = solution.get(i, 0);
                        }

                        --print results--
                    }
                    catch ( SingularMatrixException e ) {
                       throw new IllegalArgumentException();
                    }

我究竟做错了什么?

4

1 回答 1

1

我认为这可能与您的矩阵尺寸有关。

new SimpleMatrix A(4,5)应替换为new SimpleMatrix A(4,4).

于 2016-01-19T15:42:44.423 回答