How exactly do I format my code so that It would take
1x+2y+3z+4w=5e
6x+7y+8z+9w=10e
11x+12y+13z+14w=15e
16x+17y+18z+19w=20e
and return x,y,z,w
?
I was trying to follow the format described in this answer,
Currently what I have is the following code which which throws IllegalArgumentException: "java.lang.IllegalArgumentException: Can't solve for wide systems. More variables than equations.
when it attempts to calculate the 4 results:
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();
}
What am I doing wrong?
I think it may have to do with your matrix dimensions.
new SimpleMatrix A(4,5)
should be replaced with new SimpleMatrix A(4,4)
.