VBA Code:
Function Secant(R0 As Double, R1 As Double) As Double
' returns the root of a function of the form F(R) = 0
' using the secant method.
' R1 is a first guess at value of R that solves the equation
' R0 is a "previous" value not equal to R1.
' this function assumes there is an external function named F(r) that
' represents the function whose root is to be solved
Dim R As Double 'the current guess for root being sought
Dim Rold As Double 'previous guess for root being sought
Dim DeltaR As Double
Dim Iter As Integer 'iteration counter
Const Tol = 0.00000001 'convergence tolerance
Rold = R0
R = R1
'permit a maximum of 100 iterations
For Iter = 1 To 100
DeltaR = (R - Rold) / (1 - f(Rold) / f(R))
R = R - DeltaR
If Abs(DeltaR) < Tol Then GoTo solution
Next Iter
MsgBox "no root found", vbExclamation, "secant result"
solution:
Secant = R
End Function
Function f(R As Double)
f = a + b(Ln(R)) + c(Ln(R)) ^ 3
a = 1.29241 * 10 ^ -3
b = 2.341077 * 10 ^ -4
c = 8.775468 * 10 ^ -8
End Function
Sub secantmethod()
Rold = 18000
R = 20000
tolerence = 0.00000001
e = 0.00000001
bound = Abs((R - Rold))
Do While bound < tolerence
fxc = (3 * R) - (2 * R ^ 2) + (e ^ R) - 10
fxp = (3 * Rold) - (2 * Rold ^ 2) + (e ^ Rold) - 10
nxt = R - ((fxc * (Rold - R)) / (fxp - fxc))
Rold = R
R = nxt
bound = Abs((R - Rold))
Cells(i + 3) = r2
Cells(i + 4) = ea
Loop
MsgBox (nxt)
End Sub
i can't compute this at all, i'm a beginner at using this vba. i just use the code i see on internet to do that
Last edited by a moderator: