0% found this document useful (0 votes)
33 views4 pages

Descomposicion LU

The document describes solving systems of linear equations using LU decomposition. It defines a class to represent the system of equations and perform LU decomposition. Methods are included to read the coefficients, perform the LU decomposition, and calculate and check the solution.

Uploaded by

D4rk G0rE
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)
33 views4 pages

Descomposicion LU

The document describes solving systems of linear equations using LU decomposition. It defines a class to represent the system of equations and perform LU decomposition. Methods are included to read the coefficients, perform the LU decomposition, and calculate and check the solution.

Uploaded by

D4rk G0rE
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/ 4

#include <iostream>

#include<iomanip>

using namespace std;

class SEL{
protected:
int n;
double **mat,**matU;
double x0,x1,x2,d0,d1,d2;
double **matriz;
double **matriz01;
public:
SEL(){
x0=0,x1=0,x2=0,d0=0,d1=0,d2=0;
cout<<"\n\tIngrese el numero de ecuaciones: ";
cin>>n;

matriz= new double*[n];


for(int i =0; i<n;i++){
matriz[i]= new double [n+1];
}
matriz01= new double*[n];
for(int i =0; i<n;i++){
matriz01[i]= new double [n+1];
}
mat= new double*[n];
for(int i =0; i<n;i++){
mat[i]= new double [n+1];
}
matU= new double*[n];
for(int i =0; i<n;i++){
matU[i]= new double [n+1];
}

}
void leermat(){
char opc='\0';
do{
cout<<"\n Lectura de matriz aumentada: "<<endl;
for(int i=0;i<n;i++){
for(int j=0;j<n+1;j++){
if(j==3){
cout<<"\t b["<<i+1<<"]: ";
}
else{
cout<<"\t a["<<i+1<<"]["<<j+1<<"]: ";
}
cin>>matriz[i][j];
}
}
cout<<endl<<endl;
for(int i =0; i<n; i++){
cout<<"\t";
for( int j=0; j<n;j++){
cout<<" "<<matriz[i][j]<<"(X"<<j<<")";
if( matriz[i][j+1]<0) {
cout<<" ";
}
else
if(j<2)cout<<" +";
}
cout<<" = "<<matriz[i][n]<<endl;
}
cout<<"\n\tEl sistema de ecuaciones lineales es correcto?
(S/N): ";
cin>>opc;

}while(opc=='n' or opc=='N');

for(int i=0;i<n;i++){
for(int j =0; j<=n;j++){
matriz01[i][j]=matriz[i][j];
}
}
}
};

class lu: public SEL{


private:
double *inc;
double *r;
double aux;

public:
lu(){
inc= new double [n];
r= new double [n];
for(int i=0;i<n; i++){
inc[i]=0;
r[i]=0;
}
aux=0;
}
void metodolu(){
mat[1][0]=matriz[1][0]/matriz[0][0];
mat[2][0]=matriz[2][0]/matriz[0][0];
for(int j=1;j<n;j++){
matriz[1][j]= matriz[1][j]-(((matriz[0][j])/(matriz[0]
[0]))*matriz[1][0]);
matriz[2][j]= matriz[2][j]-(((matriz[0][j])/(matriz[0]
[0]))*matriz[2][0]);
}
mat[2][1]=(matriz[2][1]/matriz[1][1]);
matriz[1][0]=0;matriz[2][0]=0;
cout<<"MATRIZ U\n";
matriz[2][2]= matriz[2][2]-(((matriz[1][2])/(matriz[1]
[1]))*matriz[2][1]);
matriz[2][1]=0;
for (int i=0;i<n;i++){
for (int j =0;j<n;j++){
matU[i][j]=matriz[i][j];
cout<<setprecision(3)<<matriz[i][j]<<"\t";
}
cout<<endl;
}
cout<<"\n\nMATRIZ L\n";
// cascada
matriz[1][2]=matriz[0][1]=matriz[0][2]=0;
for(int i=0;i<n;i++){
matriz[i][i]=1;
matriz[i][0]=mat[i][0];
}
matriz[2][1]=mat[2][1];matriz[0][0]=1;
for (int i=0;i<n;i++){
for (int j =0;j<n;j++){
cout<<matriz[i][j]<<"\t";
}
cout<<endl;
}
d0=matriz[0][3];
d1=matriz[1][3]-matriz[1][0]*d0;
d2=matriz[2][3]-matriz[2][0]*d0-matriz[2][1]*d1;
cout<<"\n\nd[2] = "<<d2<<endl;
cout<<"d[1] = "<<d1<<endl;
cout<<"d[0] = "<<d0<<endl;
x2=d2/matU[2][2];
x1=(d1-matU[1][2]*x2)/matU[1][1];
x0=(d0-matU[0][2]*x2-matU[0][1]*x1)/matU[0][0];
cout<<"\n\nx[2] = "<<x2<<endl;
cout<<"x[1] = "<<x1<<endl;
cout<<"x[0] = "<<x0<<endl;
}
void comprobacion(){
cout<<"\nComprobando resultados...\n";

inc[0]=x0;
inc[1]=x1;
inc[2]=x2;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
aux += matriz01[i][j]* inc[j];
}
r[i]=aux;
aux=0;
}
for(int i =0; i<n; i++){
for( int j=0; j<n;j++){
cout<<matriz01[i][j]<<"(X"<<j<<")";
if( matriz01[i][j+1]<0) {
cout<<" ";
}
else
if(j<2)cout<<" +";
}
cout<<" = "<<r[i]<<endl;
}

}
};

int main(){

lu obj;
obj.leermat();
obj.metodolu();
obj.comprobacion();

system("pause");
return 0;

You might also like