SolveFuction_gauss_jacobi_working
This commit is contained in:
47
main.c
47
main.c
@@ -51,7 +51,7 @@ int main(int argc, char* argv[]) {
|
|||||||
strncpy(filePath, argv[1], MAX_FILE_PATH_LENGTH);
|
strncpy(filePath, argv[1], MAX_FILE_PATH_LENGTH);
|
||||||
} else {
|
} else {
|
||||||
puts("Please enter the path of the file you'd like to open");
|
puts("Please enter the path of the file you'd like to open");
|
||||||
int result = scanf("%" STR(MAX_FILE_PATH_LENGTH) "[^\n]", filePath);
|
int result = scanf("%s" STR(MAX_FILE_PATH_LENGTH) "[^\n]", filePath);
|
||||||
if(result == EOF || result == 0 || filePath[0] == 0) {
|
if(result == EOF || result == 0 || filePath[0] == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -255,6 +255,8 @@ void flushStdin(void) {
|
|||||||
Vector* solve(Method method, Matrix* A, Vector* b, Vector* x, double e) {
|
Vector* solve(Method method, Matrix* A, Vector* b, Vector* x, double e) {
|
||||||
Vector* vectors = malloc(sizeof(Vector) * (MAX_ITERATION_STEPS + 1));
|
Vector* vectors = malloc(sizeof(Vector) * (MAX_ITERATION_STEPS + 1));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
initVector(&vectors[0], b->n);
|
initVector(&vectors[0], b->n);
|
||||||
memcpy(vectors[0].data, x->data, b->n * sizeof(double));
|
memcpy(vectors[0].data, x->data, b->n * sizeof(double));
|
||||||
|
|
||||||
@@ -262,8 +264,11 @@ Vector* solve(Method method, Matrix* A, Vector* b, Vector* x, double e) {
|
|||||||
double temp;
|
double temp;
|
||||||
double delta = 0.0;
|
double delta = 0.0;
|
||||||
|
|
||||||
|
if (method == 0){
|
||||||
|
|
||||||
do {
|
do {
|
||||||
delta= 0.0;
|
delta= 0.0;
|
||||||
|
initVector(&vectors[vectorCount],b->n);
|
||||||
for(int i=0; i<b->n; i++){
|
for(int i=0; i<b->n; i++){
|
||||||
temp=0.0;
|
temp=0.0;
|
||||||
for(int j=0; j<b->n; j++){
|
for(int j=0; j<b->n; j++){
|
||||||
@@ -271,15 +276,43 @@ Vector* solve(Method method, Matrix* A, Vector* b, Vector* x, double e) {
|
|||||||
temp= temp+A->data[i][j]*vectors[vectorCount-1].data[j];
|
temp= temp+A->data[i][j]*vectors[vectorCount-1].data[j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
vectors[vectorCount].data[i]=1/A->data[i][i]*(b->data[i]-temp);
|
vectors[vectorCount].data[i]=(b->data[i]-temp)/A->data[i][i];
|
||||||
delta=fmax(fabs(vectors[vectorCount].data[i]-vectors[vectorCount-1].data[i]),delta);
|
delta=fmax(fabs(vectors[vectorCount].data[i]-vectors[vectorCount-1].data[i]),delta);
|
||||||
}
|
}
|
||||||
// CODE
|
|
||||||
printVector(&vectors[vectorCount]);
|
printVector(&vectors[vectorCount]);
|
||||||
vectorCount++;
|
vectorCount++;
|
||||||
} while (delta < e && vectorCount < MAX_ITERATION_STEPS);
|
} while (delta > e && vectorCount < MAX_ITERATION_STEPS);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (method ==1){
|
||||||
|
|
||||||
|
|
||||||
|
do{
|
||||||
|
delta = 0.0;
|
||||||
|
if (vectorCount==1){
|
||||||
|
initVector(&vectors[vectorCount], b->n);
|
||||||
|
memcpy(vectors[vectorCount].data, x->data, b->n * sizeof(double));}
|
||||||
|
else {
|
||||||
|
initVector(&vectors[vectorCount], b->n);
|
||||||
|
memcpy(vectors[vectorCount].data, vectors[vectorCount-1].data, b->n * sizeof(double));}
|
||||||
|
|
||||||
|
for (int i = 0; i<b->n; i++){
|
||||||
|
temp=0.0;
|
||||||
|
for (int j=0; j<b->n; j++){
|
||||||
|
if (j!=i){
|
||||||
|
temp = temp + A->data[i][j]*vectors[vectorCount].data[j];
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vectors[vectorCount].data[i] = (b->data[i]-temp)/A->data[i][i];
|
||||||
|
delta=fmax(fabs(vectors[vectorCount].data[i]-vectors[vectorCount-1].data[i]),delta);
|
||||||
|
|
||||||
|
}
|
||||||
|
printVector(&vectors[vectorCount]);
|
||||||
|
vectorCount++;
|
||||||
|
}while(delta > e && vectorCount < MAX_ITERATION_STEPS);
|
||||||
|
}
|
||||||
|
|
||||||
// MAX_ITERATION_STEPS enthält die maximal zulässige Anzahl an Iterationsschritten (100)
|
// MAX_ITERATION_STEPS enthält die maximal zulässige Anzahl an Iterationsschritten (100)
|
||||||
// Die einzelnen Vektoren müssen noch mit initVector initialisiert werden
|
// Die einzelnen Vektoren müssen noch mit initVector initialisiert werden
|
||||||
@@ -289,11 +322,9 @@ Vector* solve(Method method, Matrix* A, Vector* b, Vector* x, double e) {
|
|||||||
|
|
||||||
// on success
|
// on success
|
||||||
// Sei x die Anzahl der durchgeführten Iterationschritte. Dann setzt vectors[x+1].n = 0. Damit weiß das folgende Programm wie viele Schritte getätigt wurden.
|
// Sei x die Anzahl der durchgeführten Iterationschritte. Dann setzt vectors[x+1].n = 0. Damit weiß das folgende Programm wie viele Schritte getätigt wurden.
|
||||||
return vectors;
|
vectors[vectorCount].n=0;
|
||||||
|
|
||||||
// on failure
|
return vectors;
|
||||||
free(vectors);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Matrix* createMatrix(void) {
|
inline Matrix* createMatrix(void) {
|
||||||
|
|||||||
Reference in New Issue
Block a user