Und da ist halt ganz wichtig, dass Du den Lösungsweg selbst durchspielst. Nur damit kannst Du dann beobachten, was Du genau hast. Mit welchen Daten arbeitest Du? Wann machst Du was?Möchte ein Verfahren, dass es für beliebige Gleichungen löst haben.
Genau das hast Du doch in #7 bekommen.Das ist lieb, dass du helfen möchtest, aber ich brauche einen Algorithmus in Pseudocode.
import java.util.Arrays;
public class GleichungssystemLoeser {
public static double[] loeseGleichungssystem(double[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length - 1; // Letzte Spalte sind Ergebnisse
for (int i = 0; i < rows; i++) {
int pivot = i;
// Suche nach einer nicht-null-Zahl in der Spalte
while (pivot < cols && matrix[i][pivot] == 0) {
pivot++;
}
if (pivot == cols) {
// Null-Spalte, keine eindeutige Lösung
return null;
}
// Tausche Zeilen, um die Pivot-Zeile nach oben zu bringen
double[] temp = matrix[i];
matrix[i] = matrix[pivot];
matrix[pivot] = temp;
// Normalisiere die Pivot-Zeile
double divisor = matrix[i][i];
for (int j = i; j <= cols; j++) {
matrix[i][j] /= divisor;
}
// Eliminiere die Pivot-Zeile aus den anderen Zeilen
for (int j = 0; j < rows; j++) {
if (j != i && matrix[j][i] != 0) {
double factor = matrix[j][i];
for (int k = i; k <= cols; k++) {
matrix[j][k] -= factor * matrix[i][k];
}
}
}
}
// Extrahiere die Lösungen
double[] solution = new double[cols];
for (int i = 0; i < rows; i++) {
if (matrix[i][i] == 1) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] != 0) {
solution[j] = matrix[i][cols];
break;
}
}
}
}
return solution;
}
public static void main(String[] args) {
double[][] matrix = {
{2, -1, 1, 2},
{1, 1, -1, 0},
{3, -2, 2, 3}
};
double[] solution = loeseGleichungssystem(matrix);
if (solution != null) {
System.out.println("Lösung:");
for (int i = 0; i < solution.length; i++) {
System.out.println("x" + i + " = " + solution[i]);
}
} else {
System.out.println("Das Gleichungssystem hat keine eindeutige Lösung.");
}
}
}
loeseGleichungssystem
nimmt eine Matrix als Eingabe, wobei die letzte Spalte die Ergebnisse der Gleichungen enthält. Falls eine eindeutige Lösung existiert, wird ein Array mit den Lösungen zurückgegeben, andernfalls null
.main
-Teil demonstriert die Verwendung dieser Methode mit einer Beispielmatrix und gibt die Lösungen aus, wenn vorhanden. Beachte, dass dies ein grundlegendes Beispiel ist und spezielle Fälle oder Fehlerbehandlungen möglicherweise nicht abgedeckt sind.hilft weder Dir noch erhöht es die Lust, Dir einfach #7 noch ausführlicher zu schreiben.Wenn ich dir einen . auf eine Leinwand malen würde, wäre das dann auch Kunst? Wären die Brötchen dadurch bezahlt?
Wieso liest Du denn nicht einfach nach, was das gaußsche Eleminationsverfahren ist? Gaußsches Eliminationsverfahren – WikipediaNaja, ist aber auch Blödsinn, weil ich keine Koeffizientenmatrix habe.
package org.java_forum.gleichungen_loesen_203373;
/**
*
*/
public interface Term
{
Term eval();
}
package org.java_forum.gleichungen_loesen_203373;
/**
* {@link Value} or {@link Variable}.
*/
public interface Operand extends Term {
}
package org.java_forum.gleichungen_loesen_203373;
/**
* Value
*/
public class Value implements Operand {
public final int value;
/**
* Constructor.
*
* @param value
*/
public Value(final int value) {
this.value = value;
}
@Override
public Term eval() {
return this;
}
@Override
public String toString() {
return String.valueOf( this.value );
}
}
package org.java_forum.gleichungen_loesen_203373;
/**
* This is a term in which variables can be replaced by values.
*
* {@link Add} or {@link Subtract} or {@link Variable}.
*/
public interface ReplacableTerm extends Operand {
Term replace( final Variable variable, final Value value );
}
package org.java_forum.gleichungen_loesen_203373;
/**
* Variable.
*/
public class Variable implements ReplacableTerm {
public final String name;
/**
* Constructor.
*
* @param name
*/
public Variable(String name) {
this.name = name;
}
@Override
public Term eval() {
return this;
}
@Override
public Term replace(final Variable variable, final Value value) {
if ( this.equals( variable ) ) {
return value;
}
return this;
}
/**
* Generated by Eclipse
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
/**
* Generated by Eclipse
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Variable other = (Variable) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return name;
}
}
package org.java_forum.gleichungen_loesen_203373;
/**
* Add
*/
public class Add implements ReplacableTerm {
public final Term lhs;
public final Term rhs;
/**
* Constructor.
*
* @param lhs
* @param rhs
*/
public Add(final Term lhs, final Term rhs) {
this.lhs = lhs;
this.rhs = rhs;
}
@Override
public Term eval() {
if (lhs.eval() instanceof Value lhsValue && rhs.eval() instanceof Value rhsValue) {
return new Value( lhsValue.value + rhsValue.value );
}
return this;
}
@Override
public Term replace(final Variable variable, final Value value) {
final Term lhsReplaced;
if (lhs instanceof ReplacableTerm lhsReplacableTerm) {
lhsReplaced = lhsReplacableTerm.replace(variable, value);
}
else {
lhsReplaced = this.lhs;
}
final Term rhsReplaced;
if (rhs instanceof ReplacableTerm rhsReplacableTerm) {
rhsReplaced = rhsReplacableTerm.replace(variable, value);
}
else {
rhsReplaced = this.rhs;
}
return new Add(lhsReplaced, rhsReplaced).eval();
}
@Override
public String toString() {
return lhs + "+" + rhs;
}
}
package org.java_forum.gleichungen_loesen_203373;
/**
* Subtract
*/
public class Subtract implements ReplacableTerm {
public final Term lhs;
public final Term rhs;
/**
* Constructor.
*
* @param lhs
* @param rhs
*/
public Subtract(final Term lhs, final Term rhs) {
this.lhs = lhs;
this.rhs = rhs;
}
@Override
public Term eval() {
if ( lhs.eval() instanceof Value lhsValue && rhs.eval() instanceof Value rhsValue ) {
return new Value( lhsValue.value - rhsValue.value );
}
return this;
}
@Override
public Term replace(final Variable variable, final Value value) {
final Term lhsReplaced;
if (lhs instanceof ReplacableTerm lhsReplacableTerm) {
lhsReplaced = lhsReplacableTerm.replace(variable, value);
}
else {
lhsReplaced = this.lhs;
}
final Term rhsReplaced;
if (rhs instanceof ReplacableTerm rhsReplacableTerm) {
rhsReplaced = rhsReplacableTerm.replace(variable, value);
}
else {
rhsReplaced = this.rhs;
}
return new Subtract(lhsReplaced, rhsReplaced).eval();
}
@Override
public String toString() {
return lhs + "-" + rhs;
}
}
(offtopic) Es gibt viele Wege ein LGS zu bearbeiten. Je nach Struktur der Matrix und Anforderung an die Genauigkeit der Lösung können exakte (z.B. Additions-/Subtraktionsverfahren) oder numerische (die realen Werte werden (zwangsweise) nur angenähert) Verfahren verwendet werden.Professorenkram
Das Themengebiet wird oft einfach als symbolische Algebra oder Symbolische Mathematik bezeichnet. Das ist ein tolles Themengebiet aber setzt zugleich sehr gute Mathematik-Kenntnisse voraus. Aber es sollte klar sein, dass wir mit diesen einfachen Gleichungssystemen nur an der Oberfläche sind und da nicht wirklich tief einsteigen.(offtopic) Es gibt viele Wege ein LGS zu bearbeiten. Je nach Struktur der Matrix und Anforderung an die Genauigkeit der Lösung können exakte (z.B. Additions-/Subtraktionsverfahren) oder numerische (die realen Werte werden (zwangsweise) nur angenähert) Verfahren verwendet werden.