Interpolating data points in Excel

↘锁芯ラ 提交于 2019-12-02 17:36:06
Deniss

The easiest way to do it probably is as follows:

  1. Download Excel add-on here: XlXtrFun™ Extra Functions for Microsoft Excel

  2. Use function intepolate(). =Interpolate($A$1:$A$3,$B$1:$B$3,D1,FALSE,FALSE)

Columns A and B should contain your input, and column G should contain all your date values. Formula goes into the column E.

YGA

I came across this and was reluctant to use an add-in because it makes it tough to share the sheet with people who don't have the add-in installed.

My officemate designed a clean formula that is relatively compact (at the expensive of using a bit of magic).

Things to note:

  • The formula works by:

    • using the MATCH function to find the row in the inputs range just before the value being searched for (e.g. 3 is the value just before 3.5)
    • using OFFSETs to select the square of that line and the next (in light purple)
    • using FORECAST to build a linear interpolation using just those two points, and getting the result
  • This formula cannot do extrapolations; make sure that your search value is between the endpoints (I do this in the example below by having extreme values).

Not sure if this is too complicated for folks; but it had the benefit of being very portable (and simpler than many alternate solutions).

If you want to copy-paste the formula, it is:

=FORECAST(F3,OFFSET(inputs,MATCH(F3,inputs)-1,1,2,1),OFFSET(inputs,MATCH(F3,inputs)-1,0,2,1

(inputs being a named range)

There are two functions, LINEST and TREND, that you can try to see which gives you the better results. They both take sets of known Xs and Ys along with a new X value, and calculate a new Y value. The difference is that LINEST does a simple linear regression, while TREND will first try to find a curve that fits your data before doing the regression.

A nice graphical way to see how well your interpolated results fit:

Take your date,value pairs and graph them using the XY chart in Excel (not the Line chart). Right-click on the resulting line on the graph and click 'Add trendline'. There are lots of different options to choose which type of curve fitting is used. Then you can go to the properties of the newly created trendline and display the equation and the R-squared value.

Make sure that when you format the trendline Equation label, you set the numerical format to have a high degree of precision, so that all of the significant digits of the equation constants are displayed.

The answer above by YGA doesn't handle end of range cases where the desired X value is the same as the reference range's X value. Using the example given by YGA, the excel formula would return #DIV/0! error if an interpolated value at 9999 was asked for. This is obviously part of the reason why YGA added the extreme endpoints of 9999 and -9999 to the input data range, and then assumes that all forecasted values are between these two numbers. If such padding is undesired or not possible, another way to avoid a #DIV/0! error is to check for an exact input value match using the following formula:

=IF(ISNA(MATCH(F3,inputs,0)),FORECAST(F3,OFFSET(inputs,MATCH(F3,inputs)-1,1,2,1),OFFSET(inputs,MATCH(F3,inputs)-1,0,2,1)),OFFSET(inputs,MATCH(F3,inputs)-1,1,1,1))

where F3 is the value where interpolated results are wanted.

Note: I would have just added this as a comment to the original YGA post, but I don't have enough reputation points yet.

darren

alternatively.

=INDEX(yVals,MATCH(J7,xVals,1))+(J7-MATCH(J7,xVals,1))*(INDEX(yVals,MATCH(J7,xVals,1)+1)-INDEX(yVals,MATCH(J7,xVals,1)))/(INDEX(xVals,MATCH(J7,xVals,1)+1)-MATCH(J7,xVals,1))

where j7 is the x value.

xvals is range of x values yvals is range of y values

easier to put this into code.

alexkovelsky

You can find out which formula fits best your data, using Excel's "trend line" feature. Using that formula, you can calculate y for any x

  1. Create linear scatter (XY) for it (Insert => Scatter);
  2. Create Polynominal or Moving Average trend line, check "Display Equation on chart" (right-click on series => Add Trend Line);
  3. Copy the equation into cell and replace x's with your desired x value

On screenshot below A12:A16 holds x's, B12:B16 holds y's, and C12 contains formula that calculates y for any x.

I first posted an answer here, but later found this question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!