gpt4 book ai didi

java - 使用 EJML 计算线性系统

转载 作者:行者123 更新时间:2023-12-01 10:34:16 24 4
gpt4 key购买 nike

我到底如何格式化我的代码,以便它需要

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

并返回 x,y,z,w

我试图遵循 this answer 中描述的格式,目前我所拥有的是以下代码,它会抛出 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();
}

我做错了什么?

最佳答案

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

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

关于java - 使用 EJML 计算线性系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34879255/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com