-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGauss.c
More file actions
99 lines (84 loc) · 2.14 KB
/
Gauss.c
File metadata and controls
99 lines (84 loc) · 2.14 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
93
94
95
96
97
98
99
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float modulo(float x){
if (x < 0)
x = -x;
return x;
}
int main(){
int n = 0;
float mult = 0, temp = 0;
printf ("Digite a ordem da matriz:\n");
scanf ("%d",&n);
float matriz[n][n];
float b[n], vetorResposta[n];
for (int i = 0; i < n; i++){
for (int j = 0; j < n;j++){
printf ("Digite a[%d][%d]: ",i,j);
scanf ("%f",&matriz[i][j]);
}
printf ("Digite b[%d]: ",i);
scanf ("%f",&b[i]);
}
/* Método de gauss */
for (int k = 0; k < n; k++){
/* Pivoteamento parcial */
float maxEl = modulo(matriz[k][k]);
int linhaDoMax = k;
// Procura o maximo em modulo na coluna //
for (int i=k+1; i<n; i++) {
if (modulo(matriz[i][k]) > maxEl) {
maxEl = modulo(matriz[i][k]);
linhaDoMax = i;
}
}
// Se o maximo for zero nao tem como calcular a resposta
if (maxEl == 0){
printf("\n ----Matriz invalida----");
return 0;
}
// Troca linha atual com a linha de maior pivo //
for (int i = 0; i<n; i++){
float temp = matriz[linhaDoMax][i];
matriz[linhaDoMax][i] = matriz[k][i];
matriz[k][i] = temp;
}
// Trocar também no vetor resposta
float temp = b[linhaDoMax];
b[linhaDoMax] = b[k];
b[k] = temp;
/* Triangularizacao */
for (int i = k+1; i < n; i++){
mult = matriz[i][k] / matriz[k][k];
matriz[i][k] = 0;
b[i] -= mult * b[k];
for (int j = k+1; j <= n; j++){
matriz[i][j] -= mult * matriz[k][j];
}
}
}
/* Imprime matriz e vetor b*/
printf("\n\nMatriz:\n\n");
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
printf(" %.3f ", matriz[i][j]);
}
printf ("| %.3f ",b[i]);
printf("\n\n");
}
/* Substituição de cima para baixo */
vetorResposta[n-1] = b[n-1] / matriz[n-1][n-1];
for(int i=(n-2);i>=0;i--){
temp = b[i];
for(int j=(i+1);j<n;j++){
temp -= (matriz[i][j] * vetorResposta[j]);
}
vetorResposta[i] = temp / matriz[i][i];
}
/* Imprime vetor de respostas */
printf ("\n\nResposta:\n\n");
for (int i = 0; i < n; i++)
printf(" x%d = %.3f \n",i,vetorResposta[i]);
return 0;
}