Hello all, I'm working on a VBA program that uses the Redlich-Kwong equation of state to find volume. To find the roots of the equation (which can be found at the bottom of the code) I'm implementing the secant method. The program cycles through a temperature at several different pressures and outputs the V for each one. The issue I'm getting is at the "Else: Next i" line. It says there is "Else without if", even though there is an if statement above it. I don't know why it is doing this
. Any advice?
Sub Secant()
' Returns the root of a function of the form F(x) = 0
' using the Secant method.
' V1 is a first guess at the value of x that solves the equation
' V0 is a "previous" value not equal to X1.
Dim V As Double 'the current guess for root being sought
Dim Vold As Double 'previous guess for root being sought
Dim DeltaV As Double
Dim i As Integer 'counter
Const Tol = 0.00000001 'convergence tolerance
Dim T As Double
Dim P As Double
Dim ActRowT As Integer
Dim ActRowP As Integer
Dim ActRow As Integer
Dim ActColumn As Integer
Dim V0 As Double
Dim V1 As Double
V0 = ActiveSheet.Cells(1, 5)
V1 = ActiveSheet.Cells(2, 5)
Vold = V0
V = V1
ActRowT = 6
ActRowP = 6
ActRow = 6
ActColumn = 3
T = ActiveSheet.Cells(ActRowT, 1)
P = ActiveSheet.Cells(ActRowP, 2)
While T <> 0
While P <> 0
For i = 1 To 100
T = ActiveSheet.Cells(ActRowT, 1)
P = ActiveSheet.Cells(ActRowP, 2)
DeltaV = (V - Vold) / (1 - f(Vold, T, P) / f(V, T, P))
V = V - DeltaV
If Abs(DeltaV) < Tol Then ActiveSheet.Cells(ActRow, ActColumn) = V
Else: Next i
ActRow = ActRow + 1
ActRowP = ActRowP + 1
ActRowT = ActRowT + 1
Wend
Wend
End Sub
Function f(V As Double, T As Double, P As Double) As Double
Dim Tc As Double
Dim Pc As Double
Dim R As Double
Dim a As Double
Dim b As Double
Tc = ActiveSheet.Cells(1, 2)
Pc = ActiveSheet.Cells(2, 2)
R = ActiveSheet.Cells(3, 2)
a = 0.427 * ((R ^ 2 * Tc ^ 2.5) / Pc)
b = 0.0866 * Tc / Pc
f = P * V - (V * R * T) / (V - b) + a / ((V + b) * T ^ 0.5)
End Function
Bookmarks