首页 > 解决方案 > 我猜数组尺寸的一些问题

问题描述

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

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

谢谢你的帮助!!

标签: pythonarrays2dnewtons-methodindex-error

解决方案


推荐阅读