Hey guys,
I posted a thread recently wondering about the best way to construct a Newton-Raphson method to approximate a list of cells using VBA, and ended up coming up with something that works. I'll link back to the old thread once I figure out how, and I'll post my code below.
The issue is, the "correct" solution to the equation is the one that is always positive. Sometimes some of the cells give me negative values, which later on in my spreadsheet gives me errors or incorrect solutions. I can't come up with an initial guess that gives me a positive root every time, and I am wondering if there is a way to inside of the newton raphson method, force the method to converge onto the first POSITIVE real solution. ANY help would be so appreciated, this has me thoroughly stumped. Maybe I could set up something where if a negative value for P pops up, it forces p to a higher and higher guess for p, until it converges on the real solution? Any help would be great, and I'd love to hear your thoughts. Here's my code as it is, and I have a list of cells in my spreadsheet that say, "=NR(b, c, d, e, 0)" except b c d and e are just 4 columns of cells in the same row as the NR equation. Please let me know your thoughts, also, how do I link my old thread to this one?
I posted a thread recently wondering about the best way to construct a Newton-Raphson method to approximate a list of cells using VBA, and ended up coming up with something that works. I'll link back to the old thread once I figure out how, and I'll post my code below.
The issue is, the "correct" solution to the equation is the one that is always positive. Sometimes some of the cells give me negative values, which later on in my spreadsheet gives me errors or incorrect solutions. I can't come up with an initial guess that gives me a positive root every time, and I am wondering if there is a way to inside of the newton raphson method, force the method to converge onto the first POSITIVE real solution. ANY help would be so appreciated, this has me thoroughly stumped. Maybe I could set up something where if a negative value for P pops up, it forces p to a higher and higher guess for p, until it converges on the real solution? Any help would be great, and I'd love to hear your thoughts. Here's my code as it is, and I have a list of cells in my spreadsheet that say, "=NR(b, c, d, e, 0)" except b c d and e are just 4 columns of cells in the same row as the NR equation. Please let me know your thoughts, also, how do I link my old thread to this one?
Code:
Public Function f(x, b, c, d, e)
'Input a function that you are interested in finding the root of
'COUNT NUMBER OF CELLS HERE AND STORE AS J
'j = 4 'Worksheets("Sheet1").Range("Ce1:e38").Cells.SpecialCells(xlCellTypeConstants).Count
'b = Cells(j, "B")
'c = Cells(j, "A")
'd = Cells(j, "C")
f = -d * x ^ 3 + d * e * x ^ 2 + d * b ^ 2 * x + c * e * b ^ 2 - d * e * b ^ 2
End Function
Public Function df(x, b, c, d, e)
'Input derivative of function
'COUNT NUMBER OF CELLS HERE AND STORE AS J
'j = 4 'Worksheets("Sheet1").Range("e1:e38").Cells.SpecialCells(xlCellTypeConstants).Count
'b = Cells(j, "B")
'c = Cells(j, "A")
'd = Cells(j, "C")
df = -3 * d * x ^ 2 + 2 * e * d * x + d * b ^ 2
End Function
Public Function NR(b, c, d, e, p0) '(i, n, err, tol, p0)
'This is a method of approximating the answer for the wind column #2... It works similar to the way excel's solver would except it is faster since it has a taylor series approximation to conform to. (it uses derivatives to estimate the answer, not a linear approximation. Thus less iterations and a quicker result.)
'Dim the variable that gets updated by this function
Dim p, p_old As Double
' Dim b, c, d As Integer
n = 100
tol = 0.000001
'Initialize our Iteration'
i = 0
p = p0
'perform the iteration
Do
p_old = p
'From Newton's Formula
p = p - f(p, b, c, d, e) / df(p, b, c, d, e)
i = i + 1
'if p is not 1, we have an error
If (p <> 1) Then
err = Abs((p - pold) / p)
End If
'input the stopping criterion for the iteration
If err < tol Or i >= n Then Exit Do
Loop
NR = p
End Function