Newtons Method in Python - Stack Overflow
stackoverflow.com › questions › 8222247Nov 22, 2011 · def main (): dir (sympy) print ("NEWTONS METHOD") print ("Write your expression in terms of 'x' ") e = sympy.sympify (raw_input ("input expression here: ")) f = sympy.Symbol ('x') func1 = e func1d = sympy.diff (e,f) print ("the dirivative of your function = "), func1d x = input ("number to substitude for x: ") func1sub = func1.subs ( {'x':x}) func1dsub = func1d.subs ( {'x':x}) n = x - float (func1sub/func1dsub) while n != x: func1sub = func1.subs ( {'x':x}) func1dsub = ...
How to use the Newton's method in python
moonbooks.org › Articles › How-to-use-the-NewtonsFeb 21, 2019 · How to use the Newton's method in python ? Solution 1 from scipy import misc def NewtonsMethod(f, x, tolerance=0.000001): while True: x1 = x - f(x) / misc.derivative(f, x) t = abs(x1 - x) if t < tolerance: break x = x1 return x def f(x): return (1.0/4.0)*x**3+(3.0/4.0)*x**2-(3.0/2.0)*x-2 x = 4 x0 = NewtonsMethod(f, x) print('x: ', x) print('x0: ', x0) print("f(x0) = ", ((1.0/4.0)*x0**3+(3.0/4.0)*x0**2-(3.0/2.0)*x0-2 ))