Page 253 - Excel for Scientists and Engineers: Numerical Methods
P. 253
230 EXCEL NUMERICAL METHODS
Option Explicit
Option Base 1
.............................................................
Function Runge3(x_variable, y-variables, deriv-formulas, interval, Optional -
index)
'Runge-Kutta method to solve ordinary differential equations.
'Solves problems involving simultaneous first-order differential equations.
'x-variable is a reference to the independent variable x.
'y-variables is a reference to the dependent variables y(1) ... y(N).
'deriv-formulas is a reference to the derivatives dy(i)/dx, in same order.
'interval is a reference to delta x
'index specifies the y(i) to be returned. If omitted, returns the array.
Dim FormulaText() As String, XAddr As String, YAddr() As String
Dim J As Integer, N As Integer
N = y-variables.Columns.Count
If N = 1 Then N = y-variables.Rows.Count
ReDim FormulaText(N), YAddr(N)
'GET THE X REFERENCE, Y REFERENCE AND DERIVATIVE FORMULA
XAddr = x-variable.Address
ForJ=IToN
YAddr(J) = y-variables(J).Address
FormulaText(J) = Application.ConvertFormula(deriv-formulas(J).Formula, -
xlAl, xlAl, xlAbsolute)
Next J
If IsMissing(index) Then
Runge3 = RK3(N, FormulaText, XAddr, YAddr, x-variable, y-variables.
interval)
Else
Runge3 = RK3(N, FormulaText, XAddr, YAddr, x-variable, y-variables, -
interval) (index)
End If
End Function
..............................................................
Private Function RK3(N, FormulaText, XAddr, YAddr, x-variable, y-variables, -
H)
Dim X As Double, Y() As Double, term0 As Double
Dim J As Integer, K As Integer
ReDim term(4, N), Y(N)
K = 1 : X = x-variable.Value
For J = 1 To N: Y(J) = y-variables(J).Value: Next J
Call eval3(N, FormulaText, XAddr, YAddr, X, Y, H, K, term)
K = 2: X = x-variable.Value + H / 2
For J = 1 To N: Y(J) = y-variables(J).Value + term(1, J) / 2: Next J
Call eval3(N, FormulaText, XAddr, YAddr, X, Y, H, K, term)