Linear interpolation and extrapolation with calculator – x-engineer.org (2024)

Table of Contents

  • Introduction
  • Formula
  • Interpolation example
  • Extrapolation example
  • Calculator
  • Interpolation in embedded systems

Introduction

Linear interpolation is a mathematical method of using the equation of a line in order to find a new data point, based on an existing set of data points.Linear extrapolation is the same as linear interpolation, with the exception of the new data points, which are outside the range of the given (known) data points.

With other words, with linear interpolation and extrapolation, we can find new data points by approximating the current (known) data points as lines.

To apply linear interpolation or extrapolation, we need to know the coordinates of two points. These points will define the equation of a line, which will be used to find any new set of data points along the line.

Image: Linear interpolation

Image: Linear extrapolation

For example, we know the coordinates of the points A (x1, y1) and B (x2, y2). What y will be for any given x ?

Go back

Formula

To find the value of y, for a given, x1, y1, x2, y2 and x, we need to apply the linear interpolation (extrapolation) method.

Step 1. Calculate the slope m of the line, with the equation:

m = (y2 – y1) / (x2 – x1)

(1)

Step 2. Calculate the value of y using the line equation:

y = y1 + m · (x – x1)

(2)

For a better understanding, let’s look at some practical examples.

Go back

Interpolation example

Given the points (1, 2) and (5, 7), calculate the value of y when x = 2.

Step 0. Extract the coordinates of the given data points.

x1 = 1
y1 = 2
x2 = 5
y2 = 7

Step 1. Calculate the slope of the line using equation (1):

m = (7 – 2) / (5 – 1) = 1.25

Step 2. Calculate the value of y using equation (2):

y = 2 + 1.25 · (2 – 1) = 3.25

Since x is inside the interval [x1, x2], we performed a linear interpolation to find the value of y.

Go back

Extrapolation example

Given the same points (1, 2) and (5, 7), calculate the value of y when x = -2.

Step 0 and 1 are the same as in the previous example, which allows us to calculate the value of y as:

y = 2 + 1.25 · (- 2 – 1) = – 1.75

Since x is outside the interval [x1, x2], we performed a linear extrapolation to find the value of y.

Go back

Calculator

You can use the calculator below for linear interpolation and extrapolation, in order to calculate your own data points.

x1= x2=
y1= y2=
x=
y=

6

Interpolation

Go back

Interpolation in embedded systems

One very common application of linear interpolation is in embedded systems. With a given set of data points, we can approximate different mathematical function and use linear interpolation to calculate the output of that function for a given input.

Image: Linear interpolation based on a set of data points

A function y(x) can be approximated on a fixed interval [x1, xN] by taking several sample points. Between these points, the linear interpolation method is applied to estimated the output y of the function for a given input x.

Let’s take as an example the trigonometrical function sin(α). It’s often the case that embedded applications do not have predefined trigonometrical functions but they are still used in internal calculations. One way to overcome this is to sample the trigonometrical function in different points and used linear interpolation to find its value for any given input.

Example. Replace the trigonometrical function sin(α), for α between 0° and 90°, with a set of data points, with an relative error less than 2%.

Step 1. Define the sample data points for the function:

x = α [°] y = sin(α)
x1 = 0 y1 = 0
x2 = 20 y2 = 0.3420201
x3 = 30 y3 = 0.5
x4 = 40 y4 = 0.6427876
x5 = 50 y5 = 0.7660444
x6 = 60 y6 = 0.8660254
x7 = 70 y7 = 0.9396926
x8 = 80 y8 = 0.9848078
x9 = 90 y9 = 1

Step 2. Define the interpolation function, which is going to use the sample data points and for any give angle, between 0 and 90, will return the sinus of the angle.

For this particular example, we are going to use Scilab for the definition of the interpolation function lininterp1d(axis, map, x). The linear interpolation function will have 3 arguments:

  • axis: which is an array containing the xN points
  • map: array containing the yN points
  • x: point in which the function will be evaluated

The output of the function will be y, which is the value of the function in the x point.

function [y]=lininterp1d(axis,map,x) if (x<=axis(1)) then y=map(1); elseif (x >= axis(length(axis))) then y=map(length(axis)); else for i=1:length(axis) if (x==axis(i)) y=map(i); break; elseif ((x > axis(i)) && (x < axis(i + 1))) x1 = axis(i); x2 = axis(i + 1); y1 = map(i); y2 = map(i + 1); slope = (y2 - y1) / (x2 - x1); y = y1 + slope * (x - x1); break; end end endendfunction

The above Scilab instructions will need to be saved in a file called lininterp1d.sci and loaded into the Scilab workspace before calling it.

Step 3. Evaluate the interpolation function for several input values and check the relative error.

Using a Scilab script, we are going to evaluate the interpolation function for the following angles α [°] (x values): 5, 15, 25, 35, 45, 55, 65, 75, 85.

// Sample pointsxN_deg = [0 20 30 40 50 60 70 80 90];xN_rad = xN_deg*%pi/180;yN = sin(xN_rad);// High resolution anglealpha_rad = [0:%pi/1000:%pi/2];alpha_deg = alpha_rad*180/%pi;sin_alpha = sin(alpha_rad);// Evaluation pointsx_deg = [5 15 25 35 45 55 65 75 85];// Interpolationfor i=1:length(x_deg) y(i) = lininterp1d(xN_deg,yN,x_deg(i));end// Plotplot(alpha_deg,sin_alpha)plot(xN_deg,yN,"dr--")plot(x_deg,y,"sb")title("x-engineer.org","Color","blue")xgrid()xlabel("$\alpha \text{ [} ^{\circ} \text{]}$","FontSize",3)ylabel("$\text{sin}(\alpha)$","FontSize",3)legend("actual","sample points","interpolated points",2)// Display in Scilab consoleclc()mprintf("%s \t\t %s \t\t %s \t\t %s \n", "Interpolation", "Interpolated", "Real", "Relative");mprintf("%s \t\t\t %s \t\t %s \t %s \n", "points", "output", "output", "error [%]");for i=1:length(x_deg) sin_x(i) = sin(x_deg(i)*%pi/180); mprintf("%d \t\t\t %.6f \t\t %.6f \t %.6f\n", x_deg(i), y(i), sin_x(i), ((abs(sin_x(i)-y(i)))/y(i))*100);end// Predifined Scilab function for interpolation[y]=interpln([xN_deg;yN],x_deg);disp(y)

After running the Scilab instruction above, we’ll have displayed in the Scilab console:

Interpolation Interpolated Real Relative points output output error [%] 5 0.085505 0.087156 1.93053815 0.256515 0.258819 0.89816825 0.421010 0.422618 0.38198435 0.571394 0.573576 0.38198445 0.704416 0.707107 0.38198455 0.816035 0.819152 0.38198465 0.902859 0.906308 0.38198475 0.962250 0.965926 0.38198485 0.992404 0.996195 0.381984

As you can see, the relative error of the interpolation is less than 2%. The same results are also plotted in the images below.

Image: Linear interpolation based on Scilab dataset

Image: Linear interpolation based on Scilab dataset – zoom in

For linear interpolation, Scilab has also its own predefined function:

[y]=interpln(xyd,x)

To get the same results, we can use the predefined function as:

[y]=interpln([xN_deg;yN],x_deg);

To try out the interpolation function algorithm based on datasets, we can use the online calculator below.

xN =
yN =
x =

For more tutorials, click on the links below.

Go back

Linear interpolation and extrapolation with calculator – x-engineer.org (2024)

FAQs

How to do linear interpolation and extrapolation? ›

To apply linear interpolation or extrapolation, we need to know the coordinates of two points. These points will define the equation of a line, which will be used to find any new set of data points along the line. For example, we know the coordinates of the points A (x1, y1) and B (x2, y2).

What is linear extrapolation estimating? ›

Linear extrapolation means creating a tangent line at the end of the known data and extending it beyond that limit. Linear extrapolation will only provide good results when used to extend the graph of an approximately linear function or not too far beyond the known data.

How to extrapolate linear equations? ›

In the case of linear extrapolation, the data points are plotted on a graph and a linear equation is used to best represent the data. Then to extrapolate for a value close to the existing data, the line is extended and the concerned value of the dependent variable is calculated for the concerned independent variable.

How do you use linear interpolation to estimate? ›

We use linear interpolation to fill in gaps in our data—that is, to estimate values that fall in between the values we already know. To do this, we use a straight line to connect the known data points on either side of the unknown point, and use the equation of that line to estimate the value we are looking for.

What is an example of interpolation and extrapolation? ›

For example, if data was collected in 2004 through 2015, an interpolation prediction would be for the year 2010 since it is between the starting data and the ending date. An extrapolation prediction would be for the year 1999 because this is a year outside of the window of collected data points.

What is linear interpolation formula? ›

Linear interpolation is a method of curve fitting using linear polynomials to construct new data points within the range of a discrete set of known data points. Formula of Linear Interpolation. y = y 1 + ( x − x 1 ) ( y 2 − y 1 ) x 2 − x 1.

What is the formula for extrapolation? ›

It is certainly known for the calculation of linear exploration using two endpoints (x1, y1) and the (x2, y2) in the linear graph when the value of the point extrapolated is “x,” a formula that one can use is represented as y1+ [(x−x1) / (x2−x1)] *(y2−y1).

What are the three types of extrapolation? ›

Methods Of Extrapolation

Extrapolation is mainly classified into three (3) categories which as discussed below: Linear Extrapolation. Polynomial Extrapolation. Conic Extrapolation.

What are the risks of extrapolation? ›

Risks and Dangers

Inaccuracy: Extrapolating can lead to highly inaccurate results. The pattern or relationship between variables evident within the dataset may not apply outside its range. This can result in significant prediction errors.

What is the golden rule for solving linear equations? ›

Golden Rule of Algebra: “Do unto one side of the equal sign as you will do to the other…” **Whatever you do on one side of the equal sign, you MUST do the same exact thing on the other side. If you multiply by -2 on the left side, you have to multiply by -2 on the other.

What is the rule of extrapolation? ›

Extrapolation Formula

Let us consider the two endpoints in a linear graph (x1, y1) and (x2, y2) where the value of the point “x” is to be extrapolated, and then the extrapolation formula is given as. y ( x ) = y 1 + x − x 1 x 2 − x 1 ( y 2 − y 1 )

How to calculate interpolation manually? ›

The linear interpolation formula, or interpolation equation, appears as follows: y − y 1 = y 2 − y 1 x 2 − x 1 ( x − x 1 ) , where ( x 1 , y 1 ) and ( x 2 , y 2 ) are two known data points and ("x," "y") represents the data point to be estimated. The image below illustrates linear interpolation.

What is linear interpolation calculator? ›

Here, we present a linear interpolation calculator, which calculates the X or Y value of a point on a line defined by two other points. The formula used is: This formula determines the interpolated Y coordinate of a point given its X coordinate and the coordinates of 2 other points.

Which method is best for interpolation? ›

In terms of the ability to fit your data and produce a smooth surface, the Multiquadric method is considered by many to be the best. All of the Radial Basis Function methods are exact interpolators, so they attempt to honor your data.

What is the linear interpolation between A and B? ›

To state that as a generic algebra equation, the value v at fraction f of the way between points a and b is calculated as v=f(b-a)+a. This is known as linear interpolation between two numbers. It's very useful for calculating intermediate values between two known values.

How to do linear interpolation between two numbers? ›

Use the linear interpolation equation

The x represents the independent value you want to find the output for, while y represents the output. Based on the example, here's the formula with the values: y = 45 + (70-45) x [(1,500-1,000) / (2,000-1,000)].

Top Articles
Latest Posts
Article information

Author: Aron Pacocha

Last Updated:

Views: 5521

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Aron Pacocha

Birthday: 1999-08-12

Address: 3808 Moen Corner, Gorczanyport, FL 67364-2074

Phone: +393457723392

Job: Retail Consultant

Hobby: Jewelry making, Cooking, Gaming, Reading, Juggling, Cabaret, Origami

Introduction: My name is Aron Pacocha, I am a happy, tasty, innocent, proud, talented, courageous, magnificent person who loves writing and wants to share my knowledge and understanding with you.