Metodo dos gradientes em c

Disponível somente no TrabalhosFeitos
  • Páginas : 2 (384 palavras )
  • Download(s) : 0
  • Publicado : 7 de abril de 2013
Ler documento completo
Amostra do texto
#include
#include
#include

/*
Felipe Osorio ThomÈ 7696409
Vitor Lima Garcia 7573361
Thales Freschi De Andrade 7573527
*/

double **matrixAllocate(int, int);
double **freeMatrix(double**, int, int);
double **matrixMul(double **,int, int, double **, int, int);
double **matrixSub(double **, double **, int, int);
double **matrixAdd(double **, double **, int, int);
double**transposedMatrix(double **, int, int);
double **scalarMul(double **, int, int, double);
void metodoGradientes(double **, double **, double **, double, int);

int main()
{
int i, j, n;
double**A, **b, **x, p;
scanf("%d", &n);
getchar();
A = matrixAllocate(n, n);
for(i = 0; i < n; i++)
{
for(j = i; j < n; j++)
{
scanf(" %lf",&A[i][j]);
getchar();
if(i != j) A[j][i] = A[i][j];
}
}
b = matrixAllocate(n, 1);
for(i = 0; i < n; i++)
{
scanf("%lf", &b[i][0]);getchar();
}
x = matrixAllocate(n, 1);
for(i = 0; i < n; i++)
{
scanf("%lf", &x[i][0]);
getchar();
}
scanf("%lf", &p);
getchar();metodoGradientes(A, b, x, p, n);

return(0);
}

void metodoGradientes(double **A, double **b, double **x, double p, int n)
{
int i, k;
double alfa, **Ax, **r, **rt, **rt_r, **Ar, **rt_Ar,**alfa_r, **x_aux, error;
for(k = 0; k < 10; k++)
{
Ax = matrixMul(A, n, n, x, n, 1);
r = matrixSub(b, Ax, n, 1);
rt = transposedMatrix(r, n, 1);
rt_r =matrixMul(rt, 1, n, r, n, 1);
Ar = matrixMul(A, n, n, r, n, 1);
rt_Ar = matrixMul(rt, 1, n, Ar, n, 1);
if(rt_Ar[0][0] != 0) alfa = rt_r[0][0] / rt_Ar[0][0];
else alfa = 0;alfa_r = scalarMul(r, n, 1, alfa);
x_aux = matrixAdd(x, alfa_r, n, 1);
error = sqrt(rt_r[0][0]);
freeMatrix(x, n, 1);
x = x_aux;
if(error < p)...
tracking img