#1 8. Juli 2009 Gauß Verfahren PHP: package GaussVerfahren ; import java . lang . String ; import java . util . Scanner ; public class Gauss { //x1,x2 und x3 werden angelegt. Die erste Zahl die Anzahl der Gleichungen, die zweite die Anzahl der Variablen private static double [][] a = new double [ 3 ][ 4 ]; // Gleichungssystem wird in der Console ausgegeben private static void printGlSystem () { for ( int i = 0 ; i < a . length ; i ++) { System . out . println ( a [ i ][ 0 ] + "*X1 + " + a [ i ][ 1 ] + "*X2 + " + a [ i ][ 2 ] + "*X3 = " + a [ i ][ 3 ]); } System . out . println (); } // vertausche Gleichung g mit Gleichung h // Diese Methode wird durch vertauscheGl(0, 1) aufgerufen, die 2 Zahlen in... // ...der Klammer stehen für die jeweilige Gleichung private static void vertauscheGl ( int g , int h ) { for ( int i = 0 ; i < 4 ; i ++) { double t = a [ g ][ i ]; a [ g ][ i ] = a [ h ][ i ]; a [ h ][ i ] = t ; } } public static void main ( String [] args ) { Scanner sc = new Scanner ( System . in ); System . out . println ( "Bitte Werte in Form von x,x eingeben!" ); //Vorbereitung und Deklaration für die Eingabe der 3 Gleichungen int gleichung = 1 ; int tmp = 1 ; gleichung = 0 ; // initialisiere das Gleichungssystem // manuelle Eingabe aller 3 Gleichungen for ( int i = 0 ; i < a . length ; i ++) { gleichung = gleichung + 1 ; for ( int n = 0 ; n < a [ i ]. length ; n ++) { tmp = n + 1 ; if ( tmp <= 3 ) { System . out . println ( "Bitte einen Wert für x" + tmp + " der " + gleichung + ". Gleichung eingeben:" ); } else { System . out . println ( "Bitte geben sie die rechte Seite der " + gleichung + ". Gleichung ein:" ); } a [ i ][ n ] = sc . nextDouble (); } tmp = 1 ; } printGlSystem (); // falls a[0][0]=0, vertausche gegebenfalls Gleichung 1 mit Gleichung 2 if ( a [ 0 ][ 0 ] == 0.0 ) { vertauscheGl ( 0 , 1 ); printGlSystem (); } //Falls x1 der ersten Gleichung 0 ist, dann vertausche erste Gleichung mit der dritten if ( a [ 0 ][ 0 ] == 0.0 ) { vertauscheGl ( 0 , 2 ); printGlSystem (); } // multipliziere die 1.Gleichung mit a10/a00 und ziehe sie von der 2.Gleichung ab und // speichere das Ergebnis als neue 2.Gleichung ab if ( a [ 1 ][ 0 ] != 0.0 ) { double d = a [ 1 ][ 0 ] / a [ 0 ][ 0 ]; for ( int i = 0 ; i < 4 ; i ++) a [ 1 ][ i ] = a [ 1 ][ i ] - ( d * a [ 0 ][ i ]); printGlSystem (); } // multipliziere die 1.Gleichung mit a[2][0]/a[0][0] und ziehe es von der 3.Gleichung ab und // speichere das Ergebnis als neue 3.Gleichung ab if ( a [ 2 ][ 0 ] != 0.0 ) { double d = a [ 2 ][ 0 ] / a [ 0 ][ 0 ]; for ( int i = 0 ; i < 4 ; i ++) a [ 2 ][ i ] = a [ 2 ][ i ] - ( d * a [ 0 ][ i ]); printGlSystem (); } // falls a[1][1]=0, vertausche gegebenfalls Gleichung 2 mit Gleichung 3 if ( a [ 1 ][ 1 ] == 0.0 ) { vertauscheGl ( 1 , 2 ); printGlSystem (); } // multipliziere die 2.Gleichung mit a[2][1]/a[1][1] und ziehe es von der 3.Gleichung ab und // speichere das Ergebnis als neue 3.Gleichung if ( a [ 2 ][ 1 ] != 0.0 ) { double d = a [ 2 ][ 1 ] / a [ 1 ][ 1 ]; for ( int i = 0 ; i < 4 ; i ++) a [ 2 ][ i ] = a [ 2 ][ i ] - ( d * a [ 1 ][ i ]); printGlSystem (); } double x3 ; // berechne nun X3 if ( a [ 2 ][ 3 ] == 0.0 ) x3 = 0.0 ; else x3 = a [ 2 ][ 3 ] / a [ 2 ][ 2 ]; //berechne nun x2 double x2 ; if ( a [ 1 ][ 1 ] == 0.0 ) x2 = 0.0 ; else x2 = ( a [ 1 ][ 3 ] - a [ 1 ][ 2 ] * x3 ) / a [ 1 ][ 1 ]; //berechne x1 double x1 ; if ( a [ 0 ][ 0 ] == 0.0 ) x1 = 0.0 ; else x1 = ( a [ 0 ][ 3 ] - ( a [ 0 ][ 1 ] * x2 + a [ 0 ][ 2 ] * x3 )) / a [ 0 ][ 0 ]; //Trennlinie System . out . println ( "---------------------------------" ); //Ausgabe aller Lösungen System . out . println ( "Lösung:" ); System . out . println ( "-------" ); System . out . println ( "x1 = " + x1 ); System . out . println ( "x2 = " + x2 ); System . out . println ( "x3 = " + x3 ); } } Beispiel: 1. Gleichung 2x1 + 3x2 - 4x3 = 16 2. Gleichung 4x1 + 9x2 - 1x3 = 58 3.Gleichung 1x1 + 6x2 + 2x2 = 34 Lösung: x1 = 6 x2 = 4 x3 = 2 // Habe es inzwischen doch selbst hinbekommen, Code da oben wurde aktualisiert falls es mal jemand braucht + Multi-Zitat Zitieren