此代码是一个迭代求解器。如何在每次迭代后将键值写入 Excel 电子表格?例如迭代次数、Xl
、Xu
、Xmid
和每次迭代的误差。
Option Explicit
Public Function srkEquation(Temperature, Pressure, aValue, bValue, Alpha, R) As Double
'The function defining the SRK equation, which will ultimately used the SolveSRK function to return the value of molar volume or XrNew
Dim i As Integer 'The Counter
Dim Xl As Double 'The lower X bound Value
Dim Xu As Double 'The Upper X bound Value
Dim XrNew As Double 'The newest value of Xr available
Dim XrOld As Double 'The previous value of Xr calculated
Dim errorA As Double 'The error associated with each iteration
Dim errorS As Double 'The stopping criteria
errorS = 0.001 'Stopping criteria is 0.001
Xu = 0.5 'We are starting with an Upper Bound of 0.5 because the value for molar volume will not exceed one, and will most definitely not be close to that value either
Xl = 0 'Lower bounded at zero
Do While errorA > 0.001 'While the error of the iterations is greater than the stopping criteria then keep looping
For i = 1 To 100 'Most likely will be able 100 iterations, could be less or even more
XrNew = XrOld 'declare the new value of Xr to be the old one, and then take the midpoint of the values
XrOld = Xl + Xu / 2
errorA = Abs((XrNew - XrOld) / XrNew) 'Error calculation
If solveSRK(Temperature, Pressure, aValue, bValue, Alpha, R, Xl) * solveSRK(Temperature, Pressure, aValue, bValue, Alpha, R, XrOld) < 0 Then
'If the function of SRK using Xl multiplied by the function of SRK using Xu is smaller than zero, then make the midpoint Xr, your new upper bound
Xu = XrNew
XrNew = Xl + Xl / 2 'Calculate the new midpoint
errorA = Abs((XrNew - XrOld) / XrNew) 'Calculate the new error
Else
'If the function of SRK using Xl multiplied by the function of SRK using Xu is greater than zero, then make the midpoint Xr, your new lower bound
Xl = XrNew
XrNew = Xl + Xl / 2 'Calculate the new midpoint
errorA = Abs((XrNew - XrOld) / XrNew) 'Calculate the new error
End If
Next i
Loop
srkEquation = XrNew 'The output of the SRK equation will be the value of molar volume or XrNew
End Function
Public Function reducedT(ByVal Temperature As Double, ByVal criticalT As Double) As Double
'All of these variables are ByVal because they are inputted by the user, and can be varied at any point in time to account for different temperatures and pressures
'The reduced Temperaure needed in the calculation of Alpha
'This function uses the Critical Temperatures inputted by the user, and produces a dimensionless value
reducedT = Temperature / criticalT
End Function
Public Function valueOfA(ByVal criticalT As Double, ByVal criticalP As Double, ByVal R As Double) As Double
'All of these variables are ByVal because they are inputted by the user, and can be varied at any point in time to account for different temperatures and pressures or chemical species
'The Value of A needed in the ultimate SRK equation is calculated here, using a pretty basic equation
'This function uses the Critical Temperatures and Pressures inputted by the user
valueOfA = 0.427 * (R) ^ 2 * (criticalT) ^ 2 / criticalP
End Function
Public Function valueOfB(ByVal criticalT As Double, ByVal criticalP As Double, ByVal R As Double) As Double
'All of these variables are ByVal because they are inputted by the user, and can be varied at any point in time to account for different temperatures and pressures or chemical species
'The Value of B needed in the ultimate SRK equation is calculated here, using a pretty basic equation
'This function uses the Critical Temperatures and Pressures inputted by the user
valueOfB = 0.08664 * R * criticalT / criticalP
End Function
Public Function valueOfAlpha(ByVal Omega As Double, ByVal redT As Double) As Double
'All of these variables are ByVal because they are inputted by the user, and can be varied at any point in time to account for different temperatures and pressures or chemical species
'The Value of Alpha needed in the ultimate SRK equation is calculated here
'This function uses the Critical Temperatures and Omega value, and then ultimately the reduced temperature inputted by the user to find Alpha
valueOfAlpha = (1 + (0.48508 + 1.55171 * (Omega) - 0.15613 * (Omega) ^ 2) * (1 - ((redT) ^ (0.5)))) ^ (2)
End Function
Public Function solveSRK(ByVal Temperature As Double, ByVal Pressure As Double, ByVal aValue As Double, ByVal bValue As Double, ByVal Alpha As Double, ByVal R As Double, ByVal molarVolume As Double) As Double
'This function will solve the SRK equation using the user-inputted values for temperature, pressure, the molar volume calculated by the iterative methods, as well as the calculated a and b values
'All of these variables are ByVal because they are inputted by the user, and can be varied at any point in time to account for different temperatures and pressures or chemical species
solveSRK = (((R) * (Temperature)) / (molarVolume - bValue)) - (((aValue) * (Alpha)) / (molarVolume * (molarVolume + bValue))) - Pressure
'The solveSRK function will return the ultimate solution of the values inputted into it, this equation will be used by the iterative method to solve for the molarVolume or XrNew
End Function