# Metodo dos gradientes em c

Páginas: 2 (384 palavras) Publicado: 7 de abril de 2013
#include
#include
#include

/*
Felipe Osorio ThomÈ 7696409
Vitor Lima Garcia 7573361
*/

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);

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)...

Por favor, assinar para o acesso.