This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seidel.c
74 lines (63 loc) · 1.73 KB
/
seidel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>
#include "seidel.h"
#include <math.h>
#define tol 0.00000000001
bool terminar(double xi, double x0){
double temp=fabs(xi-x0)/fabs(xi);
if(temp<=tol){
return true;
}
return false;
}
void seidel(double **matriz, double *b, int n, double *xSeidel, int *cS){
double *xA=(double*)malloc(sizeof(double)*n);
double soma;
int contador;
//VALORES INICIAIS
for (int i= 0; i < n; i++) {
if(b[i]!=0){
xSeidel[i] = b[i]/matriz[i][i];
*cS += 1;
}else{
xSeidel[i]=0;
}
xA[i] = 1;
}
//ITERACOES
while(true) {
for (int i = 0; i < n; i++) {
soma = 0;
for (int j = 0; j < n; j++) {
if(i!=j && matriz[i][j]!=0 && xSeidel[j]!=0){
soma += matriz[i][j]*xSeidel[j];
*cS+=2;
}
}
if(soma==0 && b[i]!=0){
xSeidel[i] = b[i] / matriz[i][i];
*cS += 1;
} else {
xSeidel[i] = (b[i]-soma) / matriz[i][i];
*cS += 2;
}
}
//CONFERE SE ACABOU ATRAVES DA TOLERANCIA
contador = 1;
for(int i = 0; i < n ; i++) {
if(terminar(xSeidel[i], xA[i])){
contador++;
}else{
break;
}
if(contador == n){//TRUE = ACABOU
free(xA);
return;
}
}
//ATUALIZA X-ANTIGO
for(int i = 0; i < n ; i++){
xA[i] = xSeidel[i];
}
}
}