Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. public class TP4LAGRANGE {
  2.  
  3.     /**
  4.      * @param args the command line arguments
  5.      */
  6.     public static void main(String[] args) {
  7.         lecturaArchivo();
  8.     }
  9.    
  10.    
  11.     public static double Lagrange(int X[], int F[], double x, int cantidadPuntos)
  12.     {
  13.         double suma = 0;
  14.         double lagrange;
  15.         for (int i = 0; i < cantidadPuntos; i++)
  16.         {
  17.             lagrange = 1;
  18.             for (int j = 0; j < cantidadPuntos; j++)
  19.             {
  20.                 if (j != i)
  21.                 {
  22.                     lagrange *= (x - X[j]) / (X[i] - X[j]);
  23.                 }  
  24.             }
  25.             suma += lagrange * F[i];
  26.         }
  27.         return suma;
  28.     }
  29.    
  30.     public static void lecturaArchivo()
  31.     {
  32.         String archivo = "C:\\Users\\CristianZar\\Documents\\datos.txt";
  33.         String linea;
  34.         int[] x;
  35.         int[] fx;
  36.         int cantidadPuntos = 10;
  37.         FileReader file;
  38.         try
  39.         {
  40.             file = new FileReader(archivo);
  41.             BufferedReader buffer = new BufferedReader(file);
  42.             x = new int[cantidadPuntos];
  43.             fx = new int[cantidadPuntos];
  44.             int i = 0;
  45.             while((linea = buffer.readLine()) != null)
  46.             {
  47.                 String[] partesLinea = linea.split(" ");
  48.                 x[i] = (int) Double.parseDouble(partesLinea[0]);
  49.                 fx[i] = (int) Double.parseDouble(partesLinea[1]);
  50.                 i++;
  51.             }
  52.             buffer.close();
  53.             System.out.println("AƑO    POBLACION");
  54.             for (int j=0; j < x.length; j++)
  55.             {
  56.                 System.out.println(x[j] + "   " + fx[j]);
  57.             }
  58.            
  59.             Scanner input = new Scanner(System.in);
  60.             System.out.print("\nIngrese el aƱo a interpolar: ");
  61.             double valorBuscado = Double.parseDouble(input.next());
  62.             System.out.println("\nEl valor aproximado de F(" + Double.valueOf(valorBuscado).intValue() +")= " + Double.valueOf(Lagrange(x, fx, valorBuscado, cantidadPuntos)).intValue());
  63.            
  64.         }
  65.         catch (FileNotFoundException ex)
  66.         {
  67.             System.out.println("Error" + ex.getMessage());
  68.         }
  69.         catch (IOException ex)
  70.         {
  71.             System.out.println("Error" + ex.getMessage());
  72.         }  
  73.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement