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

1.532160
1.690133
1.630142
1.570152
1.510164
1.450177
1.390193
1.330211
1.270233
1.210259
1.150291
1.090330
1.030379
0.960453
0.900539
0.840656
0.780819
0.7201058
0.6601425
0.6002027
0.5403104

<colgroup><col><col></colgroup><tbody>
</tbody>
 
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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
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,214,798
Messages
6,121,636
Members
449,043
Latest member
farhansadik

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