I've written this function in python:
def f2(x):
return (5.0*x + log1p(x) - 10000.0)
def dfdx2(x):
return (5.0-(1.0/x))
def newtonRaphson2(f, dfdx, x, tol):
x0 = x
for i in range(1, 2000):
if f(x) == 0.0:
return x
if dfdx(x) == 0.0:
print(dfdx(x))
break
x = x - (f(x) / dfdx(x))
#print(x)
Er = abs(x0-x)/abs(x0)
if Er <= tol:
return x
print(Er)
x0 = x
return x
Then I execute it like this:
task2 = newtonRaphson2(f2, dfdx2, 1, 0.000001);
print(task2)
For the output check the Er outputs final accuracy of 4.245665128208564e-05 before it returns x.
X is returned at 1998.479871524306, which is a pretty good estimate, but I'm preferably looking to get it down to 1.0e-06 at least. Changing tol variable to 1.0e-08 seems to do nothing.
I'm guessing maybe putting every variable into double is a better idea, but I still have no idea why my code stops where it does. I'm not that stable with python either, which is why I'm asking. I've already written one of these that works, but its for a far simpler equation.