Excel VBA Cubic Spline Interpolation

wengkey95

New Member
Joined
Jan 15, 2019
Messages
1
Hi Guys,
By referring to the older post, I found something below as Cubic Spline Interpolation. The formula I found was s(x) = a(x-xi)^3+ b(x-xi)^2 + c(x-xi) + d, I would like to understand how it translate to the algorithm below.

Much Appreciate.

Function spline(periodcol As Range, ratecol As Range, x As Range)
Dim period_count As Integer
Dim rate_count As Integer
period_count = periodcol.Rows.Count
rate_count = ratecol.Rows.Count
If period_count <> rate_count Then
spline = "Error: Range count dos not match"
GoTo endnow
End If

ReDim Xin(period_count) As Single
ReDim Yin(period_count) As Single
Dim c As Integer
For c = 1 To period_count
Xin(c) = periodcol(c)
Yin(c) = ratecol(c)
Next c
Dim n As Integer
Dim i, k As Integer
Dim p, qn, sig, un As Single
ReDim u(period_count - 1) As Single
ReDim yt(period_count) As Single
n = period_count
yt(1) = 0
u(1) = 0


'calculate the derivatives for cubic interpolation
For i = 2 To n - 1
sig = (Xin(i) - Xin(i - 1)) / (Xin(i + 1) - Xin(i - 1))
p = sig * yt(i - 1) + 2
yt(i) = (sig - 1) / p
u(i) = (Yin(i + 1) - Yin(i)) / (Xin(i + 1) - Xin(i)) - (Yin(i) - Yin(i - 1)) / (Xin(i) - Xin(i - 1))
u(i) = (6 * u(i) / (Xin(i + 1) - Xin(i - 1)) - sig * u(i - 1)) / p

Next i

qn = 0
un = 0
yt(n) = (un - qn * u(n - 1)) / (qn * yt(n - 1) + 1) 'last knot boundary condition


For k = n - 1 To 1 Step -1
yt(k) = yt(k) * yt(k + 1) + u(k) 'second derivatives
Next k


'now eval spline at one point
Dim klo, khi As Integer
Dim h, b, a As Single
klo = 1
khi = n


Do
k = khi - klo
If Xin(k) > x Then
khi = k
Else
klo = k
End If
k = khi - klo
Loop While k > 1


h = Xin(khi) - Xin(klo)
a = (Xin(khi) - x) / h
b = (x - Xin(klo)) / h
y = a * Yin(klo) + b * Yin(khi) + ((a ^ 3 - a) * yt(klo) + (b ^ 3 - b) * yt(khi)) * (h ^ 2) / 6


spline = y
endnow:
End Function
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

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