Praktikum fertig :)
This commit is contained in:
parent
1ac9396867
commit
4b6b2f5c46
6 changed files with 121 additions and 53 deletions
Binary file not shown.
|
@ -49,6 +49,24 @@ void Gauss::stufenform()
|
|||
{
|
||||
int dim = A->getDim();
|
||||
|
||||
/* Vertausche Zeilen um 0en in der Hauptdiagonale zu vermeiden */
|
||||
for (int i = 0; i < dim; i++)
|
||||
{
|
||||
if (A->get(i, i) == 0) /* 0 gefunden! */
|
||||
{
|
||||
for (int j = 0; j < dim; j++)
|
||||
{
|
||||
if (i != j && A->get(j, i) != 0)
|
||||
{
|
||||
A->vertauscheZeile(i, j);
|
||||
cout << "Zeile " << i + 1 << " und " << j + 1
|
||||
<< " vertauscht!" << endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = 0; k < dim; k++)
|
||||
{
|
||||
for (int l = k + 1; l < dim; l++)
|
||||
|
@ -70,7 +88,7 @@ void Gauss::rueckwaertsEinsetzen()
|
|||
{
|
||||
int dim = A->getDim();
|
||||
|
||||
for (int i = dim-1; i >= 0; i--)
|
||||
for (int i = dim - 1; i >= 0; i--)
|
||||
{
|
||||
double xi = b->get(i);
|
||||
|
||||
|
|
|
@ -51,7 +51,22 @@ void LR::zerlege(QMatrix& A)
|
|||
{
|
||||
int dim = A.getDim();
|
||||
|
||||
// TODO Der PseudoCode funktioniert nur, wenn nicht durch 0 dividiert wird.
|
||||
/* Vertausche Zeilen um 0en in der Hauptdiagonale zu vermeiden */
|
||||
for (int i = 0; i < dim; i++)
|
||||
{
|
||||
if (A.get(i, i) == 0) /* 0 gefunden! */
|
||||
{
|
||||
for (int j = 0; j < dim; j++)
|
||||
{
|
||||
if (i != j && A.get(j, i) != 0) {
|
||||
A.vertauscheZeile(i, j);
|
||||
cout << "Zeile " << i+1 << " und " << j+1 << " vertauscht!" << endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < dim; i++)
|
||||
{
|
||||
for (int j = i + 1; j < dim; j++)
|
||||
|
@ -91,7 +106,6 @@ void LR::erzeugeLundR(QMatrix& A)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -94,18 +94,32 @@ int QMatrix::getDim() const
|
|||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Transponiere die Matrix
|
||||
void QMatrix::transponiere()
|
||||
QMatrix QMatrix::transponiere()
|
||||
{
|
||||
double temp;
|
||||
QMatrix mat(dim);
|
||||
|
||||
for (int i = 0; i < dim; i++)
|
||||
{
|
||||
for (int j = i + 1; j < dim; j++)
|
||||
for (int j = 0; j < dim; j++)
|
||||
{
|
||||
temp = A[i * dim + j];
|
||||
A[i * dim + j] = A[j * dim + i];
|
||||
A[j * dim + i] = temp;
|
||||
mat.set(i, j, A[j * dim + i]);
|
||||
}
|
||||
}
|
||||
|
||||
return mat;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
QMatrix QMatrix::vertauscheZeile(int i, int j)
|
||||
{
|
||||
double temp;
|
||||
|
||||
for (int k = 0; k < dim; k++)
|
||||
{
|
||||
temp = A[i * dim + k];
|
||||
A[i * dim + k] = A[j * dim + k];
|
||||
A[j * dim + k] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -173,9 +187,7 @@ QMatrix QMatrix::adjunkte()
|
|||
}
|
||||
}
|
||||
|
||||
mat.transponiere();
|
||||
|
||||
return mat;
|
||||
return mat.transponiere();
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -43,7 +43,8 @@ public:
|
|||
int getDim() const;
|
||||
|
||||
// Hilfsmethoden für die Cramersche Regel
|
||||
void transponiere();
|
||||
QMatrix transponiere();
|
||||
QMatrix vertauscheZeile(int i, int j);
|
||||
QMatrix untermatrix(int i, int j);
|
||||
QMatrix inverse();
|
||||
QMatrix adjunkte();
|
||||
|
|
|
@ -6,12 +6,14 @@
|
|||
// Inhalt: Hauptprogramm
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "QMatrix.h"
|
||||
#include "Vektor.h"
|
||||
#include "LR.h"
|
||||
#include "Gauss.h"
|
||||
#include "Cramer.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
@ -19,8 +21,6 @@ using namespace std;
|
|||
void Eingabe(QMatrix& A, int n);
|
||||
void Eingabe(Vektor& b, int n);
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
Die Benennung der Variablen entspricht, in diesem Versuch,
|
||||
den mathematischen Bezeichnungen im Script.
|
||||
|
@ -29,56 +29,79 @@ void Eingabe(Vektor& b, int n);
|
|||
*/
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
int main()
|
||||
{
|
||||
int dim = 3;
|
||||
//cout << "Wieviele Gleichungen/Unbekannte hat ihr LGS?: "; cin >> dim;
|
||||
QMatrix A(dim);
|
||||
Vektor b(dim);
|
||||
Vektor x(dim);
|
||||
//Eingabe(A, dim);
|
||||
//Eingabe(b, dim);
|
||||
|
||||
/* Beispiel Daten. Ergebnis (4, -2, -1) */
|
||||
A.set(0, 0, 1);
|
||||
A.set(0, 1, 2);
|
||||
A.set(0, 2, 2);
|
||||
A.set(1, 0, 2);
|
||||
A.set(1, 1, 3);
|
||||
A.set(1, 2, 1);
|
||||
A.set(2, 0, 3);
|
||||
A.set(2, 1, 4);
|
||||
A.set(2, 2, 1);
|
||||
char eingabe;
|
||||
cout << "Wollen Sie selbst ein LGS angeben (j/n)? ";
|
||||
cin >> eingabe;
|
||||
if (eingabe == 'j')
|
||||
{
|
||||
cout << "Wieviele Gleichungen/Unbekannte hat ihr LGS?: ";
|
||||
cin >> dim;
|
||||
Eingabe(A, dim);
|
||||
Eingabe(b, dim);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Beispiel Daten. Ergebnis (4, -2, -1) */
|
||||
A.set(0, 0, 1);
|
||||
A.set(0, 1, 2);
|
||||
A.set(0, 2, 2);
|
||||
A.set(1, 0, 2);
|
||||
A.set(1, 1, 3);
|
||||
A.set(1, 2, 1);
|
||||
A.set(2, 0, 3);
|
||||
A.set(2, 1, 4);
|
||||
A.set(2, 2, 1);
|
||||
|
||||
b.set(0, -2);
|
||||
b.set(1, 1);
|
||||
b.set(2, 3);
|
||||
b.set(0, -2);
|
||||
b.set(1, 1);
|
||||
b.set(2, 3);
|
||||
|
||||
/*A.set(0, 0, 3);
|
||||
A.set(0, 1, 2);
|
||||
A.set(0, 2, 1);
|
||||
A.set(1, 0, 6);
|
||||
A.set(1, 1, 6);
|
||||
A.set(1, 2, 3);
|
||||
A.set(2, 0, 9);
|
||||
A.set(2, 1, 10);
|
||||
A.set(2, 2, 6);
|
||||
/*A.set(0, 0, 3);
|
||||
A.set(0, 1, 2);
|
||||
A.set(0, 2, 1);
|
||||
A.set(1, 0, 6);
|
||||
A.set(1, 1, 6);
|
||||
A.set(1, 2, 3);
|
||||
A.set(2, 0, 9);
|
||||
A.set(2, 1, 10);
|
||||
A.set(2, 2, 6);
|
||||
|
||||
b.set(0, 1);
|
||||
b.set(1, 1);
|
||||
b.set(2, 1);*/
|
||||
b.set(0, 1);
|
||||
b.set(1, 1);
|
||||
b.set(2, 1);*/
|
||||
}
|
||||
|
||||
cout << "A = " << endl << A;
|
||||
cout << endl << "A = " << endl << A;
|
||||
cout << "b = " << endl << b;
|
||||
//cout << "Dim(A) = " << A.getDim() << endl;
|
||||
//cout << "Det(A) = " << A.determinante() << endl;
|
||||
//cout << "Adj(A) = " << endl << A.adjunkte() << endl;
|
||||
//cout << "A^-1 = " << endl << A.inverse() << endl;
|
||||
|
||||
Cramer solver1(dim);
|
||||
LR solver2(dim);
|
||||
Gauss solver3(dim);
|
||||
if (A.determinante() == 0)
|
||||
{
|
||||
cerr << "Fehler: Das LGS besitzt keine eindeutige Lösung (det(A) = 0)!"
|
||||
<< endl;
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
cout << "Wollen Sie weitere Eigenschaften der Koeffizientenmatrix berechnen (j/n)? ";
|
||||
cin >> eingabe;
|
||||
if (eingabe == 'j')
|
||||
{
|
||||
cout << "Dim(A) = " << A.getDim() << endl;
|
||||
cout << "Det(A) = " << A.determinante() << endl;
|
||||
cout << "Adj(A) = " << endl << A.adjunkte() << endl;
|
||||
cout << "A^-1 = " << endl << A.inverse() << endl;
|
||||
cout << "A^T = " << endl << A.transponiere() << endl;
|
||||
}
|
||||
|
||||
Cramer solver1(dim);
|
||||
LR solver2(dim);
|
||||
Gauss solver3(dim);
|
||||
|
||||
cout << "x_cramer =" << endl << solver1.loese(A, b);
|
||||
cout << "x_lr =" << endl << solver2.loese(A, b);
|
||||
|
|
Loading…
Add table
Reference in a new issue