Why do I keep getting ValueError: solve: Input operand 1 has a mismatch in its core dimension 0?
Why do I keep getting ValueError: solve: Input operand 1 has a mismatch in its core dimension 0?
我正在尝试为 Python 中的非线性系统的牛顿法编写代码。我的 g 函数是一个 5x1 矩阵,它的雅可比矩阵(导数矩阵)是一个 5x5 矩阵。初始 y 值 (y0) 的向量也是 5x1。我一直收到错误
ValueError: solve: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (m,m),(m,n)->(m,n) (size 1 is different from 5)
我尝试手动解决我的问题,但是当我 运行 我的代码时,我得到了答案。我怀疑这个错误是我忽略的愚蠢的事情。但我终其一生都无法弄清楚问题出在哪里。下面是我的代码:
def newton_prob(y0, g, jac, tol):
max_iteration = 100
tol = 1e-6
y_value = y0
for k in range(max_iteration):
J = np.array(jac(y_value))
G = np.array(g(y_value))
diff = np.linalg.solve(J, -G)
y_value = y_value + diff
stopcrit = np.linalg.norm(y_value - y0, 2) / np.linalg.norm(y0, 2)
if stopcrit < tol:
print('Convergence, nre iter:' , k)
break
else:
return y_value
#Test
y0 = np.array([[17],
[17],
[17],
[17],
[17]])
g = lambda y: np.array([[-9*y[1] + 18*y[0] - 9*(17) - (3/16)*y[0]*y[1] + (3/16)*y[0]*(17) + (124/27)],
[-9*y[2] + 18*y[1] -9*y[0] -(3/16)*y[1]*y[2] + (3/16)*y[0]*y[1] +(557/108)],
[-9*y[3] + 18*y[2] -9*y[1] + (3/16)*y[1]*y[2] - (3/16)*y[2]*y[3] + 6],
[-9*y[4] + 9*y[3] -9*y[2] - (3/16)*y[3]*y[4] + (3/16)*y[2]*y[3] + (775/108)],
[-9*(43/3) +18*y[4] -9*y[3] + (3/16)*y[3]*y[4] - (3/16)*y[4]*(43/3) + (236/27)]])
jac = lambda y: np.array([[18 -(3/16)*y[1] + (3/16)*(17), -9 -(3/16)*y[0], 0, 0, 0],
[-9 + (3/16)*y[1], 18 - (3/16)*y[2] + (3/16)*y[0], -9 - (3/16)*y[1], 0, 0],
[0, -9 + (3/16)*y[2], 18 + (3/16)*y[1] - (3/16)*y[3], -9 - (3/16)*y[2], 0],
[0, 0, -9 + (3/16)*y[3], 9 - (3/16)*y[3] + (3/16)*y[2], -9 - (3/16)*y[3]],
[0, 0, 0, -9 + (3/16)*y[4], 18 + (3/16)*y[3] - (3/16)*(43/3)]])
tol = 1e-6
print(newton_prob(y0, g, jac, tol))
如果可能请帮忙
y0
和 g
的尺寸似乎有误。将它们缩小一维:
y0 = np.array([17,
17,
17,
17,
17])
g = lambda y: np.array([-9*y[1] + 18*y[0] - 9*(17) - (3/16)*y[0]*y[1] + (3/16)*y[0]*(17) + (124/27),
-9*y[2] + 18*y[1] -9*y[0] -(3/16)*y[1]*y[2] + (3/16)*y[0]*y[1] +(557/108),
-9*y[3] + 18*y[2] -9*y[1] + (3/16)*y[1]*y[2] - (3/16)*y[2]*y[3] + 6,
-9*y[4] + 9*y[3] -9*y[2] - (3/16)*y[3]*y[4] + (3/16)*y[2]*y[3] + (775/108),
-9*(43/3) +18*y[4] -9*y[3] + (3/16)*y[3]*y[4] - (3/16)*y[4]*(43/3) + (236/27)])
输出:
[ 1.90727371e-01 -1.59772226e+01 -4.74196657e+01 -5.16165838e+03 4.86453399e+01]
我正在尝试为 Python 中的非线性系统的牛顿法编写代码。我的 g 函数是一个 5x1 矩阵,它的雅可比矩阵(导数矩阵)是一个 5x5 矩阵。初始 y 值 (y0) 的向量也是 5x1。我一直收到错误
ValueError: solve: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (m,m),(m,n)->(m,n) (size 1 is different from 5)
我尝试手动解决我的问题,但是当我 运行 我的代码时,我得到了答案。我怀疑这个错误是我忽略的愚蠢的事情。但我终其一生都无法弄清楚问题出在哪里。下面是我的代码:
def newton_prob(y0, g, jac, tol):
max_iteration = 100
tol = 1e-6
y_value = y0
for k in range(max_iteration):
J = np.array(jac(y_value))
G = np.array(g(y_value))
diff = np.linalg.solve(J, -G)
y_value = y_value + diff
stopcrit = np.linalg.norm(y_value - y0, 2) / np.linalg.norm(y0, 2)
if stopcrit < tol:
print('Convergence, nre iter:' , k)
break
else:
return y_value
#Test
y0 = np.array([[17],
[17],
[17],
[17],
[17]])
g = lambda y: np.array([[-9*y[1] + 18*y[0] - 9*(17) - (3/16)*y[0]*y[1] + (3/16)*y[0]*(17) + (124/27)],
[-9*y[2] + 18*y[1] -9*y[0] -(3/16)*y[1]*y[2] + (3/16)*y[0]*y[1] +(557/108)],
[-9*y[3] + 18*y[2] -9*y[1] + (3/16)*y[1]*y[2] - (3/16)*y[2]*y[3] + 6],
[-9*y[4] + 9*y[3] -9*y[2] - (3/16)*y[3]*y[4] + (3/16)*y[2]*y[3] + (775/108)],
[-9*(43/3) +18*y[4] -9*y[3] + (3/16)*y[3]*y[4] - (3/16)*y[4]*(43/3) + (236/27)]])
jac = lambda y: np.array([[18 -(3/16)*y[1] + (3/16)*(17), -9 -(3/16)*y[0], 0, 0, 0],
[-9 + (3/16)*y[1], 18 - (3/16)*y[2] + (3/16)*y[0], -9 - (3/16)*y[1], 0, 0],
[0, -9 + (3/16)*y[2], 18 + (3/16)*y[1] - (3/16)*y[3], -9 - (3/16)*y[2], 0],
[0, 0, -9 + (3/16)*y[3], 9 - (3/16)*y[3] + (3/16)*y[2], -9 - (3/16)*y[3]],
[0, 0, 0, -9 + (3/16)*y[4], 18 + (3/16)*y[3] - (3/16)*(43/3)]])
tol = 1e-6
print(newton_prob(y0, g, jac, tol))
如果可能请帮忙
y0
和 g
的尺寸似乎有误。将它们缩小一维:
y0 = np.array([17,
17,
17,
17,
17])
g = lambda y: np.array([-9*y[1] + 18*y[0] - 9*(17) - (3/16)*y[0]*y[1] + (3/16)*y[0]*(17) + (124/27),
-9*y[2] + 18*y[1] -9*y[0] -(3/16)*y[1]*y[2] + (3/16)*y[0]*y[1] +(557/108),
-9*y[3] + 18*y[2] -9*y[1] + (3/16)*y[1]*y[2] - (3/16)*y[2]*y[3] + 6,
-9*y[4] + 9*y[3] -9*y[2] - (3/16)*y[3]*y[4] + (3/16)*y[2]*y[3] + (775/108),
-9*(43/3) +18*y[4] -9*y[3] + (3/16)*y[3]*y[4] - (3/16)*y[4]*(43/3) + (236/27)])
输出:
[ 1.90727371e-01 -1.59772226e+01 -4.74196657e+01 -5.16165838e+03 4.86453399e+01]