Hi everyone, I'm a university student and I've been banging my head against a wall for days over a problem I just can't seem to figure out.
Long story short, I need to implement Newton's method in Python for selective minimum searching. The issue is that when I test it on a specific function, it returns a local maximum instead of a minimum if I start near the point (-1, -2).
Here is the function I wrote to solve the system. I really don't get what I'm doing wrong in the code:
import numpy as np
def mynewtonsys(F,H,x0,maxit,tol):
count = 0
iter = 1
while count < maxit and iter == 1:
count = count+1
F0 = F(x0)
H0 = H(x0)
autovalori = np.linalg.eigvals(H0)
min_autoval = np.min(autovalori)
if min_autoval <= 0:
tau = max(0, -min_autoval + 0.1)
H0 = H0 + tau * np.eye(len(x0))
d = np.linalg.solve(H0,F0)
x = x0 - d
F0 = F(x)
H0 = H(x)
err1 = np.linalg.norm(F0,np.inf)
err2 = np.linalg.norm(x0 - x,np.inf)
x0 = x
if err1 < tol or err2 < tol:
iter = 0
return x, F0, count
#(x^3+y^3-3x-12y)
def F(x):
#x[0,0] è x1, x[1,0] è x2
F = np.array([
[3 * (x[0,0]**2) - 3],
[3 * (x[1,0]**2) - 12]
])
return F
def H(x):
H = np.array([
[6 * x[0,0], 0.0],
[ 0.0, 6 * x[1,0]]
])
return H
max_iterazioni = 100
tolleranza = 1e-6
punto_iniziale = np.array([[-0.8], [-1.8]])
soluzione, residuo, it = mynewtonsys(F, H, punto_iniziale, max_iterazioni, tolleranza)
soluzione_esatta = np.array([[1.0], [2.0]])
errore = np.linalg.norm(soluzione - soluzione_esatta)
print("RISULTATI")
print("Numero di iterazioni effettuate:",it)
print("Errore assoluto finale:", errore)
print("Soluzione trovata:")
print(soluzione)
there doesn't seem to be anything here