Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Interpolation;
- import java.util.Arrays;
- import java.util.Scanner;
- public class Tesy {
- public static void main(String[] args) {
- Tesy.rty();
- }
- public static void rty() {
- double product, sum = 0;
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter the Number of Terms: ");
- int n = sc.nextInt();
- double[] x = new double[n];
- double[] y = new double[n];
- System.out.println("Enter all the x, y terms: ");
- for (int i = 0; i < n; i++) {
- x[i] = sc.nextDouble();
- y[i] = sc.nextDouble();
- }
- System.out.println("x = {" + Arrays.toString(x) + "}");
- System.out.println("y = {" + Arrays.toString(y) + "}");
- System.out.println("Do you want to interpolate for x or y."
- + " If x enter 1, If y enter 2");
- int cc = sc.nextInt();
- if (cc == 1) {
- System.out.println("Enter the value of x");
- } else if (cc == 2) {
- System.out.println("Enter the value of y");
- } else {
- System.out.println("Invalid value entered");
- }
- int xPoint = sc.nextInt();
- // End of inputs
- product = 1;
- // Peforming Arithmatic Operation
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- if (j != i) {
- product *= (xPoint - x[j]) / (x[i] - x[j]);
- }
- }
- sum += product * y[i];
- product = 1; // Must set to 1
- }
- System.out.println("The value at point " + xPoint + " is : " + sum);
- // End of the Program
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement