import sys import numpy as np import matplotlib.pyplot as plt def Newton(f, x, dfdx, tol = 1.0E-7, max_n=100): n = 0 while abs(f(x)) > tol and n <= max_n: x = x - f(x)/dfdx(x) n += 1 return x, n, f(x) def g(x): return np.exp(-0.1*x**2) * np.sin(np.pi / 2*x) def dg(x): return -2*0.1*x*np.exp(-0.1*x**2)*np.sin(np.pi/2*x) + \ np.pi/2*np.exp(-0.1*x**2)*np.cos(np.pi/2*x) x = np.linspace(0,10,1001) plt.plot(x,g(x)) plt.plot(x,np.zeros(len(x)), '--') plt.show() x0 = float(sys.argv[1]) x, n, val = Newton(g, x0, dg) print('Computed root:', x)