from sympy import *
def calcNewton(f, df, xn):
return [N(f.subs(x, xn)), N(df.subs(x, xn)), N(xn - (f.subs(x, xn)/df.subs(x, xn)))]
x0 = 1.0
x = symbols("x")
f = cos(x) - x
iteraciones = 4
df = f.diff(x)
init_printing()
display(f)
print("n \t xn \t\t f(xn) \t\t f'(xn) \t xn+1")
xn = x0
for n in range(iteraciones):
xn1 = calcNewton(f, df, xn)
print("%d \t %0.9f \t %0.9f \t %0.9f \t %0.9f" %(n, xn, xn1[0], xn1[1], xn1[2]))
xn = xn1[2]
n xn f(xn) f'(xn) xn+1 0 1.000000000 -0.459697694 -1.841470985 0.750363868 1 0.750363868 -0.018923074 -1.681904953 0.739112891 2 0.739112891 -0.000046456 -1.673632544 0.739085133 3 0.739085133 -0.000000000 -1.673612029 0.739085133