jacobi and gauss solve function working
This commit is contained in:
26
main.c
26
main.c
@@ -78,7 +78,7 @@ int main(int argc, char* argv[]) {
|
||||
//printVector(b);
|
||||
//puts("Vector x:");
|
||||
//printVector(x);
|
||||
|
||||
|
||||
puts("Please enter the algorithm to use:\n\t0: Jacobi (default)\n\t1: Gauss-Seidel");
|
||||
int algorithm;
|
||||
|
||||
@@ -212,7 +212,7 @@ bool load(const char* filename, Matrix* matrix, Vector* b, Vector* x) {
|
||||
|
||||
// initialize vector x with zeros
|
||||
memset(x->data, 0, cols * sizeof(double));
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
success:
|
||||
@@ -234,7 +234,7 @@ int readMatrixLine(FILE* file, double* matrixLine, int maxCols) {
|
||||
if(fscanf(file, "%lf", &buffer) != 1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
matrixLine[col] = buffer;
|
||||
|
||||
col++;
|
||||
@@ -253,19 +253,29 @@ void flushStdin(void) {
|
||||
}
|
||||
|
||||
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);
|
||||
memcpy(vectors[0].data, b->data, b->n * sizeof(double));
|
||||
memcpy(vectors[0].data, x->data, b->n * sizeof(double));
|
||||
|
||||
int vectorCount = 1;
|
||||
|
||||
double temp;
|
||||
double delta = 0.0;
|
||||
|
||||
do {
|
||||
|
||||
delta= 0.0;
|
||||
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-1].data[j];
|
||||
}
|
||||
}
|
||||
vectors[vectorCount].data[i]=1/A->data[i][i]*(b->data[i]-temp);
|
||||
delta=fmax(fabs(vectors[vectorCount].data[i]-vectors[vectorCount-1].data[i]),delta);
|
||||
}
|
||||
// CODE
|
||||
|
||||
printVector(&vectors[vectorCount]);
|
||||
vectorCount++;
|
||||
} while (delta < e && vectorCount < MAX_ITERATION_STEPS);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user