0% found this document useful (0 votes)
364 views3 pages

LU Decomposition With Partial Pivoting C Program

This C++ program performs Gaussian elimination on a 3x3 matrix A to decompose it into matrices L, U, and P such that A = L*U*P. It initializes matrix A, performs row operations to put it in upper triangular form U, and calculates the matrices L, U, and P. It then outputs the original matrix A and the decomposed matrices U, P, and L.

Uploaded by

Amit Makhija
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
364 views3 pages

LU Decomposition With Partial Pivoting C Program

This C++ program performs Gaussian elimination on a 3x3 matrix A to decompose it into matrices L, U, and P such that A = L*U*P. It initializes matrix A, performs row operations to put it in upper triangular form U, and calculates the matrices L, U, and P. It then outputs the original matrix A and the decomposed matrices U, P, and L.

Uploaded by

Amit Makhija
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<iostream>

#include<iomanip>
#include<math.h>
#include<cstdio>
#include <stdlib.h>
using namespace std;
int main()
{
double a[10][10], l[10][10], q[10][10], b[10][10], c[10][10],w[10][10], r[10][10],
mul[10][10], s[10][10], lo[10][10], temp[10], temp1[10];
int p , i, j, k ;
float max, max1, mult, d;

a[1][1] = 1; a[1][2] = 2; a[1][3] = 4;


a[2][1] = 4; a[2][2] = 6; a[2][3] = 5;
a[3][1] = 2; a[3][2] = 2; a[3][3] = 7;

for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
r[i][j] = a[i][j] ;

for (i =1; i<=3; i++)


{
for (j =1; j<=3; j++)
{
if (i==j)
{
q[i][j] = 1;
}
else
{
q[i][j] = 0;
}
std::cout<<q[i][j]<<" ";
}
std::cout<<"\n";
}

std::cout<<"Matrix A is"<<"\n";
for (i = 1; i<=3; i++)
{
for (j = 1; j<=3; j++)
{
std::cout<<a[i][j]<<" ";
}
std::cout<<"\n";
}
for (i = 1; i<=3; i++)
{
max = a[i][i];
p = i;
for (int k = i+1; k<=3; k++)
{
if(fabs(max)<fabs(a[k][i]))
{
max = a[k][i];
p = k;
}
}
for(j = 1; j <=3; j++)
{
temp[j] = a[p][j];
a[p][j] = a[i][j];
a[i][j] = temp[j];

temp1[j] = q[p][j];
q[p][j] = q[i][j];
q[i][j] = temp1[j];
}

for(j=i+1;j<=3;j++)
{
mult = a[j][i]/a[i][i];
for(k=1;k<=3;k++)
a[j][k] -= mult*a[i][k];
}
}
std::cout<<"\n";
std::cout<<"Matrix U is"<<"\n";
for ( i = 1; i<=3; i++)
{
for ( j = 1; j<=3; j++)
{
std::cout<<a[i][j]<<" ";
}
std::cout<<"\n";
}

for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
s[i][j] = a[i][j] ;

for(i=1;i<=3;i++)
for(j=3+1;j<=3*2;j++)
{ if(i+3==j)
{
s[i][j] = 1 ;
}
else
{
s[i][j] = 0;
}
}

std::cout<<"\n";

std::cout<<"Matrix P is"<<"\n";

for ( i = 1; i<=3; i++)


{
for ( j = 1; j<=3; j++)
{
std::cout<<q[i][j]<<" ";
}
std::cout<<"\n";
}
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 3 * 2; j++)
if (j != i)
{
d = s[j][i] / s[i][i];
for (k = 1; k <= 3 * 2; k++)
s[j][k] -= s[i][k] * d;
}
}
/************** reducing to unit matrix *************/
for (i = 1; i <= 3; i++)
{
d = s[i][i];
for (j = 1; j <= 3 * 2; j++)
s[i][j] = s[i][j] / d;
}
std::cout<<"\n";

for (i = 1; i <=3; i++)


{
for (j = 1; j <=3; j++)
{
c[i][j] = 0;
for (k = 1; k <=3; k++)
{
c[i][j] += r[i][k] * s[k][j+3];
}
}
}

for (i = 1; i <=3; i++)


{
for (j = 1; j <=3; j++)
{
w[i][j] = 0;
for (k = 1; k <=3; k++)
{
w[i][j] += q[i][k] * c[k][j];
}
}
}

std::cout<<"Matrix L is"<<"\n";
for (i = 1; i <=3; i++)
{
for (j = 1; j <=3; j++)
{
std::cout<<w[i][j]<<" ";
}
std::cout<<"\n";
}

return (0);
}

You might also like