我猜数组尺寸有一些问题

时间:2018-09-28 08:24:46

标签: python arrays 2d newtons-method index-error

使用此代码,我想使用牛顿方法从二维函数中查找最小值:

from numpy import array

from numpy.linalg import solve, norm

def newton2d(f, df, x, tol=1e-12, maxit=50):

    x = atleast_2d(x)

    for i in range(maxit):

        s = solve(df(x), f(x))
        x -=s
        if norm(s)<tol: print(x); print(i); break

f = lambda x: array([x[0]**2-x[1]**4, x[0]-x[1]**3])

df = lambda x: array([[2*x[0], -4*x[1]**3], [1, -3*x[1]**2]])


x = array([0.7, 0.7])

newton2d(f,df,x)

我认为这段代码应该可以工作,但是出现如下错误:

IndexError: index 1 is out of bounds for axis 0 with size 1

感谢您的帮助!

0 个答案:

没有答案