Polynomial Interpolation and Extrapolation

In today’s lesson, we’re going to cover the following topics:

1. Polynomial Interpolation

2. Polynomial Extrapolation

video coming soon

Polynomial Interpolation

Interpolation is when we are trying to find values of a function that are not specifically known, but they are between known data points.

For example, if I had a linear graph and I knew that when x = 1, y = 1 and when x = 3, y = 3, then when x = 2, y = ? This is where you would interpolate to find the value of y when x = 2, because it was not known.

I won’t get into how to manually interpolate in mathematics, but let’s focus on how we can use MATLAB to do the work for us.

If we have a set of data points (x,y), but you want to find a point that is not specified in those data points that still falls within those values, we would interpolate. For example, say I am given (1,4) (2,8) (3,12) (4,16), when x = 1, 2, 3, or 4. Now if x = 2.5, then what does y equal? That is “in between” the data points. We can use command interp1 to interpolate this value in MATLAB. This command only works with linear coordinates.

In your class, you will usually be given coordinates in one of 3 ways:

(1) Graph: If you are given a graph and you are picking data points off of it to get MATLAB to interpolate, you can tell whether you have a straight line or not. If it is a straight line, you have a linear function and can use interp1.

(2) Equation: If you are given an equation, you don’t need to even use interp1. You can, but don’t need to. If you are using interp1 with this, then you need to make sure the equation is linear (eg. it can be put into y = mx + b form). Instead, you will enter the equation given, then find the y-value for an input value of x. Like this equation below, where I have y = 2*x, and I want to find y when x = 5.

When I run the code, it gives me y = 10 when x = 5.

(3) Data Points: If you are given data points, you wouldn’t know whether the equation is linear or not. You can directly tell whether the y-values and x-values are straight multiples of one another first. For example, if you have (1,3) (2,6) (3,9) as your data points. This means x = 1 and y = 3, then x = 2 and y = 6, then x = 3 and y = 9. Notice how the y-value is always 3 times that of the x-value. This makes it linear.

If you still aren’t sure, I would create the equation of the line. Using two of the (x,y) coordinates, set up a system of equations to find the b-value and m-value in y = mx + b (I won’t go through the math of finding this linear equation though, because it is covered in the 9th grade math course). If you are able to create a linear equation that satisfies all the data points, then you can use interp1.

Example

Say we are given a set of (x,y) data points. These are (1,4) (2,8) (3,12) (4,16). Looking at these coordinates, I can tell that the y-value is always 4 times that of the x-value, so it is linear (eg. when x = 1, y = 4. Then when x = 2, y = 8, and so on).

Now let’s use the interp1 command. It will interpolate to find you the new data point you want to find, which will be (xi, yi). I have set the interp1 command equal to the variable yi on the left side of the equation, because the command is working with the xi value to give a new yi value.

Within the interp1 brackets, we will have 3 parts, which are highlighted below. The first part will be the x and y that you defined above (or whatever you called your variables – doesn’t have to be x and y). The second part will be xi, which is the x-value that you want to find the y-value at. You will replace this with an actual number. The last part is the interpolation method to be used. For interpolating linear functions, we use the word linear in single quotes.

Now I can’t leave xi as it is in the brackets, I need to replace it with a number. I want to interpolate this at a value of xi= 3.42. I will replace this here:

Running this code gives the following. The value of yi = 13.6800. This means that when x = 3.42, y = 13.6800.

Now to do interpolation with a quadratic function, there is no specific interpolation method in MATLAB. But we can use a method which is usually referred to as the general polynomial interpolation method – this is the method spline.

Here’s an example of quadratic data points: (1,1) (2,4) (3,9) (4,16). Now let’s put these into MATLAB:

Now we will use interp1 with the interpolation method spline rather than linear.

I want to find the value of yi when xi = 1.2, for example. So I will set xi = 1.2.

Running the code gives this. When x = 1.2, y = 1.44. This is correct because the data points I gave above were for the function y = x^2, and substituting x = 1.2 into the equation does give y = 1.44.

We can also do interpolation with a cubic function.

Here’s an example of data points: (1,1) (2,8) (3,27) (4,64) (5,125). I know these points are cubic, because every y-value is just every x-value but cubed (y = x^3 would be the function). If there were transformations done to the function, where there are additions, subtractions, or other movements of the data points, it’s not as easy to pick up on the fact that it is cubic that quickly. You will likely be told that it’s cubic, or you’ll have to make an educated guess based on graphing the data points.

Here we will use spline rather than interp1. In the quadratic function, we used interp1 as the command and spline as the interpolation method. Here, we will use spline as the command, and no interpolation method within the brackets. This is how it will look:

Now I will replace the value of xi with 1.4:

This is what the output gives. y = 2.7440 when x = 1.4.

Polynomial Extrapolation

Extrapolation is finding values of a function that are not specified in the graph/data points, but the values are beyond the endpoints. For example, if I have a linear graph that ends at x = 10, y = 10, then I could extrapolate to find the y value when x = 11.

How does MATLAB do this for us?

One way is to use interp1 again, but this time with extrapolation. The syntax will require x and y datasets, or an x dataset and a function y. Then you will need to use the command interp1(x-dataset, y-dataset, value to extrapolate at, ‘method’, ‘extrap’).

Example 1

Let’s extrapolate a linear function. Here’s an example of two datasets (x and y) in the image below. The equation that relates these values are y = 2 * x. The data points are not always related with an equation this easy, so you may not even have the option to use an equation for your y dataset. If you do have an equation that relates the x and y variables though, you can either define y using the function (as I have written in the comment in green) or you can define the data points directly like I have:

Let’s discuss the inputs to the interp1 command. There are 5 inputs – the x dataset, y dataset, extrapolation x-value, method, ‘extrap’).

For the x dataset and y dataset, you will replace these with the variables of your datasets – mine were x and y. Replace the extrapolation x-value with the x-value you want to find the y-value at (the x-value you want to extrapolate). For method, replace the method of extrapolation – spline works with most polynomials, but with linear functions such as this one you can also use linear as I have. Make sure this is in single quotes. Lastly, leave ‘extrap’ as it is at the end of the brackets.

Running the code gives 12 as an output. This makes sense because I extrapolated the value at x = 6, and because each y-value is twice that of each x-value, if x = 6, then y = 12.

Example 2

Let’s try extrapolating a quadratic function. Here is an x dataset with an equation, y = x^2. You could also list the values of y in a dataset like I did in Example 1, but as I mentioned above, if you know the equation of your dataset then you can use that instead, so this is what I am doing below. Everything is the same in the interp1 command, since I am still extrapolating at x = 6. The only thing I have changed is the extrapolation method – it is now spline rather than linear. Spline covers other polynomial extrapolation methods.

This gives the output:

Example 3

Let’s try extrapolation on a cubic function. I have changed the function to y = x^3, and kept everything else the same, including the method spline for polynomial extrapolation.

This gives the output: