23 KiB
23 KiB
In [1]:
import numpy as np
import matplotlib.pyplot as plt
In [4]:
def f(x):
return x * np.e**x - 1
def fp(x):
return np.e**x + x * np.e**x
In [5]:
def newton(x,f,fp,tol=1e-5):
fx = f(x)
while abs(fx) > tol:
fx = f(x)
fxp = fp(x)
x = x - (fx/fxp)
return x
In [10]:
xfs = np.arange(0,1.2,0.01)
yfs = np.array(f(xfs))
x = newton(1,f,fp,1e-10)
plt.plot(xfs,yfs,'r')
plt.plot(x,0,'.b')
Out[10]:
In [ ]: