Home » Numerical Techniques Using C++ » Gauss Elimination with Partial Pivoting C++

# Gauss Elimination with Partial Pivoting C++

## Intro: Gauss Elimination with Partial Pivoting

Gauss Elimination with Partial Pivoting is a direct method to solve the system of linear equations.

In this method, we use Partial Pivoting i.e. you have to find the pivot element which is the highest value in the first column & interchange this pivot row with the first row.

Then you can use the normal Gauss Elimination method to transform the Augmented Matrix into the upper triangular matrix.

## Why Partial Pivoting?

Gauss elimination has a limitation where it fails to show exact solution.

1. Division  by Zero
2. Round Off Errors

So to overcome this problem we use Partial Pivoting with Gauss elimination.

## Ex. Find the Solution of following Linear Equations using Gauss Elimination with Partial Pivoting?

x + y + z = 6
x – y + z = 2
2x – y + 3z = 9

### Step 1:- Write the given System of Equations in the form of AX=b, i.e. Matrix Form.

Where,
A = Coefficient Matrix,
X = variables (Column Matrix),
B = constants (Column Matrix.

### Step 3:  Find the Pivot Element.

1. Select Largest Absolute Value from 1st Column.

2. Interchange Pivot Row with 1st Row.

3. Now 1st element ( Pivot element ) should be 1.

### Step 4: Transform into Upper Triangular Matrix Form ( Echelon ).

Echelon: Upper Triangular Matrix with Diagonal Elements 1 or Non-zero.

1. Repeat Step 3 in Sub Matrix. Find New Pivot Element.

2. Interchange R2 with R3 ( pivot row).

## C++ Program Partial Pivoting

```.hljs{display:block;overflow-x:auto;padding:.5em;color:#abb2bf;background:#282c34}.hljs-keyword,.hljs-operator{color:#f92672}.hljs-pattern-match{color:#f92672}.hljs-pattern-match .hljs-constructor{color:#61aeee}.hljs-function{color:#61aeee}.hljs-function .hljs-params{color:#a6e22e}.hljs-function .hljs-params .hljs-typing{color:#fd971f}.hljs-module-access .hljs-module{color:#7e57c2}.hljs-constructor{color:#e2b93d}.hljs-constructor .hljs-string{color:#9ccc65}.hljs-comment,.hljs-quote{color:#b18eb1;font-style:italic}.hljs-doctag,.hljs-formula{color:#c678dd}.hljs-section,.hljs-name,.hljs-selector-tag,.hljs-deletion,.hljs-subst{color:#e06c75}.hljs-literal{color:#56b6c2}.hljs-string,.hljs-regexp,.hljs-addition,.hljs-attribute,.hljs-meta-string{color:#98c379}.hljs-built_in,.hljs-class .hljs-title{color:#e6c07b}.hljs-attr,.hljs-variable,.hljs-template-variable,.hljs-type,.hljs-selector-class,.hljs-selector-attr,.hljs-selector-pseudo,.hljs-number{color:#d19a66}.hljs-symbol,.hljs-bullet,.hljs-link,.hljs-meta,.hljs-selector-id,.hljs-title{color:#61aeee}.hljs-emphasis{font-style:italic}.hljs-strong{font-weight:700}.hljs-link{text-decoration:underline}.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines) {white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}``` //gauss elimination Partial Pivoting
//wikkihut.com

#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<math.h>

using namespace std;

int main()
{

int n,i,j,k,temp;
float a[10][10],c,d[10]={0};

cout<<"Enter No of equations ? ";
cin>>n;
cout<<"Coefficient of all Variables : \n";
for(i=0;i<n;i++)
{
cout<<"equation: "<<i+1<< "   ";
for(j=0;j<=n;j++)
cin>>a[i][j];
}
// partial pivoting

for(i=n-1;i>0;i--)
{
if(a[i-1][0]<a[i][0])
for(j=0;j<=n;j++)
{
c=a[i][j];
a[i][j]=a[i-1][j];
a[i-1][j]=c;
}
}
//DISPLAY MATRIX

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

//changing to upper triangular matrix
//Forward elimination process

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

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

// DISPLAYING UPPER TRIANGULAr MATRIX

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

printf("\n");
}

//Backward Substitution method

for(i=n-1;i>=0;i--)
{
c=0;
for(j=i;j<=n-1;j++)
c=c+a[i][j]*d[j];

d[i]=(a[i][n]-c)/a[i][i];
}

// DISPLAY

for(i=0;i<n;i++)
cout<<d[i]<<endl;

getch();
return 0;

//wikkihut.com
}
``````

Other Related Numerical Methods.