Linear Interpolation Function - Using for non-linear data

pcarrollg

New Member
Joined
Apr 3, 2013
Messages
9
Hi,

I am trying to write a function that will interpolate between values in a table. I have two columns, x and y. I have a value for x, and I want to find the corresponding y based off of the data. I have a function I found that will perform a linear interpolation. However my data is not linear for the entire data set. But for small portions of the table (for example between rows 1 and 2, 2 and 3, 3 and 4... etc.) the data can be assumed to be linear.

What I want is to rewrite the linear interpolation function so that it can calculate based off of a small interval. Write now i am having trouble getting it to work, and keep getting a #Value error. I believe this is a result of the fact that my data is not sorted in terms of descending x values.

Here is the code I am using

Code:
Public Function Linterp(Tbl As Range, x As Double) As Variant
     ' linear interpolator / extrapolator
     ' Tbl is a two-column range containing known x, known y, sorted x descending
     
    Dim nRow As Long
    Dim iLo As Long, iHi As Long
     
    nRow = Tbl.Rows.Count
    If nRow < 2 Or Tbl.Columns.Count <> 2 Then
        Linterp = CVErr(xlErrValue)
        Exit Function '-------------------------------------------------------->
    End If
     
    If x > Tbl(1, 1) Then ' x > xmax, extrapolate from first two entries
        iHi = 1
        iLo = 2
    ElseIf x < Tbl(nRow, 1) Then ' x < xmin, extrapolate from last two entries
        iHi = nRow - 1
        iLo = nRow
    Else
        iHi = Application.Match(x, Application.Index(Tbl, 0, 1), 1)
        If Tbl(iHi, 1) = x Then ' x is exact from table
            Linterp = Tbl(iHi, 2)
            Exit Function '---------------------------------------------------->
        Else ' x is between tabulated values, interpolate
            iLo = iHi + 1
        End If
    End If
     
    Linterp = Tbl(iLo, 2) + (Tbl(iHi, 2) - Tbl(iLo, 2)) _
    * (x - Tbl(iLo, 1)) / (Tbl(iHi, 1) - Tbl(iLo, 1))
     
End Function

Here is an example set of data

[TABLE="width: 180"]
<colgroup><col><col></colgroup><tbody>[TR]
[TD]1.532[/TD]
[TD]160[/TD]
[/TR]
[TR]
[TD]1.690[/TD]
[TD]133[/TD]
[/TR]
[TR]
[TD]1.630[/TD]
[TD]142[/TD]
[/TR]
[TR]
[TD]1.570[/TD]
[TD]152[/TD]
[/TR]
[TR]
[TD]1.510[/TD]
[TD]164[/TD]
[/TR]
[TR]
[TD]1.450[/TD]
[TD]177[/TD]
[/TR]
[TR]
[TD]1.390[/TD]
[TD]193[/TD]
[/TR]
[TR]
[TD]1.330[/TD]
[TD]211[/TD]
[/TR]
[TR]
[TD]1.270[/TD]
[TD]233[/TD]
[/TR]
[TR]
[TD]1.210[/TD]
[TD]259[/TD]
[/TR]
[TR]
[TD]1.150[/TD]
[TD]291[/TD]
[/TR]
[TR]
[TD]1.090[/TD]
[TD]330[/TD]
[/TR]
[TR]
[TD]1.030[/TD]
[TD]379[/TD]
[/TR]
[TR]
[TD]0.960[/TD]
[TD]453[/TD]
[/TR]
[TR]
[TD]0.900[/TD]
[TD]539[/TD]
[/TR]
[TR]
[TD]0.840[/TD]
[TD]656[/TD]
[/TR]
[TR]
[TD]0.780[/TD]
[TD]819[/TD]
[/TR]
[TR]
[TD]0.720[/TD]
[TD]1058[/TD]
[/TR]
[TR]
[TD]0.660[/TD]
[TD]1425[/TD]
[/TR]
[TR]
[TD]0.600[/TD]
[TD]2027[/TD]
[/TR]
[TR]
[TD]0.540[/TD]
[TD]3104[/TD]
[/TR]
</tbody>[/TABLE]
 
No there is nothing below the data in my range. But I may need to expand the range as well. If I set the range to something I know it will not exceed, such as A1:A50, will I be alright? I just want to make sure that I continue to get #REF errors if the function attempts to interpolate in an area where there is no data.
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Just wanted to add my thanks to Andrew Poulsom. It took me a while to get my head around the combination of FORECAST, INDEX and MATCH but it was worth the effort.
I have had an additional problem due to the fact that this seems to work only when the values you wish to forecast are within the limits of the values in column A. That's where I got #REF! errors.
I settled for assuming that values outside the A range are linear (hence use FORECAST without INDEX/MATCH) and used a simple IF to check.
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,102
Members
452,379
Latest member
IainTru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top