Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.99 KB | None | 0 0
  1. public static void main(String[] args) {
  2.         // TODO code application logic here
  3.         lecturaArchivo();
  4.     }
  5.    
  6.     public static double format(double numero)
  7.     {
  8.         BigDecimal format = new BigDecimal(String.valueOf(numero));
  9.         format = format.setScale(6, RoundingMode.HALF_UP);
  10.         return format.doubleValue();
  11.     }
  12.    
  13.    
  14.    
  15.     public static void mostrarTabla(double fx[][], int cantidadPuntos)
  16.     {
  17.         System.out.println("MATRIZ DE COEFICIENTES\n\n");
  18.         for( int i = 0; i < cantidadPuntos; i++)
  19.         {
  20.             for(int j = 0; j< cantidadPuntos; j++)
  21.             {
  22.                 System.out.print(format(fx[i][j]) + "  ");
  23.             }
  24.             System.out.print("\n\n");
  25.  
  26.         }
  27.             System.out.print("\n");
  28.     }
  29.    
  30.    
  31.     public static void mostrarVector(double x[], int cantidadPuntos)
  32.     {
  33.         System.out.println("LAS SOLUCIONES DE CADA ECUACION SON:\n");
  34.         for (int i = 0; i < cantidadPuntos; i++)
  35.         {
  36.             System.out.print("B" + String.valueOf(i+1) + "=" + format(x[i]) + "  ");
  37.         }
  38.         System.out.print("\n");
  39.     }
  40.    
  41.    
  42.     public static double errorSol(double actual, double anterior)
  43.     {
  44.         double error;
  45.         if(actual != 0.)
  46.         {
  47.             error = Math.abs(actual - anterior) / Math.abs(actual) ;
  48.         }
  49.         else
  50.         {
  51.             error = 0.;
  52.         }
  53.         return error;
  54.     }
  55.    
  56.    
  57.     public static void jacobi(double matriz[][], double vectorAnterior[], double vectorActual[], double be[], int cantidadPuntos)
  58.     {
  59.         double suma;
  60.         for(int i = 0; i < cantidadPuntos; i++)
  61.         {
  62.             suma = 0.;
  63.             for(int j = 0; j < cantidadPuntos; j++)
  64.             {
  65.                 if( j != i)
  66.                 {
  67.                     suma +=  matriz[i][j] * vectorAnterior[j];
  68.                 }
  69.             }
  70.             vectorActual[i] = (be[i] - suma) / matriz[i][i];
  71.         }
  72.     }
  73.    
  74.     public static void mostrarVectorSolucion(double x[], int cantidadPuntos, double error)
  75.     {
  76.         System.out.println("\n\nEL VECTOR SOLUCION CON ERROR < "+ error +" ES:\n");
  77.         for (int i = 0; i < cantidadPuntos; i++)
  78.         {
  79.             System.out.println("X" + String.valueOf(i+1) + "=" + x[i] + "  ");
  80.         }
  81.         System.out.print("\n");
  82.     }
  83.    
  84.    
  85.     public static void lecturaArchivo()
  86.     {
  87.        
  88.        
  89.         double error = 0.01; //el error buscado
  90.         //utilizo vectorAnterior como vector solucion inicial
  91.         double vectorAnterior[] = {0. , 0. , 0. , 0. , 0. , 0.};
  92.         double vectorActual[] = {0. , 0. , 0. , 0. , 0. , 0.};
  93.  
  94.         boolean solucion = false;
  95.        
  96.         //variables para lectura de matriz
  97.         String archivo1 = "C:\\Users\\CristianZar\\Documents\\datos5.txt";
  98.         String linea;
  99.         double[][] matriz;  //matriz que contiene los coeficientes del sistema
  100.         FileReader file;
  101.        
  102.         //variables para lectura de vector
  103.         String archivo2 = "C:\\Users\\CristianZar\\Documents\\datos5a.txt";
  104.         String linea2;
  105.         double [] be;   //vector que contiene los valores de B1, B2, B3,... etc.
  106.         FileReader file2;
  107.        
  108.         int cantidadPuntos = 6;
  109.  
  110.         try
  111.         {
  112.             //Lectura matriz de valores
  113.             file = new FileReader(archivo1);
  114.             BufferedReader buffer = new BufferedReader(file);
  115.             //x = new double[cantidadPuntos];
  116.             matriz = new double[cantidadPuntos][cantidadPuntos];
  117.             int i = 0;
  118.             while((linea = buffer.readLine()) != null)
  119.             {
  120.                 String[] partesLinea = linea.split(" ");
  121.                 for(int j = 0; j < cantidadPuntos; j++)
  122.                 {
  123.                     matriz[i][j] = Double.parseDouble(partesLinea[j]);
  124.                 }
  125.                 i++;
  126.             }    
  127.             buffer.close();
  128.             //Fin lectura matriz de valores
  129.            
  130.            
  131.             //Lectura vector de valores
  132.             file2 = new FileReader(archivo2);
  133.             BufferedReader buffer2 = new BufferedReader(file2);
  134.             be = new double[cantidadPuntos];
  135.             i = 0;
  136.             while((linea2 = buffer2.readLine()) != null)
  137.             {
  138.                 String[] partesLinea = linea2.split(" ");
  139.                 be[i] = Double.parseDouble(partesLinea[0]);
  140.                 i++;
  141.             }
  142.             buffer2.close();
  143.             //Fin lectura vector de valores
  144.            
  145.            
  146.            mostrarTabla(matriz, cantidadPuntos);
  147.            mostrarVector(be, cantidadPuntos);
  148.            
  149.            boolean encontrada = false;
  150.            int k = 0;
  151.            while(solucion == false)
  152.            {
  153.               jacobi(matriz, vectorAnterior, vectorActual, be, cantidadPuntos);
  154.              
  155.               encontrada = true;
  156.               k = 0;
  157.               while( k < cantidadPuntos )
  158.               {
  159.                   if( error >= errorSol(vectorActual[k], vectorAnterior[k]) )
  160.                   {
  161.                       encontrada = encontrada & true;
  162.                   }
  163.                   else      
  164.                   {
  165.                       encontrada = encontrada & false;
  166.                   }
  167.               k++;        
  168.               }
  169.               if(encontrada == true)
  170.               {
  171.                   solucion = true;
  172.               }
  173.               else
  174.               {
  175.                   for( int p = 0; p < cantidadPuntos; p++)
  176.                   {
  177.                       vectorAnterior[p] = vectorActual[p];
  178.                   }
  179.               }
  180.            }
  181.            mostrarVectorSolucion(vectorActual, cantidadPuntos, error);  
  182.            
  183.         }
  184.        
  185.         catch (FileNotFoundException ex)
  186.         {
  187.             System.out.println("Error" + ex.getMessage());
  188.         }
  189.         catch (IOException ex)
  190.         {
  191.             System.out.println("Error" + ex.getMessage());
  192.         }  
  193.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement