
Originally Posted by
Steffen Thomsen
Hi,
You have to deduct the linear formula
Example
y = x^2 + x + 1
Sorry, but I am not following? Is this interpolation or something else?

Originally Posted by
MrShorty
to build a new table containing x1,y1 and x2, y2.
Thank you, but I am looking for something where the raw data is hardcoded into the function and not passed from a table. I'm not sure what the best way to hardcode the data would be - the only way I'm familiar with is Arrays, but perhaps someone can mention something else. But I'd be thinking something like for instance:
Dim X_vars() As Variant
Dim Y_vars() As Variant
X_vars = Array(1, 2, 3, 4)
Y_vars = Array(8, 5, 2.5, 2)
Question: If I try to use Double as the array type I get a #VALUE! error, instead I have to use variant - why is this?
Here's the interpolation function I've written - feedback and improvements are welcome 
Function interpolate(x As Double) As Double
Dim X_vars() As Variant
Dim Y_vars() As Variant
X_vars = Array(1, 2, 3, 4)
Y_vars = Array(8, 5, 2.5, 2)
If x < X_vars(LBound(X_vars)) Or x > X_vars(UBound(X_vars)) Then
' Return error if out of bounds
interpolate = CVErr(xlErrRef)
Else
Dim index As Integer
For i = LBound(X_vars) To UBound(X_vars) - 1
If x >= X_vars(i) And x <= X_vars(i + 1) Then
index = i
Exit For
End If
Next i
interpolate = Y_vars(index) + (Y_vars(index + 1) - Y_vars(index)) * (x - X_vars(index)) / (X_vars(index + 1) - X_vars(index))
End If
End Function
Bookmarks