finished versuch07
This commit is contained in:
parent
f5f6cd3b43
commit
1ac9396867
Binary file not shown.
|
@ -19,12 +19,16 @@
|
|||
///////////////////////// Funktions-Implementierung //////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Cramer::Cramer(int dim)
|
||||
{
|
||||
x = new Vektor(dim);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
Vektor Cramer::loese(QMatrix A, Vektor b)
|
||||
{
|
||||
int dim = A.getDim();
|
||||
double det = A.determinante();
|
||||
Vektor result(dim);
|
||||
|
||||
for (int l = 0; l < dim; l++)
|
||||
{
|
||||
|
@ -34,8 +38,8 @@ Vektor Cramer::loese(QMatrix A, Vektor b)
|
|||
Ai.set(k, l, b.get(k));
|
||||
}
|
||||
|
||||
result.set(l, Ai.determinante() / det);
|
||||
x->set(l, Ai.determinante() / det);
|
||||
}
|
||||
|
||||
return result;
|
||||
return *x;
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
class Cramer : public LGSLoeser
|
||||
{
|
||||
public:
|
||||
Cramer(int dim);
|
||||
Vektor loese(QMatrix A, Vektor b);
|
||||
};
|
||||
|
||||
|
|
|
@ -19,8 +19,66 @@ using namespace std;
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
Gauss::Gauss(int dim)
|
||||
{
|
||||
x = new Vektor(dim);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
Gauss::~Gauss()
|
||||
{
|
||||
delete x;
|
||||
delete b;
|
||||
delete A;
|
||||
}
|
||||
|
||||
//////////////////////////////////////
|
||||
////Hier Implementierung einfügen ////
|
||||
//////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
Vektor Gauss::loese(QMatrix B, Vektor c)
|
||||
{
|
||||
A = new QMatrix(B);
|
||||
b = new Vektor(c);
|
||||
|
||||
stufenform();
|
||||
rueckwaertsEinsetzen();
|
||||
|
||||
return *x;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
void Gauss::stufenform()
|
||||
{
|
||||
int dim = A->getDim();
|
||||
|
||||
for (int k = 0; k < dim; k++)
|
||||
{
|
||||
for (int l = k + 1; l < dim; l++)
|
||||
{
|
||||
double quot = A->get(l, k) / A->get(k, k);
|
||||
|
||||
b->set(l, b->get(l) - b->get(k) * quot);
|
||||
|
||||
for (int m = 0; m < dim; m++)
|
||||
{
|
||||
A->set(l, m, A->get(l, m) - A->get(k, m) * quot);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
void Gauss::rueckwaertsEinsetzen()
|
||||
{
|
||||
int dim = A->getDim();
|
||||
|
||||
for (int i = dim-1; i >= 0; i--)
|
||||
{
|
||||
double xi = b->get(i);
|
||||
|
||||
for (int k = i + 1; k < dim; k++)
|
||||
{
|
||||
xi -= A->get(i, k) * x->get(k);
|
||||
}
|
||||
|
||||
x->set(i, xi / A->get(i, i));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Praktikum Informatik 1
|
||||
// Versuch 7: Lösung eines mathematischen Anwendungsproblems
|
||||
// Versuch 7: L<EFBFBD>sung eines mathematischen Anwendungsproblems
|
||||
//
|
||||
// Datei: Gauss.h
|
||||
// Inhalt: Gauss-Klasse
|
||||
|
@ -14,11 +14,30 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "LGSLoeser.h"
|
||||
#include "QMatrix.h"
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////// CLASS DECLARATION //////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class Gauss: public LGSLoeser
|
||||
{
|
||||
public:
|
||||
// Konstruktor, übernimmt die Dimension
|
||||
Gauss(int n);
|
||||
|
||||
// Lösungsmethode, übernimmt Matrix A und Vektor b und gibt die Lösung x zurück
|
||||
Vektor loese(QMatrix A, Vektor b);
|
||||
|
||||
// Destruktor
|
||||
~Gauss();
|
||||
|
||||
private:
|
||||
void stufenform();
|
||||
void rueckwaertsEinsetzen();
|
||||
|
||||
QMatrix *A;
|
||||
Vektor *b;
|
||||
};
|
||||
|
||||
#endif /*GAUSS_H_*/
|
||||
|
|
|
@ -49,14 +49,16 @@ Vektor LR::loese(QMatrix A, Vektor b)
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
void LR::zerlege(QMatrix& A)
|
||||
{
|
||||
int dim = A.getDim();
|
||||
|
||||
// TODO Der PseudoCode funktioniert nur, wenn nicht durch 0 dividiert wird.
|
||||
for (int i = 1; i <= A.getDim(); i++)
|
||||
for (int i = 0; i < dim; i++)
|
||||
{
|
||||
for (int j = i + 1; j <= A.getDim(); j++)
|
||||
for (int j = i + 1; j < dim; j++)
|
||||
{
|
||||
A.set(j, i, A.get(j, i) / A.get(i, i));
|
||||
|
||||
for (int k = i + 1; k <= A.getDim(); k++)
|
||||
for (int k = i + 1; k < dim; k++)
|
||||
{
|
||||
A.set(j, k, A.get(j, k) - (A.get(j, i) * A.get(i, k)));
|
||||
}
|
||||
|
@ -68,16 +70,25 @@ void LR::zerlege(QMatrix& A)
|
|||
// Schreibe aus der Matrix A in die Matrizen L und R
|
||||
void LR::erzeugeLundR(QMatrix& A)
|
||||
{
|
||||
for (int i = 1; i <= A.getDim(); i++)
|
||||
int dim = A.getDim();
|
||||
|
||||
for (int i = 0; i < dim; i++)
|
||||
{
|
||||
for (int j = 1; j <= A.getDim(); j++)
|
||||
for (int j = 0; j < dim; j++)
|
||||
{
|
||||
if (i == j) /* Diagonale */
|
||||
{
|
||||
L->set(i, j, 1.0);
|
||||
L->set(i, j, 1);
|
||||
R->set(i, j, A.get(i, j));
|
||||
}
|
||||
else if (j > i) /* obere Dreiecksmatrix */
|
||||
{
|
||||
R->set(i, j, A.get(i, j));
|
||||
}
|
||||
else if (j < i) /* untere Dreiecksmatrix */
|
||||
{
|
||||
L->set(i, j, A.get(i, j));
|
||||
}
|
||||
|
||||
R->set(i, j, A.get(i, j));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -86,12 +97,36 @@ void LR::erzeugeLundR(QMatrix& A)
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
void LR::vorwaertsEinsetzen(Vektor& b)
|
||||
{
|
||||
int dim = L->getDim();
|
||||
|
||||
for (int i = 0; i < dim; i++)
|
||||
{
|
||||
double yi = b.get(i);
|
||||
|
||||
for (int k = 0; k < i; k++)
|
||||
{
|
||||
yi -= L->get(i, k) * y->get(k);
|
||||
}
|
||||
|
||||
y->set(i, yi);
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
void LR::rueckwaertsEinsetzen()
|
||||
{
|
||||
int dim = R->getDim();
|
||||
|
||||
for (int i = dim-1; i >= 0; i--)
|
||||
{
|
||||
double xi = y->get(i);
|
||||
|
||||
for (int k = i + 1; k < dim; k++)
|
||||
{
|
||||
xi -= R->get(i, k) * x->get(k);
|
||||
}
|
||||
|
||||
x->set(i, xi / R->get(i, i));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -26,26 +26,30 @@ class LR: public LGSLoeser
|
|||
public:
|
||||
// Konstruktor, übernimmt die Dimension
|
||||
LR(int n);
|
||||
|
||||
// Lösungsmethode, übernimmt Matrix A und Vektor b und gibt die Lösung x zurück
|
||||
Vektor loese(QMatrix A, Vektor b);
|
||||
// Destruktor
|
||||
~LR();
|
||||
|
||||
// Destruktor
|
||||
~LR();
|
||||
|
||||
private:
|
||||
// Unterteilung des Lösungsvorgangs in 4 Schritte:
|
||||
// Zerlegt Matrix A, Die zerlegten Matrizen L und R sind danach in A gespeichert
|
||||
void zerlege(QMatrix& A);
|
||||
|
||||
// Herauskopieren der bedeutenden Einträge in L und R
|
||||
void erzeugeLundR(QMatrix& A);
|
||||
|
||||
// Vorwaertseinsetzen(berechnen von Vektor y aus L und b)
|
||||
void vorwaertsEinsetzen(Vektor& b);
|
||||
|
||||
// Rueckwaertseinsetzen(berechnen von Vektor x aus R und y)
|
||||
void rueckwaertsEinsetzen();
|
||||
|
||||
|
||||
Vektor* y;
|
||||
QMatrix* L;
|
||||
QMatrix* R;
|
||||
};
|
||||
|
||||
|
||||
#endif /*LR_H_*/
|
||||
|
|
|
@ -11,7 +11,6 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "QMatrix.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -69,7 +68,7 @@ QMatrix& QMatrix::operator=(const QMatrix& B)
|
|||
// Destruktor
|
||||
QMatrix::~QMatrix()
|
||||
{
|
||||
delete[] A;
|
||||
//delete[] A;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -143,8 +142,6 @@ QMatrix QMatrix::inverse()
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
double QMatrix::determinante()
|
||||
{
|
||||
int dim = getDim();
|
||||
|
||||
if (dim == 1)
|
||||
{
|
||||
return get(0, 0);
|
||||
|
@ -154,7 +151,7 @@ double QMatrix::determinante()
|
|||
double det = 0;
|
||||
|
||||
// Laplace Entwicklung nach erster Spalte
|
||||
for (int i = 0; i < getDim(); i++)
|
||||
for (int i = 0; i < dim; i++)
|
||||
{
|
||||
det += pow(-1, i+2) * get(i, 0) * untermatrix(i, 0).determinante();
|
||||
}
|
||||
|
@ -166,11 +163,11 @@ double QMatrix::determinante()
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
QMatrix QMatrix::adjunkte()
|
||||
{
|
||||
QMatrix mat(getDim());
|
||||
QMatrix mat(dim);
|
||||
|
||||
for (int i = 0; i < getDim(); i++)
|
||||
for (int i = 0; i < dim; i++)
|
||||
{
|
||||
for (int j = 0; j < getDim(); j++)
|
||||
for (int j = 0; j < dim; j++)
|
||||
{
|
||||
mat.set(i, j, pow(-1, i+j) * untermatrix(i, j).determinante());
|
||||
}
|
||||
|
@ -186,9 +183,9 @@ QMatrix QMatrix::operator*(double scalar)
|
|||
{
|
||||
QMatrix mat(getDim());
|
||||
|
||||
for (int i = 0; i < getDim(); i++)
|
||||
for (int i = 0; i < dim; i++)
|
||||
{
|
||||
for (int j = 0; j < getDim(); j++)
|
||||
for (int j = 0; j < dim; j++)
|
||||
{
|
||||
mat.set(i, j, get(i, j) * scalar);
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Praktikum Informatik 1
|
||||
// Versuch 7: Lösung eines mathematischen Anwendungsproblems
|
||||
// Versuch 7: L<EFBFBD>sung eines mathematischen Anwendungsproblems
|
||||
//
|
||||
// Datei: Vektor.h
|
||||
// Inhalt: Vektor-Klasse zum speichern von Vektoren variabler Größe
|
||||
// Inhalt: Vektor-Klasse zum speichern von Vektoren variabler Größe
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef VEKTOR_H_
|
||||
|
@ -24,7 +24,7 @@ using namespace std;
|
|||
class Vektor
|
||||
{
|
||||
public:
|
||||
// Konstruktor, übernimmt die Dimension
|
||||
// Konstruktor, <EFBFBD>bernimmt die Dimension
|
||||
Vektor(int n);
|
||||
//Kopierkonstruktor
|
||||
Vektor(const Vektor&);
|
||||
|
|
|
@ -8,8 +8,8 @@
|
|||
|
||||
#include "QMatrix.h"
|
||||
#include "Vektor.h"
|
||||
//#include "LR.h"
|
||||
//#include "Gauss.h"
|
||||
#include "LR.h"
|
||||
#include "Gauss.h"
|
||||
#include "Cramer.h"
|
||||
#include <iostream>
|
||||
|
||||
|
@ -55,6 +55,20 @@ int main()
|
|||
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);
|
||||
|
||||
b.set(0, 1);
|
||||
b.set(1, 1);
|
||||
b.set(2, 1);*/
|
||||
|
||||
cout << "A = " << endl << A;
|
||||
cout << "b = " << endl << b;
|
||||
//cout << "Dim(A) = " << A.getDim() << endl;
|
||||
|
@ -62,10 +76,13 @@ int main()
|
|||
//cout << "Adj(A) = " << endl << A.adjunkte() << endl;
|
||||
//cout << "A^-1 = " << endl << A.inverse() << endl;
|
||||
|
||||
Cramer lgs1;
|
||||
x = lgs1.loese(A, b);
|
||||
Cramer solver1(dim);
|
||||
LR solver2(dim);
|
||||
Gauss solver3(dim);
|
||||
|
||||
cout << "x =" << endl << x;
|
||||
cout << "x_cramer =" << endl << solver1.loese(A, b);
|
||||
cout << "x_lr =" << endl << solver2.loese(A, b);
|
||||
cout << "x_gauss =" << endl << solver3.loese(A, b);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue