I was wondering if I could get some help finding where I'm going wrong in my function. I've been trying to write a function to use the RK4 method (Runge-Kutta 4) to evaluate an ODE. I've tried changing a few things, but no matter what I do, the function will only return "#REF!". I've attached my RK4 code below (with the corresponding code for the differential equation used in the function). I'm trying to apply Euler method for the first few points, and then transition into using the RK4 method.
Function RK44(x0 As Double, y0 As Double, n As Integer, xtarg As Double) As Double
Dim k1 As Double, k2 As Double, k3 As Double, k4 As Double
Dim ym As Double, ym2 As Double, ye As Double
Dim Slope As Double, yi As Double, xi As Double, yold As Double
h = (xtarg - x0) / n
yi = y0
For i = 1 To n
'apply Euler Method to get y at the end of the interval
yold = yi
k1 = dCdt(yold)
ym = y + k1 * h / 2
k2 = dCdt(ym)
ym2 = y + k2 * h / 2
k3 = dCdt(ym2)
ye = y + k3 * h
k4 = dCdt(ye)
Slope = (1 / 6) * (k1 + 2 * k2 + 2 * k3 + k4)
yi = yold + Slope * h
xi = x0 + i * h
Next i
RK44 = yi
End Function
Here is my function,
Function dCdt(y As Variant)
dCdt = ((0.02) - (0.1) * (y))
End Function