Java Program to implement Linear Extrapolation

In this tutorial, we will discuss the logic behind computing the linear extrapolation in Java. Also, we will implement a Java problem that will compute the extrapolation result based on the formula.

What is Linear Extrapolation?

Extrapolation can be described as the mechanism in maths where the expected value is estimated more than the desire range of values. Linear extrapolation is also used to estimate the result of some readings below or above than the given range. In extrapolation, the results are predicted by analyzing the input values and its corresponding relationship. The strategy of linear interpolation is to use a straight line to connect the given data points. Linear interpolation is moreover not accurate for non-linear data. If the points in the data set change by a wide amount, linear interpolation may not give a good estimation.

Extrapolation Formula:

y(x) = y(x) + ((x-x1)/(x2-x1)) * (y2-y1)

Following is the java code Linear Extrapolation:

public class extrapolation
{ 
    static double extra_polate(double[][] d, double x) 
    { 
      double y = d[0][1] + (x - d[0][0]) / (d[1][0] - d[0][0]) *  (d[1][1] - d[0][1]); 

  return y; 
    } 


public static void main (String[] args) 
{ 
    double[][] d = {{ 1.2, 2.7 },{ 1.4, 3.1 }}; 
    double x = 2.7; 
    System.out.println("Value of y at x = 2.7 : " + extra_polate(d, x)); 
} 
} 

 

Output:

Value of y at x = 2.7 : 5.700000000000001


Explanation:  In the above java program the result of linear extrapolation is calculated using the formula that is stated above in this tutorial. In the main function, I have stated the sample dataset d in the form of 2d matrix, then further the value of x is specified as 2.7 through which the value of y will be calculated. Then the function extra_polate() is called to which the sample dataset and value of x is passed. In the extra_polate() function the formula is declared corresponding to the variable y, hence the extrapolation result is obtained and returned back to main function where it gets displayed.

 

Leave a Reply

Your email address will not be published. Required fields are marked *