2016-04-27 37 views
0

Ich versuche, ein 4 x 4 lineares Gleichungssystem (4 Variablen, 4 Gleichungen) mit Jama zu lösen. Ich habe den folgenden Code ausprobiert, aber es funktioniert nicht. Ich würde mich freuen, wenn jemand mir helfen könnte, mit Jama oder einer anderen Methode.Jama Paket, 4x4 lineare Gleichung Löser

import Jama.Matrix; 

public class OvaWork { 

    public OvaWork() 
    { 

     //Creating Arrays Representing Equations 
     double[][] lhsArray = {{-3, 1, -1}, {5, -2, 1}, {-1, 1, 3}, {2, 5, 7}}; 
     double[] rhsArray = {-4, 6, 0, 8}; 
     //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("w = " + Math.round(ans.get(0, 0))); 
     System.out.println("x = " + Math.round(ans.get(1, 0))); 
     System.out.println("y = " + Math.round(ans.get(2, 0))); 
     System.out.println("z = " + Math.round(ans.get(3, 0))); 
    } 

    public static void main(String[] args) 
    { 
     OvaWork o = new OvaWork(); 
    } 
} 
+1

Wenn Sie "aber immer noch keine Arbeit" sagen, bedeutet das, dass Sie einen Fehler bekommen oder was bedeutet das? – Austin

+0

'LhsArray' ist kein 4x4 Array – FredK

+0

@FREDK wie 1hsArray in ein 4x4 Array konvertieren? – Volazh

Antwort

2

Sie haben mit einfachen Beispielen wie diese 2x2 Gleichung versuchen

double[][] lhsArray = {{1,1},{2, 0}}; 
double[] rhsArray = {10,2}; 
Matrix lhs = new Matrix(lhsArray); 
Matrix rhs = new Matrix(rhsArray, 2); 
Matrix ans = lhs.solve(rhs); 

Es funktioniert und der Ausgang ist eine Matrix {1,9}

Das Problem mit Ihrem Code ist, dass Ihr Matrix ist nicht quadratisch, es ist 3x4

double[][] lhsArray = {{-3, 1, -1}, {5, -2, 1}, {-1, 1, 3}, {2, 5, 7}}; 

Ändern Sie Ihre Matrix zu einem Quadrat.

-Test dieser triviale Gleichung:

double[][] lhsArray = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}; 
double[] rhsArray = {1, 2, 3, 4}; 
Matrix lhs = new Matrix(lhsArray); 
Matrix rhs = new Matrix(rhsArray, 4); 
Matrix ans = lhs.solve(rhs); 

Die am ist {1,2,3,4}, wie erwartet.