PDA

View Full Version : Newton's Method Iteration Code in VBA- No errors but will not output



lobobunny
12-17-2017, 10:12 AM
Hello! I am trying to write a code to use newtons method to find the root of a function. Newtons method is xnew = x - f(x)/f'(x)
Iterations continue until xnew = 0 within a given tolerance
I wrote a code and my logic seems good to me. I can't find any issues. The program is not giving me any errors, but it will not output anything. I have my output inside the loop. I can't figure out what's wrong. Does anyone see why this is not outputting anything? Please and thank you!

I attached a screenshot of my spreadsheet so you can see how I have it set up.

Thanks so much.


Sub NewtonMethod()
Dim x As Double
Dim xnew As Double
Dim EoverD As Double
Dim Re As Double
Dim ActRow As Integer
Dim FofX As Double
Dim DfDx As Double
Dim Tol As Double
Dim iMax As Integer
Dim i As Integer

Worksheets(ActiveSheet.Name).Activate

iMax = 100
Tol = ActiveSheet.Cells(18, 5)
x = ActiveSheet.Cells(25, 1)
EoverD = ActiveSheet.Cells(1, 5)
Re = ActiveSheet.Cells(2, 5)
ActRow = 25
i = 0

While ((i <= iMax) And (Abs(xnew) > Tol))
i = i + 1
FofX = (-1 / Sqr(x)) - (2 * (Log((EoverD / 3.7) + (2.51 / (Re * Sqr(x)))) / Log(10)))
DfDx = Fcalc(x)
xnew = x - (FofX / DfDx)
x = xnew
ActiveSheet.Cells(25 + i, 1) = x
ActiveSheet.Cells(24 + i, 2) = FofX
ActiveSheet.Cells(24 + i, 3) = DfDx
ActiveSheet.Cells(24 + i, 4) = i
Wend

End Sub



Function Fcalc(x)
Fcalc = (1 / x^(1.5)) + ((2 / Log(10)) * ((2.51 / (2 * Re * x^(1.5))) / ((EoverD / 3.7) + (2.51 / Re * Sqr(x)))))
End Function

SamT
12-17-2017, 01:42 PM
xnew = 0 (Zero) when this line is first encountered

While ((i <= iMax) And (Abs(xnew) > Tol))
Zero is less than .000...1 so the While loop is never entered.

Try replacing all instances of "xnew" in your code with "x"
Even

xnew = x - (FofX / DfDx)
'to
x = x - (FofX / DfDx)

Paul_Hossler
12-17-2017, 06:03 PM
Look at a related post

http://www.vbaexpress.com/forum/showthread.php?58661-Possible-to-Run-Goal-Seek-on-Array-elements-Within-VBA


1. Use Option Explicit at top of module

2. EoverD and Re are Dim-ed in NewtonMethod, and are out of scope in FCalcX

Either pass them to FCalcX or Dim them at the module level, i.e. at the top and not in a Sub

snb
12-18-2017, 01:58 AM
rewritten to:


Sub M_snb_NewtonMethod()
sn = Array(Cells(18, 5), Cells(25, 1), Cells(1, 5), Cells(2, 5))
x = sn(1)
ReDim sp(100, 3)

For j = 1 To 100
sp(j, 1) = (-1 / Sqr(x)) - (2 * (Log((sn(2) / 3.7) + (2.51 / (sn(3) * Sqr(x)))) / Log(10)))
sp(j, 2) = Fcalc(x, sn)
sp(j, 0) = x - (sp(j, 2) / sp(j, 1))
sp(j, 3) = j
If sp(j,0) > sn(0) Then Exit For
Next

Cells(25).Resize(10, 4) = sp
End Sub

Function Fcalc(y, st)
Fcalc = (1 / y ^ (1.5)) + ((2 / Log(10)) * ((2.51 / (2 * st(3) * y ^ (1.5))) / ((st(2) / 3.7) + (2.51 / st(3) * Sqr(y)))))
End Function