NUMERICAL METHOD LAB 4:Gauss Elimination Method
Gauss Elimination Method
THEORY
Gauss Elimination method can be adopted to find the solution of linear simultaneous equations arising in engineering problems. In the method, equations are solved by elimination procedure of the unknowns successively.
In Gauss-Elimination method, these equations are solved by eliminating the unknowns successively.
For this, let us first consider the following three equations:
a1x + b1y + c1z = d1
a2x + b2y + c2z = d2
a3x + b3y + c3z = d3
a2x + b2y + c2z = d2
a3x + b3y + c3z = d3
Assuming a1 ≠ 0, x is eliminated from the second equation by subtracting (a2/ a1) times the first equation from the second equation. In the same way, the C code presented here eliminates x from third equation by subtracting (a3/a1) times the first equation from the third equation.
Then we get the new equations as:
a1x + b1y + c1z = d1
b’2y + c’2z = d’2
c’’3z = d’’3
b’2y + c’2z = d’2
c’’3z = d’’3
The elimination procedure is continued until only one unknown remains in the last equation. After its value is determined, the procedure is stopped.
z= d’’3 / c’’3
y=(d’2 – c’2z) / b’2
x=( d1- c1z- b1y)/ a1
y=(d’2 – c’2z) / b’2
x=( d1- c1z- b1y)/ a1
Gauss Elimination Algorithm:
- Start
- Declare the variables and read the order of the matrix n.
- Take the coefficients of the linear equation as:
Do for k=1 to n
Do for j=1 to n+1
Read a[k][j]
End for j
End for k - Do for k=1 to n-1
Do for i=k+1 to n
Do for j=k+1 to n+1
a[i][j] = a[i][j] – a[i][k] /a[k][k] * a[k][j]
End for j
End for i
End for k - Compute x[n] = a[n][n+1]/a[n][n]
- Do for k=n-1 to 1
sum = 0
Do for j=k+1 to n
sum = sum + a[k][j] * x[j]
End for j
x[k] = 1/a[k][k] * (a[k][n+1] – sum)
End for k - Display the result x[k]
- Stop
Source Code for Gauss Elimination Method in C:
C Program for Gauss Elimination Method
C
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#include<stdio.h>
int main()
{
int i,j,k,n;
float A[20][20],c,x[10],sum=0.0;
printf(“nEnter the order of matrix: “);
scanf(“%d”,&n);
printf(“nEnter the elements of augmented matrix row-wise:nn”);
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
printf(“A[%d][%d] : “, i,j);
scanf(“%f”,&A[i][j]);
}
}
for(j=1; j<=n; j++) /* loop for the generation of upper triangular matrix*/
{
for(i=1; i<=n; i++)
{
if(i>j)
{
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
A[i][k]=A[i][k]–c*A[j][k];
}
}
}
}
x[n]=A[n][n+1]/A[n][n];
/* this loop is for backward substitution*/
for(i=n–1; i>=1; i—)
{
sum=0;
for(j=i+1; j<=n; j++)
{
sum=sum+A[i][j]*x[j];
}
x[i]=(A[i][n+1]–sum)/A[i][i];
}
printf(“nThe solution is: n”);
for(i=1; i<=n; i++)
{
printf(“nx%d=%ft”,i,x[i]); /* x1, x2, x3 are the required solutions*/
}
return(0);
|
