Facebook
From Soiled Parrot, 6 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 282
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. /**
  7.  * Created by Daniel on 2018-03-03.
  8.  */
  9. public class Main {
  10.  
  11.     public static void main(String[] args) {
  12.  
  13.  
  14.         List<Double> x = new ArrayList<Double>();
  15.         List<Double> y = new ArrayList<Double>();
  16.         x.add(1.0);
  17.         x.add(2.0);
  18.         x.add(3.0);
  19.         x.add(4.0);
  20.         y.add(1.0);
  21.         y.add(4.0);
  22.         y.add(9.0);
  23.         y.add(16.0);
  24.  
  25.  
  26.  
  27.  
  28.         double punkt = 15;
  29.         double wartosc = lagrangeInterpolation(x,y,punkt);
  30.         System.out.println("W punkcie "+punkt+" wartość "+ wartosc);
  31.  
  32.     }
  33.  
  34.     public static double lagrangeInterpolation(List xs, List ys, double x ){
  35.         double t;
  36.         double y = 0.0;
  37.  
  38.         for(int k = 0; k< xs.size(); k++){
  39.             t = 1.0;
  40.             for(int j = 0; j < xs.size() ; j++){
  41.                 if(j != k ){
  42.                     double a = (Double) xs.get(j);
  43.                     double b = (Double) xs.get(k);
  44.  
  45.  
  46.                     t=t*((x-a)/(b-a));
  47.                 }
  48.             }
  49.             double c = (Double) ys.get(k);
  50.             y += t*c;
  51.         }
  52.         return y;
  53.     }
  54. }
  55.