SHARE
TWEET

Untitled

a guest Jun 18th, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class MexicanWave {
  2.  
  3.     public static void main(String[] args) {
  4. // ( jak nie trudno się domyślić funkcja to x^2)
  5.         double[] x = {-2,1,2,4 };
  6.         double[]y = {3, 1, -3, 8};
  7.  
  8. //chcemy znaleźć wartość w x = 2
  9.         double y2 = lagrangeInterpolation(x,y,0);
  10.         System.out.println("x = 2 -> y = "+y2);
  11.  
  12.     }
  13.  
  14.     public static double lagrangeInterpolation(double[] xs, double[] ys, double x ){
  15.         double t;
  16.         double y = 0.0;
  17.  
  18.         for(int k = 0; k< xs.length; k++){
  19.             StringBuilder builder = new StringBuilder();
  20.             StringBuilder builder1 = new StringBuilder();
  21.             builder.append(k+1).append(": ");
  22.             t = 1.0;
  23.             for(int j = 0; j < xs.length ; j++){
  24.                 if(j != k ){
  25.                     builder.append(String.format("(x-%.0f)/(%.0f-%.0f)*", xs[j], xs[k], xs[j]));
  26.                     builder1.append(String.format("(x-x%d)/(x%d-x%d)*", (j+1), (k+1), (j+1)));
  27.                     t = t * ((x-xs[j])/(xs[k]-xs[j]));
  28.                 }
  29.             }
  30.             System.out.println(builder.toString());
  31.             System.out.println(builder1.toString());
  32.             System.out.println();
  33.             y += t*ys[k];
  34.         }
  35.         return y;
  36.     }
  37.  
  38.  
  39. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top