Sie sind auf Seite 1von 4

#include <stdio.

h>
#include <conio.h>
#define tam 15

int n,m,i,j,k,h,l;
float A[tam][tam],X[tam],C[tam][tam],B[tam],D[tam];

void lee_datos(void)
{
printf("Ingrese el numero ecuaciones ");
scanf("%d",&m);
printf("\n");
printf("Ingrese el numero de incognitas ");
scanf("%d",&n);
printf("\n");
if(m<n)
{
printf("El sistema tiene infinitas soluciones");
}
else
{
if(n<m)
{
printf("El sistema no tiene solución");
}
else
{
printf("Ingrese la matriz de coeficientes (Tenga en cuenta que el
elemento a[11] debe ser diferente de 0) \n\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
printf("a[%d%d] ",i,j);
scanf("%f",&A[i][j]);
}
}
printf("\n");
if(A[1][1]!=0)
{
printf("Ingrese el vector de soluciones\n");
for(i=1;i<=m;i++)
{
printf("x[%d] ",i);
scanf("%f",&X[i]);
}
printf("\n");
}
else
{
printf("Matriz no valida");
}
}
}
}

void cambiar_fila(float U[tam][tam],int x,int y)


{
float F[tam][tam];
for(j=1;j<=m;j++)
{
F[x][j]=U[x][j];
}

for(j=1;j<=m;j++)
{
U[x][j]=U[y][j];
}

for(j=1;j<=m;j++)
{
U[y][j]=F[x][j];
}
}

float valor_absoluto(float x)
{
float val;
if(x<0)
{
val=-x;
}
else
{
val=x;
}
return(val);
}

void matriz_triangular(float V[tam][tam], float U[tam])


{
//construir la matriz ampliada
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
C[i][j] = A[i][j];
}
}
for(i=1;i<=m;i++)
{
C[i][n+1] = X[i];
}

//convertirla en matriz triangular superior


for(i=1;i<=m;i++)
{
for(k=1;k<=m-1;k++)
{
//coeficiente
B[k]=-C[i+k][i]/C[i][i];
}

//nueva fila
for(h=i+1;h<=m+1;h++)
{
for(l=1;l<=n+1;l++)
{
C[h][l]=C[h][l]+B[h-i]*C[i][l];
}
}
}

//calcular incógnitas
D[n]=C[m][n+1]/C[m][n];
float tot=0, tot1=0;
for(i=m-1;i>=1;i--)
{
tot=C[i][n+1];
for(j=n;j>=i+1;j--)
{
tot1=tot1+C[i][j]*D[j];
}
D[i]=(tot-tot1)/C[i][i];
tot1=0;
}
}

//resultados
void main(void)
{
printf("\tMetodo de Eliminacion Gaussina para resolucion de Sist. de
Ecuaciones\n\n");
lee_datos();
{
if((m==n) && (A[1][1]!=0))
{
matriz_triangular(A,X);
printf("\nLa matriz triangular es:\n\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n+1;j++)
{
printf("%f\t",C[i][j]);
}
printf("\n");
}
printf("\n\n");
if((valor_absoluto(C[m][n])==0) &&
(valor_absoluto(C[m][n+1])==0))
printf("El sistema tiene infinitas soluciones");
else
{
if((valor_absoluto(C[m][m])==0) && (C[m][m+1]!=0))
{
printf("El sistema no tiene solucion\n");
}
else
{
printf("La solución al sistema es:\n");
for(i=1;i<=m;i++)
{
printf("X[%d]:\t%f\n",i,D[i]);
}
}
}
}
}

getch();
}

Das könnte Ihnen auch gefallen