1.- CÓDIGO FUENTE. Ahora implementamos el método de Doolittle para resolver un sistema de ecuaciones lineales de
n x n n factorizando factorizando
la matriz A como un producto de matrices
es una matriz triangular inferior y
una U una
LU ,
donde
L
matriz triangular superior. El lenguaje de
programación utilizado será Java. Para ello, hemos modificado la clase Matriz.java que implementa el método en cuestión, además de algunos métodos auxiliares para que el usuario ingrese las entradas de la matriz desde el teclado y ésta se imprima con un formato entendible, así como también unos métodos que realizan sustituciones hacia adelante y hacia atrás para obtener la solución. A continuación se muestra el código 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.lengt m.length h; 13 this.columnas = m[0].length ; 14 15 } 16 17 set setFilas (int filas ) { public void 18 this.filas = filas ; 19 } 20 public void 21 set 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 imprimirMatriz() { imprimirMatriz() public void 34 for (int i = 0; i < m.length; i++) { for (int j = 0; j < m[i].length ; j++) { 35 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 llenarMatriz() { llenarMatriz() public void 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 } /** * Implementación del algoritmo de Doolittle. Factorización 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 Lista que contiene las matrices L y U. */ < public ArrayList public ArrayList doolittle doolittle() { int N int N = m.length; //Matriz triangular inferior: double L double L[][] = new double[filas][columnas]; //Matriz triangular superior: double U double U[][] = new double[filas][columnas]; //Inicializamos L con 1's en su diagonal principal int i i=0; i for( for (int
} } U[0][0] = m[0][0];
int j j=1; j for( for (int <=N-1; j++) { <= U[0][j] = m[0][j]; L[j][0] = m[j][0]/U[0][0]; }
2
int i i=1; i for( for (int <=N-2; i++) { <= double sumaAux1 = 0.0; double sumaAux1 int k k=0; k for( for (int <=i-1; k++) { <= sumaAux1 += L[i][k] * U[k][i];
} U[i][i] = m[i][i] - sumaAux1; //----------------------------------------------------------double sumaAux2 = 0.0; double sumaAux2 double sumaAux3 double sumaAux3 = 0.0; int j j=i+1; j for( for (int <=N-1; j++) { <= int k k=0; k for( for (int <=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; double sumaAux4 int k k=0; k for( for (int <=N-2; k++) { <= sumaAux4 += L[N-1][k] * U[k][N-1];
} U[N-1][N-1] = m[N-1][N-1] - sumaAux4; ArrayList matricesFactor = new ArrayList matricesFactor < <>(); <>(); matricesFactor.add(L); matricesFactor.add(U);
return matricesFactor; } //---------------------------------------------------------------------public double[] sustitucionAdelante(double double L L[][], double double b b[]) { int N int N = m.length; double Y double Y[] = new double[N]; Y[0] = b[0]; System.out.println("Y[1]: " + Y[0]); double sumaAux; double sumaAux int i i=1; i for( for (int <=N-1; i++) { <= sumaAux = 0.0; int j j=0; j for( for (int <=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 double U U[][], double double Y Y[]) { int N int N = m.length; double X 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 double sumaAux; int i i=N-2; i>=0; i--) { for( for (int sumaAux = 0.0; int j j=i+1; j for( for (int <=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 void main main(String[] args) { Scanner entrada = new Scanner(System.in); System.out.print("\nIngrese la dimensión de la matriz: "); int dim int dim = entrada.nextInt(); double A 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 double L[][] = matrix.doolittle().get(0); double U 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 b1 double Y1 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 b2 double Y2 double Y2[] = matrix.sustitucionAdelante(L, b2); matrix.sustitucionAtras(U, Y2); } 4
2.- PRUEBA Y EJECUCIÓN. Probamos el algoritmo resolviendo el siguiente sistema de ecuaciones de 4x4:
1 + 1 + 0 + 3 = 8 2 + 1 − 1 + 1 = 7 3 − 1 − 1 + 2 = 14 −1 + 2 + 3 − 1 = −7
Durante la ejecución el programa solicita al usuario ingresar la dimensión del sistema de ecuaciones (4 en este caso); acto seguido, el usuario deberá ingresar mediante el teclado la matriz de coeficientes: 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 factorización buscada es:
1 2 3 −1
1 1 −1 2
0 −1 −1 3
3 1 0 1 = 2 1 2 3 4 −1 − 1 −3
0 0 1 0
0 1 1 0 ∗ 0 −1 0 0 0 1 0 0
0 −1 3 0
3 −5 13 −13
Aplicando las sustituciones hacia delante y hacia atrás tenemos que la solución es:
X = (3, -1, 0, 2)
6