Option Base 1
Function SimultEqNL(equations, variables, constants)
'Newton iteration method to find roots of nonlinear simultaneous equations
Dim I As Integer, J As Integer, K As Integer, N As Integer
Dim NIterations As Integer
Dim R As Integer, C As Integer
Dim VarAddr() As String, FormulaString() As String
Dim con() As Double, A() As Double, B() As Double
Dim V() As Double
Dim Y1 As Double, Y2 As Double
Dim tolerance As Double, incr As Double
N = equations.Rows.Count
K = variables.Rows.Count
If K = 1 Then K = variables.Columns.Count
If K <> N Then SimultEqNL = CVErr(xlErrRef): Exit Function
ReDim VarAddr(N), FormulaString(N), V(N), con(N)
ReDim A(N, N + 1), B(N, N + 1)
tolerance = 0.000000000001 'Convergence criterion.
incr = 0.0000000001 'Increment for numerical differentiation.
NIterations = 50
For I = 1 To N
VarAddr(I) = variables(I).Address
Next
'Initial values
For I = 1 To N
con(I) = constants(I).Value
V(I) = variables(I).Value: If V(I) = 0 Then V(I) = 1
Next
For J = 1 To NIterations
'Create N x N matrix of partial derivatives.
For R = 1 To N
For C = 1 To N
'Formulastring is formula in which all but one variable in each equation
'is replaced by current values.
FormulaString(R) = Application.ConvertFormula(equations(R).Formula, xlA1, xlA1,
xlAbsolute)
For I = 1 To N
If I <> C Then FormulaString(R) = Application.Substitute(FormulaString(R), VarAd
dr(I), V(I))
Next I
'Calculate partial derivative (central differences).
Y2 = Evaluate(Application.Substitute(FormulaString(R), VarAddr(C), V(C) * (1 + i
ncr)))
Y1 = Evaluate(Application.Substitute(FormulaString(R), VarAddr(C), V(C) * (1 - i
ncr)))
A(R, C) = (Y2 - Y1) / (2 * incr * V(C))
Next C
Next R
'Augment matrix of derivatives with vector of constants.
For R = 1 To N
FormulaString(R) = Application.ConvertFormula(equations(R).Formula, xlA1, xlA1,
xlAbsolute)
For C = 1 To N