Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TP4LAGRANGE {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- lecturaArchivo();
- }
- public static double Lagrange(int X[], int F[], double x, int cantidadPuntos)
- {
- double suma = 0;
- double lagrange;
- for (int i = 0; i < cantidadPuntos; i++)
- {
- lagrange = 1;
- for (int j = 0; j < cantidadPuntos; j++)
- {
- if (j != i)
- {
- lagrange *= (x - X[j]) / (X[i] - X[j]);
- }
- }
- suma += lagrange * F[i];
- }
- return suma;
- }
- public static void lecturaArchivo()
- {
- String archivo = "C:\\Users\\CristianZar\\Documents\\datos.txt";
- String linea;
- int[] x;
- int[] fx;
- int cantidadPuntos = 10;
- FileReader file;
- try
- {
- file = new FileReader(archivo);
- BufferedReader buffer = new BufferedReader(file);
- x = new int[cantidadPuntos];
- fx = new int[cantidadPuntos];
- int i = 0;
- while((linea = buffer.readLine()) != null)
- {
- String[] partesLinea = linea.split(" ");
- x[i] = (int) Double.parseDouble(partesLinea[0]);
- fx[i] = (int) Double.parseDouble(partesLinea[1]);
- i++;
- }
- buffer.close();
- System.out.println("AĆO POBLACION");
- for (int j=0; j < x.length; j++)
- {
- System.out.println(x[j] + " " + fx[j]);
- }
- Scanner input = new Scanner(System.in);
- System.out.print("\nIngrese el aƱo a interpolar: ");
- double valorBuscado = Double.parseDouble(input.next());
- System.out.println("\nEl valor aproximado de F(" + Double.valueOf(valorBuscado).intValue() +")= " + Double.valueOf(Lagrange(x, fx, valorBuscado, cantidadPuntos)).intValue());
- }
- 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