Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Gauss Elimination Method

# include <stdio.h>
# include <conio.h>
void main()
{
int i, j, k, n ;
float a[20][20], x[20] ;
double s, p ;
printf("Enter the number of equations : ") ;
scanf("%d", &n) ;
printf("\nEnter the co-efficients of the equations :\n\n") ;
for(i = 0 ; i < n ; i++)
{
for(j = 0 ; j < n ; j++)
{
printf("a[%d][%d] = ", i + 1, j + 1) ;
scanf("%f", &a[i][j]) ;
}
printf("b[%d] = ", i + 1) ;
scanf("%f", &a[i][n]) ;
}
for(k = 0 ; k < n - 1 ; k++)
{

for(i = k + 1 ; i < n ; i++)


{
p = a[i][k] / a[k][k] ;
for(j = k ; j < n + 1 ; j++)
a[i][j] = a[i][j] - p * a[k][j] ;
}
}
x[n-1] = a[n-1][n] / a[n-1][n-1] ;
for(i = n - 2 ; i >= 0 ; i--)
{
s=0;
for(j = i + 1 ; j < n ; j++)
{
s += (a[i][j] * x[j]) ;
x[i] = (a[i][n] - s) / a[i][i] ;
}
}
printf("\nThe result is :\n") ;
for(i = 0 ; i < n ; i++)
printf("\nx[%d] = %.2f", i + 1, x[i]) ;
getch() ;
}

RESULT:
Enter the number of equations : 4

Enter the co-efficients of the equations :

a[1][1] = 10
a[1][2] = -7
a[1][3] = 3
a[1][4] = 5
b[1] = 6
a[2][1] = -6
a[2][2] = 8
a[2][3] = -1
a[2][4] = -4
b[2] = 5
a[3][1] = 3
a[3][2] = 1
a[3][3] = 4
a[3][4] = 11
b[3] = 2
a[4][1] = 5
a[4][2] = -9
a[4][3] = -2
a[4][4] = 4

b[4] = 7

The result is :

x[1] = 5.00
x[2] = 4.00
x[3] = -7.00
x[4] = 1.00

Gauss Seidel Iteration Method


include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.0001
#define X1(x2,x3) ((17 - x2 + 2*(x3))/20)
#define X2(x1,x3) ((-18 - 3*(x1) + (x3))/20)
#define X3(x1,x2) ((25 - 2*(x1) + 3*(x2))/20)
void main()
{
double x1=0,x2=0,x3=0,y1,y2,y3;
int i=0; #
printf("\n__________________________________________\n");
printf("\n x1\t\t x2\t\t x3\n");
printf("\n__________________________________________\n");
printf("\n%f\t%f\t%f",x1,x2,x3);
do
{
y1=X1(x2,x3);
y2=X2(y1,x3);
y3=X3(y1,y2);

if(fabs(y1-x1)<ESP && fabs(y2-x2)<ESP && fabs(y3-x3)<ESP )


{
printf("\n__________________________________________\n");
printf("\n\nx1 = %.3lf",y1);
printf("\n\nx2 = %.3lf",y2);
printf("\n\nx3 = %.3lf",y3);
i = 1;
}
else
{
x1 = y1;
x2 = y2;
x3 = y3;
printf("\n%f\t%f\t%f",x1,x2,x3);
}
}while(i != 1);
getch();
}

Output:
__________________________________________

x1

x2

x3

__________________________________________

0.000000

0.000000

0.000000

0.850000

-1.027500

1.010875

1.002463

-0.999826

0.999780

0.999969

-1.000006

1.000002

__________________________________________

x1 = 1.000

x2 = -1.000

x3 = 1.000

You might also like