0

我正在尝试使用 Apache 的 commons-math-2.2.jar 求解线性方程。

对于三个小点,我得到了正确的结果。

当我使用以下数据(大数字)时,我没有得到正确的结果,而是我会说结果没有意义;

以下是我正在使用的代码和数据:

double [][]matrixPoint= new double[][]{{1,80,6400,512000,4.096*Math.pow(10, 7)},{1,100,10000,1000000,1.0*Math.pow(10, 8)},{1,120,14400,1728000,2.073*Math.pow(10, 8)},{1,160,25600,4096000,6.553*Math.pow(10, 8)},{1,200,40000,8000000,1.6*Math.pow(10, 9)}};
        double [] matrixVector=new double[]{300,350,300,350,250};

RealMatrix coefficients =
                    new Array2DRowRealMatrix(matrixPoint,false);                           
         DecompositionSolver solver = new LUDecompositionImpl(coefficients).getSolver();    
            //  RealVector constants = new ArrayRealVector(new double[] { 1, -2, 1 }, false);
         RealVector constants = new ArrayRealVector(matrixVector, false);
         RealVector solution = solver.solve(constants); 
                System.out.println("The values are:"+Math.round(solution.getEntry(0))+":"+Math.round(solution.getEntry(1))+":"+Math.round(solution.getEntry(2))+":"+Math.round(solution.getEntry(3))+":"+Math.round(solution.getEntry(4)));

API 有什么限制吗?如果您知道任何其他用于求解线性方程的库,请告诉我。

提前致谢

拉克什

4

1 回答 1

0

What results do you get?

The vector of closest doubles to the components of the exact solution is

[-7680.952380952381,261.8703703703704,-3.102149470899471,1.5775462962962963e-2,-2.9100529100529102e-5]

rounding the components yields [-7681,262,-3,0,0]. Multiplying that with the original matrix yields

[-5921,-11481,-19441,-42561,-75281]

which is far off, but that's to be expected, since the rounding errors in the coefficients is far larger than the reciprocals of the corresponding column entries for all but the first coefficient.

Multiplying the matrix with the double approximations to the exact solution yields

[300.0000000000019,350.00000000000256,300.00000000000335,350.0000000000051,250.0000000000071]

which is pretty close.

Solving the system with double arithmetic instead of rational arithmetic will probably produce slightly different results, but should be close. The values I got using Gaussian elimination are

[-7680.952380952391,261.8703703703705,-3.1021494708994711.577546296296295e-2,-2.9100529100529095e-5]
[299.9999999999927,349.99999999999227,299.99999999998636,349.99999999996,249.9999999999054]
[-1.000444171950221e-11,1.1368683772161603e-13,0.0,-1.3877787807814457e-17,6.776263578034403e-21]

for the solution, matrix × solution and difference between solution and best approximation to the exact solution.

If the results you get are close to those, the library is working well enough. I suspect the nonsensical result you obtained is solely due to the rounding you did for printing. If not, please update with the double results you got from the library.

于 2012-08-01T16:17:50.500 回答