Seal_of_approval

Lagrange

Nov 1st, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. public class Lagrange {
  2.  
  3.     public static void main(String[] args) {
  4.         List<Double> x = generate(1, 4, 1);
  5.         List<Double> fx = x.stream()
  6.                 .map(val -> val *val)
  7.                 .collect(Collectors.toList());
  8.  
  9.         List<Double> lagrangeValues = new ArrayList<>();
  10.         for (int i = 0; i < x.size() - 1; i++) {
  11.             double xCur = (x.get(i) + x.get(i + 1)) / 2.0;
  12.             lagrangeValues.add(L(xCur, x, fx));
  13.         }
  14.  
  15.         List<Double> result = merge(fx, lagrangeValues);
  16.  
  17.         printOldTable(x, fx);
  18.         printNewTable(x, result);
  19.     }
  20.  
  21.     public static List<Double> generate(int start, int end, int dx) {
  22.         List<Double> values = new ArrayList<>();
  23.         for (double i = start; i <= end; i += dx) {
  24.             values.add(i);
  25.         }
  26.  
  27.         return values;
  28.     }
  29.     public static void printOldTable(List<Double> x, List<Double> fx) {
  30.         for (double aX : x) {
  31.             System.out.printf("%.3f |\t", aX);
  32.         }
  33.         System.out.println();
  34.         for (double aX : fx) {
  35.             System.out.printf("%.3f |\t", aX);
  36.         }
  37.     }
  38.  
  39.     public static void printNewTable(List<Double> x, List<Double> fx) {
  40.         System.out.println("\n");
  41.         for (int i = 0; i < x.size(); i++) {
  42.             System.out.printf("%.3f |\t", x.get(i));
  43.             if(i + 1 < x.size()) {
  44.                 System.out.printf("%.3f |\t", (x.get(i) + x.get(i + 1)) / 2);
  45.             }
  46.         }
  47.         System.out.println();
  48.         for (double aX : fx) {
  49.             System.out.printf("%.3f |\t", aX);
  50.         }
  51.     }
  52.  
  53.     public static double L(double x, List<Double> input, List<Double> values) {
  54.         double sum = 0;
  55.  
  56.         for (int i = 0; i < input.size(); i++) {
  57.             double num = 1;
  58.             double denom = 1;
  59.  
  60.             for (int j = 0; j < input.size(); j++) {
  61.                 if (j == i) {
  62.                     continue;
  63.                 }
  64.  
  65.                 num *= (x - input.get(j));
  66.                 denom *= (input.get(i) - input.get(j));
  67.             }
  68.  
  69.             sum += (num / denom) * values.get(i);
  70.         }
  71.  
  72.         return sum;
  73.     }
  74.  
  75.     public static List<Double> merge(List<Double> fx, List<Double> lagrangeValues) {
  76.         List<Double> result = new ArrayList<>(fx);
  77.         int index = 1;
  78.         for (double lagrangeValue : lagrangeValues) {
  79.             result.add(index, lagrangeValue);
  80.             index += 2;
  81.         }
  82.  
  83.         return result;
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment