Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. /** Interpolacja Lagrange'a
  2. * Michał Ładanowski (c) 2011
  3. * www.algorytm.org
  4. */
  5. package com.company.zpo;
  6. public class Main {
  7.  
  8. public static void main(String[] args) {
  9. // węzły interpolacji oraz odpowiadające im wartości
  10. // ( jak nie trudno się domyślić funkcja to x^2)
  11. double[] x = {1,2,3,4};
  12. double[]y = {9,45,141,327};
  13.  
  14. //chcemy znaleźć wartość w x = 2
  15. double y2 = lagrangeInterpolation(x,y,5);
  16. System.out.println("x = 2 -> y = "+y2);
  17.  
  18. }
  19.  
  20. public static double lagrangeInterpolation(double[] xs, double[] ys, double x ){
  21. double t;
  22. double y = 0.0;
  23.  
  24. for(int k = 0; k< xs.length; k++){
  25. t = 1.0;
  26. for(int j = 0; j < xs.length ; j++){
  27. if(j != k ){
  28. t=t*((x-xs[j])/(xs[k]-xs[j]));
  29. }
  30. }
  31. y += t*ys[k];
  32. }
  33. return y;
  34. }
  35.  
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement