Code của em:
Function Secant(X0 As Double, X1 As Double) As Double
' Returns the root of a function of the form F(x) = 0
' using the Secant method.
' X1 is a first guess at the value of x that solves the equation
' X0 is a "previous" value not equal to X1.
' This function assumes there is an external function named FS that
' represents the function whose root is to be solved
Dim X As Double 'the current guess for root being sought
Dim Xold As Double 'previous guess for root being sought
Dim DeltaX As Double
Dim Iter As Integer 'iteration counter
Const Tol = 0.00000001 'convergence tolerance
Xold = X0
X = X1
'permit a maximum of 100 iterations
For Iter = 1 To 100
DeltaX = (X - Xold) / (1 - FS(Xold) / FS(X))
X = X - DeltaX
If Abs(DeltaX) < Tol Then GoTo Solution
Next Iter
MsgBox "No root found", vbExclamation, "Secant result"
Solution:
Secant = X
End Function
_____________________________________
Function FS(X As Double, ppr As Double, Tpr As Double) As Double
A1 = 0.3265
A2 = -1.07
A3 = -0.5339
A4 = 0.01569
A5 = -0.05165
A6 = 0.5475
A7 = -0.7361
A8 = 0.1844
A9 = 0.1056
A10 = 0.6134
A11 = 0.721
pr = 0.27 * ppr / (X * Tpr)
K1 = A1 + A2 / Tpr + A3 / (Tpr) ^ 3 + A4 / (Tpr) ^ 4 + A5 / (Tpr) ^ 5
K2 = A6 + A7 / (Tpr) + A8 / ((Tpr) ^ 2)
K3 = A9 * (A7 / (Tpr) + A8 / (Tpr) ^ 2)
K4 = A10 * (1 + A11 * (pr) ^ 2) * ((pr) ^ 2) / ((Tpr) ^ 3) * Exp(-A * 11 * (pr) ^ 2)
FS = X - (1 + K1 * pr + K2 * (pr) ^ 2 - K3 * (pr) ^ 5 + K4)
End Function