I have defined the function newton to be the implementation of the Newton method to find roots of real-valued and vector-valued functions.
The problem is that, the sequence created is being included into an extra [] at every step.
Why is this happening and how can I avoid it by making changes on the newton function definition?
This is my output sequence:
x_1 = [[4.66097544]]
x_2 = [[[2.76124695]]]
x_3 = [[[[3.14624513]]]]
x_4 = [[[[[3.14159265]]]]]
This is my code:
from math import exp
import numpy as np
from numpy import array
from numpy.linalg import norm
from numpy import sin,cos,pi
def newton (F, DF, x0, eps, K):
x = x0 - np.linalg.inv(DF(x0))*F(x0)
k=1
print("x_", end="")
print(k, end="")
print(" = ", end="")
print(x)
print("( ||F(x)|| = ", end="")
print(norm(F(x)), end=" )\n")
while (norm(F(x)) > eps) and (k<=K):
x = x - np.linalg.inv(DF(x))*F(x)
k += 1
print("x_", end="")
print(k, end="")
print(" = ", end="")
print(x)
print("( ||F(x)|| = ", end="")
print(norm(F(x)), end=" )\n")
return x,k
F = lambda y: cos(y/2)
DF = lambda y: array([-sin(y/2)/2])
x,k = newton(F, DF, array([1.]), 1e-8, 100)
Any help or comments are highly appreciated. Thanks!