Advertisement
kdaud

Interpolation

Apr 9th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. package Interpolation;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class Tesy {
  7.  
  8.     public static void main(String[] args) {
  9.         Tesy.rty();
  10.     }
  11.  
  12.     public static void rty() {
  13.         double product, sum = 0;
  14.  
  15.         Scanner sc = new Scanner(System.in);
  16.         System.out.print("Enter the Number of Terms: ");
  17.         int n = sc.nextInt();
  18.  
  19.         double[] x = new double[n];
  20.         double[] y = new double[n];
  21.  
  22.         System.out.println("Enter all the x, y terms: ");
  23.         for (int i = 0; i < n; i++) {
  24.             x[i] = sc.nextDouble();
  25.             y[i] = sc.nextDouble();
  26.         }
  27.  
  28.         System.out.println("x = {" + Arrays.toString(x) + "}");
  29.         System.out.println("y = {" + Arrays.toString(y) + "}");
  30.  
  31.         System.out.println("Do you  want to interpolate for x or y."
  32.                 + " If x enter 1, If y enter 2");
  33.         int cc = sc.nextInt();
  34.         if (cc == 1) {
  35.             System.out.println("Enter the value of x");
  36.         } else if (cc == 2) {
  37.             System.out.println("Enter the value of y");
  38.         } else {
  39.             System.out.println("Invalid value entered");
  40.         }
  41.  
  42.  
  43.             int xPoint = sc.nextInt();
  44.             // End of inputs
  45.  
  46.             product = 1;
  47.             // Peforming Arithmatic Operation
  48.             for (int i = 0; i < n; i++) {
  49.                 for (int j = 0; j < n; j++) {
  50.                     if (j != i) {
  51.                         product *= (xPoint - x[j]) / (x[i] - x[j]);
  52.                     }
  53.                 }
  54.                 sum += product * y[i];
  55.  
  56.                 product = 1;    // Must set to 1
  57.             }
  58.             System.out.println("The value at point " + xPoint + " is : " + sum);
  59.  
  60.             // End of the Program
  61.         }
  62.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement