-1

I have this code which give solution to 4x4 linear equations. how can i print out when a linear equation have no solution or have multiples ones. instead of prints error ?

public class OvaWork 
{


    void fourthEquationSolver()
    {
        //Creating  Arrays Representing Equations
        double[][] lhsArray = {{8,1,10,1}, {2,1,5,4}, {1,5,3,2}, {9,8,4,6}};
        double[] rhsArray = {14,22,38,44};
        //Creating Matrix Objects with arrays
        Matrix lhs = new Matrix(lhsArray);
        Matrix rhs = new Matrix(rhsArray, 4);
        //Calculate Solved Matrix
        Matrix ans = lhs.solve(rhs);
        //Printing Answers
        System.out.println("x1 = " + (ans.get(0, 0)));
        System.out.println("x2 = " + (ans.get(1, 0)));
        System.out.println("X3 = " + (ans.get(2, 0)));
        System.out.println("X4 = " + (ans.get(3, 0)));
    }



    public static void main(String[] args) 
    {
        OvaWork equation = new OvaWork();

    }
}

When i write into this code a matrix like this:

1,1,1,1=14
2,2,2,2=22
3,3,3,3=38
4,4,4,4=44

this code prints :

Exception in thread "main" java.lang.RuntimeException: Matrix is singular.
    at Jama.LUDecomposition.solve(LUDecomposition.java:282)
    at Jama.Matrix.solve(Matrix.java:815)
    at OvaWork.fourthEquationSolver(OvaWork.java:20)
    at OvaWork.main(OvaWork.java:106)

because the above matrix have or multiple solutions, or no have solution

Volazh
  • 128
  • 2
  • 11
  • So, change your code to do that. BTW: Did you notice that you clicked a button called "**Ask Question**" when you wrote your text here? I don't see any question in your text. – Andreas Apr 28 '16 at 14:36
  • 1
    @Andreas I'm asking question about how to print out when a linear equation implemented in my code have no soution or have multiple ones. cuz till now, my code just print out when have just one solution. – Volazh Apr 28 '16 at 14:41
  • 2
    Please could you describe the error you are getting, as it is difficult to help answer your question otherwise. – jr593 Apr 28 '16 at 14:44

1 Answers1

0

You can ask for the determinant https://en.wikipedia.org/wiki/Determinant

"A system of linear equations has a unique non-trivial solution if and only if its determinant is non-zero. If this determinant is zero, then the system has either no nontrivial solutions or an infinite number of solutions."

http://math.oregonstate.edu/home/programs/undergrad/CalculusQuestStudyGuides/vcalc/system/system.html

if (lhs.det() == 0) {
    System.out.println("No solution or infinite number of solutions");
} else {
    Matrix ans = lhs.solve(rhs);
    //Printing Answers
    System.out.println("x1 = " + (ans.get(0, 0)));
    System.out.println("x2 = " + (ans.get(1, 0)));
    System.out.println("X3 = " + (ans.get(2, 0)));
    System.out.println("X4 = " + (ans.get(3, 0)));
}
Troncador
  • 2,856
  • 2
  • 18
  • 39
  • 1
    very usefull your answer, but can i get a separate ones?. I mean is diferent when i get a no solution than when i get a multiples solutions. (when i get a 0,0,0,x = x) mean there're multiples solution, than when i get a (0,0,0,0 = 0) that's mean that the matix doesn't have solution at all – Volazh Apr 28 '16 at 15:05
  • You can reduce the matrix to the "row echelon" form, if somes row have only zeros, then there is infinite solutions – Troncador Apr 28 '16 at 15:13
  • how "row echelon" form looks like implemented in my code? – Volazh Apr 28 '16 at 15:42
  • humn... I don't know, maybe you have to program it, or find a library. Or maybe there is another matrix property you can use to differentiate it that is beyond my math knowledge :p – Troncador Apr 29 '16 at 02:38