Tuesday, 1 April 2014

Gauss Elimination Method

#include<stdio.h>
#include<math.h>
int main()
{
int n=3,i,j,k;
float a[10][10],c[10],x[10];
printf("Enter the no. of equations :\n");
scanf("%d", &n);
printf("Enter the right hand side of equation ");
for(i=0;i<=n-1;i++)
{
scanf("%f",&c[i]);
}

for(i=0;i<=n-1;i++)
{
printf("Enter the coefficient of equation %d.\n",i+1);
for(j=0;j<=n-1;j++)
{
scanf("%f",&a[i][j]);
}

}

for(k=0;k<=n-2;k++)
{
for(i=k+1;i<=n-1;i++)
{
for(j=k+1;j<=n-1;j++)
{
a[i][j]=a[i][j]-((a[i][k]/a[k][k])*a[k][j]);
}
c[i]=c[i]-((a[i][k]/a[k][k])*c[k]);
}

}

for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%f ",a[i][j]);
}
printf("\n");
}
printf("\n");

x[n-1]=c[n-1]/a[n-1][n-1];
printf("The solution is :\n");
printf("x[%d] = %f\n",n-1,x[n-1]);
for(k=0;k<=n-2;k++)
{
i=n-k-2;
for(j=i+1;j<=n-1;j++)
{
c[i]=c[i]-(a[i][j]*x[j]);
}
x[i]=c[i]/a[i][i];
printf("x[%d] = %f\n",i,x[i]);
}
return 0;
}

No comments:

Post a Comment