### 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.

For example, we know the coordinates of the points *A (x _{1}, y_{1})* and

*B (x*. What

_{2}, y_{2})*y*will be for any given

*x*?

### Formula

To find the value of *y*, for a given, *x _{1}*,

*y*,

_{1}*x*,

_{2}*y*and

_{2}*x*, we need to apply the

**linear interpolation (extrapolation) method**.

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

_{2}– y

_{1}) / (x

_{2}– x

_{1})

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

_{1}+ m · (x – x

_{1})

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

### 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.

_{1}= 1

y

_{1}= 2

x

_{2}= 5

y

_{2}= 7

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

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

Since *x* is inside the interval *[x _{1}, x_{2}]*, we performed a

**linear interpolation**to find the value of

*y*.

### 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:

Since *x* is outside the interval *[x _{1}, x_{2}]*, we performed a

**linear extrapolation**to find the value of

*y*.

### Calculator

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

x_{1} = | x_{2} = | ||

y_{1} = | y_{2} = | ||

x = | |||

y = | 6 | Interpolation |

### 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.

A function *y(x)* can be approximated on a fixed interval *[x _{1}, x_{N}]* 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(α) |

x_{1} = 0 | y_{1} = 0 |

x_{2} = 20 | y_{2} = 0.3420201 |

x_{3} = 30 | y_{3} = 0.5 |

x_{4} = 40 | y_{4} = 0.6427876 |

x_{5} = 50 | y_{5} = 0.7660444 |

x_{6} = 60 | y_{6} = 0.8660254 |

x_{7} = 70 | y_{7} = 0.9396926 |

x_{8} = 80 | y_{8} = 0.9848078 |

x_{9} = 90 | y_{9} = 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:

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

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 end endfunction

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 points xN_deg = [0 20 30 40 50 60 70 80 90]; xN_rad = xN_deg*%pi/180; yN = sin(xN_rad); // High resolution angle alpha_rad = [0:%pi/1000:%pi/2]; alpha_deg = alpha_rad*180/%pi; sin_alpha = sin(alpha_rad); // Evaluation points x_deg = [5 15 25 35 45 55 65 75 85]; // Interpolation for i=1:length(x_deg) y(i) = lininterp1d(xN_deg,yN,x_deg(i)); end // Plot plot(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 console clc() 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.930538 15 0.256515 0.258819 0.898168 25 0.421010 0.422618 0.381984 35 0.571394 0.573576 0.381984 45 0.704416 0.707107 0.381984 55 0.816035 0.819152 0.381984 65 0.902859 0.906308 0.381984 75 0.962250 0.965926 0.381984 85 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.

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.

x_{N} = | |

y_{N} = | |

x = |

For more tutorials, click on the links below.