-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJACOBI.c
More file actions
92 lines (84 loc) · 1.92 KB
/
Copy pathJACOBI.c
File metadata and controls
92 lines (84 loc) · 1.92 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//Jacobi
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define t 50
//esse programa calcula sitemas lineares at� t vari�veis,inicialmente,
//define t como 50, mas para calcular um sistema maior basta modificar
// o valor de t no define acima.
int main(){
int i,j,v,d;
float a[t][t],x[t],b[t],aux[t],auxx[t],erro,e,maiore,maiorx,auxe[t];
//preenchi a matriz e alguns vetores com 0, para n�o ocorrerem erros.
for(i=0;i<t;i++){
for(j=0;j<t;j++)
a[i][j]=0;
}
for(i=0;i<t;i++){
auxe[i]=0;
x[i]=0;
}
//coleta de dados
printf("GAUSS-JACOBI");
printf("\nQuantas variaveis deseja calcular:");
scanf("%d",&v);
if(v>t){
printf("\nEsse programa calcula sistemas ate %d variaveis!\n",t);
exit(0);
}
printf("Digite a precisao desejada:");
scanf("%f",&erro);
for(i=0;i<v;i++){
printf("Digite o valor da aproximacao inicial de x%d:",i+1);
scanf("%f",&x[i]);
}
for(i=0;i<v;i++){
for(j=0;j<v;j++){
printf("Digite a[%d][%d]:",i+1,j+1);
scanf("%f",&a[i][j]);
}
}
for(i=0;i<v;i++){
printf("Digite o valor de b[%d]:",i+1);
scanf("%f",&b[i]);
}
//processamento
e=100;
while(e>erro){
//todos os vetores que come�am com aux s�o vetores auxiliares
//(usei para facilitar o processamento).
for(d=0;d<v;d++){
auxx[d]=x[d];
}
//processo interativo:
for(i=0;i<v;i++){
aux[i]=b[i];
for(j=0;j<v;j++){
if(i==j){
}
if(i!=j){
aux[i]=aux[i]-(a[i][j]*auxx[j]);
}
}
x[i]=(1/a[i][i])*(aux[i]);
}
//c�lculo do erro:
for(i=0;i<v;i++){
auxe[i]=fabs(x[i]-auxx[i]);
}
maiore=auxe[0];
for(i=1;i<v;i++){
if(maiore<auxe[i+1]) maiore=auxe[i+1];
}
maiorx=x[0];
for(i=1;i<v;i++){
if(maiorx<x[i+1]) maiorx=x[i+1];
}
e=maiore/maiorx;
}
//resultados:
for(i=0;i<v;i++){
printf("\n x[%d]=%f",i+1,x[i]);
}
return 0;
}