Anda di halaman 1dari 6

1.- CDIGO FUENTE.

Ahora implementamos el mtodo de Doolittle para resolver un sistema de ecuaciones


lineales de n x n factorizando la matriz A como un producto de matrices LU, donde L
es una matriz triangular inferior y U una matriz triangular superior. El lenguaje de
programacin utilizado ser Java. Para ello, hemos modificado la clase Matriz.java
que implementa el mtodo en cuestin, adems de algunos mtodos auxiliares para que
el usuario ingrese las entradas de la matriz desde el teclado y sta se imprima con un
formato entendible, as como tambin unos mtodos que realizan sustituciones hacia
adelante y hacia atrs para obtener la solucin. A continuacin se muestra el cdigo
fuente:
1
2 import java.util.Scanner;
3
4 class Matriz {
5
6 int filas;
7 int columnas;
8 double m[][];
9
10 public Matriz(double m[][]) {
11 this.m = m;
12 this.filas = m.length;
13 this.columnas = m[0].length;
14
15 }
16
17 public void setFilas(int filas) {
18 this.filas = filas;
19 }
20
21 public void setColumnas(int columnas) {
22 this.columnas = columnas;
23 }
24
25 public int getFilas() {
26 return this.filas;
27 }
28
29 public int getColumnas() {
30 return this.columnas;
31 }
32
33 public void imprimirMatriz() {
34 for (int i = 0; i < m.length; i++) {
35 for (int j = 0; j < m[i].length; j++) {
36
37 if (m[i][j] >= 0) {
38 System.out.printf("\t%s%.2f", " ", m[i][j]);
39 } else {

1
40 System.out.printf("\t%.2f", m[i][j]);
41 }
42
43 }
44 System.out.println("");
45 }
46 System.out.println("");
47 System.out.println("");
48 }
49
50 public void llenarMatriz() {
51 Scanner entrada = new Scanner(System.in);
52 for (int i = 0; i < m.length; i++) {
53 for (int j = 0; j < m[i].length; j++) {
54 System.out.print("Ingrese el elemento [" + (i + 1) +
"," + (j + 1) + "]: ");
55 m[i][j] = entrada.nextInt();
56 }
57 }
58 System.out.println("");
59 }

/**
* Implementacin del algoritmo de Doolittle. Factorizacin LU.
* Factoriza una matriz de n x n como el producto de una matriz
triangular...
* ... inferior L y una matriz triangular superior U.
* @return ArrayList<double[][]> Lista que contiene las matrices L y
U.
*/
public ArrayList<double[][]> doolittle() {

int N = m.length;

//Matriz triangular inferior:


double L[][] = new double[filas][columnas];

//Matriz triangular superior:


double U[][] = new double[filas][columnas];

//Inicializamos L con 1's en su diagonal principal


for(int i=0; i<L.length; i++) {
for (int j=0; j<L[i].length; j++) {
if (i == j)
L[i][j] = 1.0;
}
}

U[0][0] = m[0][0];

for(int j=1; j<=N-1; j++) {


U[0][j] = m[0][j];
L[j][0] = m[j][0]/U[0][0];
}

2
for(int i=1; i<=N-2; i++) {

double sumaAux1 = 0.0;


for(int k=0; k<=i-1; k++) {
sumaAux1 += L[i][k] * U[k][i];
}
U[i][i] = m[i][i] - sumaAux1;

//-----------------------------------------------------------

double sumaAux2 = 0.0;


double sumaAux3 = 0.0;
for(int j=i+1; j<=N-1; j++) {
for(int k=0; k<=i-1; k++) {
sumaAux2 += L[i][k] * U[k][j];
sumaAux3 += L[j][k] * U[k][i];
}

U[i][j] = m[i][j] - sumaAux2;


L[j][i] = (m[j][i] - sumaAux3)/U[i][i];

sumaAux2 = 0.0;
sumaAux3 = 0.0;
}
}

//----------------------------------------------------------------

double sumaAux4 = 0.0;


for(int k=0; k<=N-2; k++) {
sumaAux4 += L[N-1][k] * U[k][N-1];
}
U[N-1][N-1] = m[N-1][N-1] - sumaAux4;

ArrayList<double[][]> matricesFactor = new ArrayList<>();


matricesFactor.add(L);
matricesFactor.add(U);

return matricesFactor;
}

//----------------------------------------------------------------------
public double[] sustitucionAdelante(double L[][], double b[]) {
int N = m.length;
double Y[] = new double[N];
Y[0] = b[0];
System.out.println("Y[1]: " + Y[0]);

double sumaAux;
for(int i=1; i<=N-1; i++) {

sumaAux = 0.0;
for(int j=0; j<=i-1; j++) {
sumaAux += L[i][j] * Y[j];
}

Y[i] = b[i] - sumaAux;

3
System.out.println("Y["+(i+1)+"]: " + Y[i]);
}

System.out.println("");

return Y;
}

//--------------------------------------------------------------------
public double[] sustitucionAtras(double U[][], double Y[]) {
int N = m.length;
double X[] = new double[N];
X[N-1] = Y[N-1] / U[N-1][N-1];
System.out.println("X["+N+"]: " + X[N-1]);
double sumaAux;
for(int i=N-2; i>=0; i--) {
sumaAux = 0.0;

for(int j=i+1; j<=N-1; j++) {


sumaAux += U[i][j]*X[j];
}

X[i] = (Y[i]-sumaAux)/U[i][i];
System.out.println("X["+(i+1)+"]: " + X[i]);
}

return X;
}

public static void main(String[] args) {

Scanner entrada = new Scanner(System.in);


System.out.print("\nIngrese la dimensin de la matriz: ");
int dim = entrada.nextInt();
double A[][] = new double[dim][dim];
Matriz matrix = new Matriz(A);
matrix.llenarMatriz();
System.out.println("\nLa matriz ingresada A es: \n");
matrix.imprimirMatriz();
double L[][] = matrix.doolittle().get(0);
double U[][] = matrix.doolittle().get(1);

System.out.println("\nLa matriz triangular inferior L: \n");


matrix.imprimirMatriz(L);
System.out.println("\nLa matriz triangular superior U: \n");
matrix.imprimirMatriz(U);

double b1[] = {8, 7, 14, -7};


double Y1[] = matrix.sustitucionAdelante(L, b1);
matrix.sustitucionAtras(U, Y1);

System.out.println("\nProbando con otra B...\n");

double b2[] = {4, 1, -3, 4};


double Y2[] = matrix.sustitucionAdelante(L, b2);
matrix.sustitucionAtras(U, Y2); }

4
2.- PRUEBA Y EJECUCIN.
Probamos el algoritmo resolviendo el siguiente sistema de ecuaciones de 4x4:
11 + 12 + 03 + 34 = 8
21 + 12 13 + 14 = 7
31 12 13 + 24 = 14
11 + 22 + 33 14 = 7
{

Durante la ejecucin el programa solicita al usuario ingresar la dimensin del sistema


de ecuaciones (4 en este caso); acto seguido, el usuario deber ingresar mediante el
teclado la matriz de coeficientes:

5
Una vez ingresada la matriz, el programa proceder a ejecutar el algoritmo de Doolittle,
resolviendo el sistema para el vector b = (8, 7, 14, -7):

Observemos que la factorizacin buscada es:


1 1 0 3 1 0 0 0 1 1 0 3
2 1 1 1 2 1 0 0 0 1 1 5
=
3 1 1 2 3 4 1 0 0 0 3 13
1 2 3 1 1 3 0 1 0 0 0 13
Aplicando las sustituciones hacia delante y hacia atrs tenemos que la solucin es:

X = (3, -1, 0, 2)

Anda mungkin juga menyukai