gauss elimination method with partial pivoting
TRANSCRIPT
Gauss Elimination Method with Partial Pivoting
Goal and Purpose:
Gauss Elimination involves combining equations to eliminate unknowns.
Although it is one of the earliest methods for solving simultaneous
equations, it remains among the most important algorithms in use now a
days and is the basis for linear equation solving on many popular software
packages.
Description of the problem:
In the method of Gauss Elimination the fundamental idea is to add multiples
of one equation to the others in order to eliminate a variable and to continue
this process until only one variable is left. Once this final variable is
determined, its value is substituted back into the other equations in order to
evaluate the remaining unknowns. This method, characterized by step‐by‐step elimination of the variables.
To perform row reduction on a matrix, one uses a sequence of elementary
row operations to modify the matrix until the lower left-hand corner of the
matrix is filled with zeros, as much as possible. There are three types of
elementary row operations:
1) Swapping two rows.
2) Multiplying a row by a non-zero number.
3) Adding a multiple of one row to another row.
The technique of pivoting has been developed to partially avoid
division by zero problem.
Algorithm
1. Start
2. Declare the variables and read the order of the matrix n.
3. 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
4. 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
5. Compute x[n] = a[n][n+1]/a[n][n]
6. 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
7. Display the result x[k]
8. Stop
C-Code :
#include<stdio.h>
int main()
{
int i,j,p,n;
double a[10][10],b,c,d,x1,x2,x3,temp,max;
printf("Enter the size of matrix : ");
scanf("%d",&n);
printf("Enter the matrix \n");
for(i=0;i<n;i++)
{
for(j=0;j<=n;j++)
{
scanf("%lf",&a[i][j]);
}
}
max=a[0][0];
if(a[1][0]>=max)
p=1;
if(a[2][0]>=max)
p=2;
for(j=0;j<=n;j++)
{
temp=a[p][j];
a[p][j]=a[0][j];
a[0][j]=temp;
}
printf("AFTER PIVOTOING\n");
for(i=0;i<n;i++)
{
for(j=0;j<=n;j++)
{
printf("%lf ",a[i][j]);
}
printf("\n\n");
}
b=a[1][0];
c=a[2][0];
d=a[0][0];
for(j=0;j<=i;j++)
{
a[1][j]=(a[1][j]-((b/(double)d)*a[0][j]));
a[2][j]=(a[2][j]-((c/(double)d)*a[0][j]));
}
b=a[2][1];
d=a[1][1];
for(j=0;j<=n;j++)
{
a[2][j]=(a[2][j]-((b/(double)d)*a[1][j]));
}
printf("AFTER ELIMINATON\n");
for(i=0;i<n;i++)
{
for(j=0;j<=n;j++)
{
printf("%lf ",a[i][j]);
}
printf("\n\n");
}
x3=(a[2][3]/(double)a[2][2]);
x2=((a[1][3]-(a[1][2]*x3))/(double)a[1][1]);
x1=((a[0][3]-(a[0][1]*x2)-(a[0][2]*x3))/(double)a[0][0]);
printf("the solution are:\n");
printf("x1=%lf\n\n",x1);
printf("x2=%lf\n\n",x2);
printf("x3=%lf\n\n",x3);
return 0; }
Results:
Sample Input:
Size of matrix : 3
Enter Matrix :
4 1 -1 -2
5 1 2 4
6 1 1 6
Output:
After Pivoting:
6 1 1 6
5 1 2 4
4 1 -1 -2
After Elimination:
6 1 1 6
0 0.166667 1.166667 -1
0 0 -4 -4
Conclusion:
Gauss elimination method is one of the earliest methods for solving
simultaneous equations.
Appendices :
Gauss Seidel Method
Goal and purposes:
The main goal and purpose of the program is to solve a system of n
linear simultaneous equation using Gauss Seidel method.
Description of the problem:
Assume that there given a set of n equations:
{A}{X}={B}
suppose there is 3*3 set of equations. If the diagonal elements are all nonzero,
the first equations
can be solved for x1, the second for x2 and the third for x3 to yield
x1= (b1-a12-a13x3)/a11
x2=(b2-a21x1-a23x3)/a22
x3=(b3-a31x1-a32x2)/a33
Now, the solution process can be started by choosing guesses for the x’s.
Algorithm with C - code:
#include<stdio.h>
int main()
{
int i,j;
double a[10][10],x1,x2,x3,prex1,es,b,eax1,ea,eax2,eax3,prex2,prex3;
printf("enter the element of the matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<=3;j++)
{
scanf("%lf",&a[i][j]);
}
}
printf("initial gueses:\n");
printf("x1=\n");
scanf("%lf",&x1);
printf("x2=\n");
scanf("%lf",&x2);
printf("x3=\n");
scanf("%lf",&x3);
i=1;
prex1=x1;
prex2=x2;
prex3=x3;
while(1)
{
printf("ITERATION NO.%d\n",i);
x1=(((a[0])[3]-(a[0][1]*x2)-(a[0][2]*x3))/(double)a[0][0]);
printf("x1.%d=%lf\n",i,x1);
eax1=((x1-prex1)/(double)x1)*100;
prex1=x1;
printf("eax1=%lf\n",eax1);
x2=(((a[1])[3]-(a[1][0]*x1)-(a[1][2]*x3))/(double)a[1][1]);
printf("x2.%d=%lf\n",i,x2);
eax2=((x2-prex2)/(double)x2)*100;
prex2=x2;
printf("eax2=%lf\n",eax2);
x3=(((a[2])[3]-(a[2][0]*x1)-(a[2][1]*x2))/(double)a[2][2]);
printf("x3.%d=%lf\n",i,x3);
eax3=((x3-prex3)/(double)x3)*100;
prex3=x3;
printf("eax3=%lf\n",eax3);
printf("\n");
if(i==5)
break;
i++;
}
return 0;
}
Results:
Input:
25 5 1 106.8
64 8 1 177.2
144 12 1 279.2
Initial guesses:
1
2
5
Output:
X1 :3.672 X2: -7.851 X3: -155.356
Eax1 :72.7669 Eax2 : 125.474 Eax3 : 103.218
X1 : 12.0564 X2: -54.822 X3 : -798.343
Eax1: 69.5433 Eax2: 85.6948 Eax3: 80.5402
X1: 47.1821 X2: -255.514 X3: -3448.46
Eax1: 74.447 Eax2: 78.5209 Eax3: 76.852
Apendices :
Conclusion:
The Gauss Seidel method is the most commonly used iterative method.