Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- // TODO code application logic here
- lecturaArchivo();
- }
- public static double format(double numero)
- {
- BigDecimal format = new BigDecimal(String.valueOf(numero));
- format = format.setScale(6, RoundingMode.HALF_UP);
- return format.doubleValue();
- }
- public static void mostrarTabla(double fx[][], int cantidadPuntos)
- {
- System.out.println("MATRIZ DE COEFICIENTES\n\n");
- for( int i = 0; i < cantidadPuntos; i++)
- {
- for(int j = 0; j< cantidadPuntos; j++)
- {
- System.out.print(format(fx[i][j]) + " ");
- }
- System.out.print("\n\n");
- }
- System.out.print("\n");
- }
- public static void mostrarVector(double x[], int cantidadPuntos)
- {
- System.out.println("LAS SOLUCIONES DE CADA ECUACION SON:\n");
- for (int i = 0; i < cantidadPuntos; i++)
- {
- System.out.print("B" + String.valueOf(i+1) + "=" + format(x[i]) + " ");
- }
- System.out.print("\n");
- }
- public static double errorSol(double actual, double anterior)
- {
- double error;
- if(actual != 0.)
- {
- error = Math.abs(actual - anterior) / Math.abs(actual) ;
- }
- else
- {
- error = 0.;
- }
- return error;
- }
- public static void jacobi(double matriz[][], double vectorAnterior[], double vectorActual[], double be[], int cantidadPuntos)
- {
- double suma;
- for(int i = 0; i < cantidadPuntos; i++)
- {
- suma = 0.;
- for(int j = 0; j < cantidadPuntos; j++)
- {
- if( j != i)
- {
- suma += matriz[i][j] * vectorAnterior[j];
- }
- }
- vectorActual[i] = (be[i] - suma) / matriz[i][i];
- }
- }
- public static void mostrarVectorSolucion(double x[], int cantidadPuntos, double error)
- {
- System.out.println("\n\nEL VECTOR SOLUCION CON ERROR < "+ error +" ES:\n");
- for (int i = 0; i < cantidadPuntos; i++)
- {
- System.out.println("X" + String.valueOf(i+1) + "=" + x[i] + " ");
- }
- System.out.print("\n");
- }
- public static void lecturaArchivo()
- {
- double error = 0.01; //el error buscado
- //utilizo vectorAnterior como vector solucion inicial
- double vectorAnterior[] = {0. , 0. , 0. , 0. , 0. , 0.};
- double vectorActual[] = {0. , 0. , 0. , 0. , 0. , 0.};
- boolean solucion = false;
- //variables para lectura de matriz
- String archivo1 = "C:\\Users\\CristianZar\\Documents\\datos5.txt";
- String linea;
- double[][] matriz; //matriz que contiene los coeficientes del sistema
- FileReader file;
- //variables para lectura de vector
- String archivo2 = "C:\\Users\\CristianZar\\Documents\\datos5a.txt";
- String linea2;
- double [] be; //vector que contiene los valores de B1, B2, B3,... etc.
- FileReader file2;
- int cantidadPuntos = 6;
- try
- {
- //Lectura matriz de valores
- file = new FileReader(archivo1);
- BufferedReader buffer = new BufferedReader(file);
- //x = new double[cantidadPuntos];
- matriz = new double[cantidadPuntos][cantidadPuntos];
- int i = 0;
- while((linea = buffer.readLine()) != null)
- {
- String[] partesLinea = linea.split(" ");
- for(int j = 0; j < cantidadPuntos; j++)
- {
- matriz[i][j] = Double.parseDouble(partesLinea[j]);
- }
- i++;
- }
- buffer.close();
- //Fin lectura matriz de valores
- //Lectura vector de valores
- file2 = new FileReader(archivo2);
- BufferedReader buffer2 = new BufferedReader(file2);
- be = new double[cantidadPuntos];
- i = 0;
- while((linea2 = buffer2.readLine()) != null)
- {
- String[] partesLinea = linea2.split(" ");
- be[i] = Double.parseDouble(partesLinea[0]);
- i++;
- }
- buffer2.close();
- //Fin lectura vector de valores
- mostrarTabla(matriz, cantidadPuntos);
- mostrarVector(be, cantidadPuntos);
- boolean encontrada = false;
- int k = 0;
- while(solucion == false)
- {
- jacobi(matriz, vectorAnterior, vectorActual, be, cantidadPuntos);
- encontrada = true;
- k = 0;
- while( k < cantidadPuntos )
- {
- if( error >= errorSol(vectorActual[k], vectorAnterior[k]) )
- {
- encontrada = encontrada & true;
- }
- else
- {
- encontrada = encontrada & false;
- }
- k++;
- }
- if(encontrada == true)
- {
- solucion = true;
- }
- else
- {
- for( int p = 0; p < cantidadPuntos; p++)
- {
- vectorAnterior[p] = vectorActual[p];
- }
- }
- }
- mostrarVectorSolucion(vectorActual, cantidadPuntos, error);
- }
- catch (FileNotFoundException ex)
- {
- System.out.println("Error" + ex.getMessage());
- }
- catch (IOException ex)
- {
- System.out.println("Error" + ex.getMessage());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement