我有这段代码:
import numpy as np
import math as m
import numdifftools as nd
def g(x):
return np.array(x[1]*m.exp(x[0]-1)+((x[1]**2)/3)-x[0]-2*x[1])
def Newton(f):
Hess_f = nd.Hessian(f)
Jac_f = nd.Jacobian(f)
x0 = np.array([0,0])
print(x0,len(x0),"info about x0")
Hm = np.matrix(Hess_f(x0))
Hi = np.linalg.inv(Hm)
t = Jac_f(x0).dot(Hi)
print(t, len(t),"info about t")
x1 = x0 - t
print(x1, len(x1),"info about x1")
while np.linalg.norm(x1-x0) > 0.05:
x0 = x1
Hm = np.matrix(Hess_f(x0))
Hi = np.linalg.inv(Hm)
t = Jac_f(x0).dot(Hi)
x1 = x0 - t
print(x1)
return(x1)
Newton(g)
正如你所看到的,我得到了这条打印线,用于查看x0,x1和t变量的长度。问题在于t和x1:程序显示它们的长度为1(但是在打印时,它们在数组中显示2个值)。为什么是这样? 0.05中有停止算法的错误标准。