python - 我猜数组尺寸的一些问题
问题描述
使用此代码,我想使用牛顿法从二维函数中找到最小值:
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
谢谢你的帮助!!
解决方案
推荐阅读
- python - 如何更改由日期组成的 xaxis 的视觉方面?
- python - 从两个列表python中获取无序的唯一元素
- google-sheets - 在 Google Analaytics Sheets 中按周排序添加
- android - Viewpager empty when opened second time
- machine-learning - val数据在拟合过程中的损失和准确性与评估过程不相等
- mongodb - Mongo中的连接嵌套数组
- amazon-web-services - AWS Lambda 函数会重试多少次以异步调用?
- laravel - rest api 登录资源的最佳实践
- domain-driven-design - 提醒 - DDD
- git - Git 日志获取真正的提交作者