﻿ Linear Interpolation
HOME Mathematics Computer Science Economics
Linear Interpolation
Prerequisites
Show All
Hide All
Show/Hide
﻿
Introduction
• A gradient describes how much y changes relative to a change in x.
• For example, we can define a line as starting at (0, 0) and ending at (10, 25):
• Instead of giving explicit start and end points for the line we can describe it by how much y changes relative to x.
• In the line above, a change of 10 in x produces a change of 25 in y.
• The gradient is defined as the change in y divided by the change in x.
• If we use Δ to denote 'change' then the formula for the gradient (g) of a line is:

LaTeX: g = \frac{\Delta y}{\Delta x}
• In the line above, the gradient is:
Finding the change in x and y
• In our above example, our first coordinate was (9, 0). This made it easy to calculate how much x and y changed.
• However when both coordinates are non-zero, it gets a bit more complicated.
• For example, calculate the gradient of the line between (1, 2) and (7, 10)
1. To find how much x and y change, we need to subtract the first coordinate from the second
• We can use this in our equation to find the gradient of a line:
Special Cases
• There are two special cases to look out for when calculating the gradient of a line:
1. When the line is horizontal
• There will be no change in y
2. When the line is vertical
• There will be no change in x
Code
import math

deltaY = p2[1] = p1[1]
deltaX = p2[0] - p1[0]

if deltaX == 0:
return math.inf
else:
return deltaY / deltaX


Interpolation
Show/Hide
﻿
Introduction
• If we are given a set of data points:  X 1 2 3 4 5 6 7 y 28 21 96 96 120 87 50
• We know the value of y when x is a whole number, i.e. 1, 2, 3
• But when x is 1.5 we do not have a corresponding value for y.
• Interpolation is the method of estimating the value of y for a given x when we don't have a data point for it.

Linear Interpolation
﻿
Introduction
• Linear interpolation is a very simple type of interpolation.
• To find the value of a point which is not in the given set of points:
1. Find the two points that surround the point you are estimating: one will be smaller, one larger.
2. Draw a straight line between the two known points.
3. Find the value of y for the point that lies on the line at the given x.
Visualization
Example
• If we are given a set of data points:  X 1 2 3 4 5 6 7 y 28 21 96 96 120 87 50
Find the value for y when x is 2.4
1. The points the surround 2.4 are (2, 21) and (3, 96)
2. We calculate the gradient of this line:
3. And then find the value of y when x is 2.4:
4. When x is 2.4, y is 51
Code (using SciPy)
from scipy import interpolate

dataPoints = [
[1, 28],
[2, 21],
[3, 96],
[4, 96],
[5, 120],
[6, 87],
[7, 50]
]

interpolator = interpolate.interp1d(
[dataPoint[0] for dataPoint in dataPoints],
[dataPoint[1] for dataPoint in dataPoints],
'linear')

print(interpolator(2.4)) # prints 50.99999999999999
print(interpolator(2.6)) # prints 66.0

Code (using NumPy)
import numpy as np

class LinearInterpolator:
# an array of x values, in order
_sortedDomain = []

# a dictionary of x, y values
_dataPointsDict = {}

def __init__(self, dataPoints):
for dataPoint in dataPoints:
self._dataPointsDict[dataPoint[0]] = dataPoint[1]

self._sortedDomain = [dataPoint[0] for dataPoint in dataPoints]
self._sortedDomain = np.sort(self._sortedDomain)

def interpolate(self, x):
# find the index of the smallest element larger than x
closest = np.searchsorted(self._sortedDomain, x)

# find the two x values (before and after x
x1 = self._sortedDomain[closest - 1]
x2 = self._sortedDomain[closest]

# and their corresponding y values
y1 = self._dataPointsDict[x1]
y2 = self._dataPointsDict[x2]

# find the deltaX and deltaY
deltaX = x2 - x1
deltaY = y2 - y1

# and finally interpolate
xPart = x - x1

return y1 + yPart

dataPoints = [
[1, 28],
[2, 21],
[3, 96],
[4, 96],
[5, 120],
[6, 87],
[7, 50]
]

interpolator = LinearInterpolator(dataPoints)

print(interpolator.interpolate(2.4)) # 50.99999999999999
print(interpolator.interpolate(2.6)) # 66.0