简体   繁体   English

为什么我不断收到 ValueError:解决:输入操作数 1 的核心维度 0 不匹配?

[英]Why do I keep getting ValueError: solve: Input operand 1 has a mismatch in its core dimension 0?

I am attempting to write a code for Newton's Method for nonlinear systems in Python.我正在尝试为 Python 中的非线性系统的牛顿法编写代码。 My g function is a 5x1 matrix and the jacobian (derivative matrix) of this is a 5x5 matrix.我的 g function 是一个 5x1 矩阵,它的雅可比(导数矩阵)是一个 5x5 矩阵。 The vector for the initial y values (y0) is also a 5x1.初始 y 值 (y0) 的向量也是 5x1。 i keep on getting the error我不断收到错误

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)

I have tried solving my problem manually and I get my answer however when I run my code.我尝试手动解决我的问题,但是当我运行我的代码时我得到了答案。 I suspect that the error is something silly that I am simply overlooking.我怀疑这个错误是愚蠢的,我只是忽略了。 But I just can't for the life of me figure out what the issue is.但我无法终生弄清楚问题所在。 Below is my code:下面是我的代码:


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))

Please help if possible如果可能,请提供帮助

The dimensions of y0 and g seems to be wrong. y0g的尺寸似乎是错误的。 Reduce them by one dimension:将它们减少一维:

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)])

Output: Output:

[ 1.90727371e-01 -1.59772226e+01 -4.74196657e+01 -5.16165838e+03  4.86453399e+01]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “ValueError:matmul:输入操作数 1 在其核心维度 0 中存在不匹配……(大小 2 与 1 不同)” - “ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0 … (size 2 is different from 1)” 列向量乘法 ValueError 中的错误:matmul:输入操作数 1 在其核心维度 0 中不匹配 - Error in colmn vector multiplication ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0 matmul:输入操作数 1 的核心维度 0 不匹配,带有 gufunc 签名 - matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature ValueError:matmul:输入操作数 1 在其核心维度 0 中不匹配,带有 gufunc 签名 (n?,k),(k,m?)-&gt;(n?,m?) - ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)-&gt;(n?,m?) (size 1 is different from 2) - ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 2) ValueError: matmul: 输入操作数 1 在其核心维度 0 中存在不匹配,具有 gufunc 签名 (n?,k),(k,m?)-&gt;(n?,m?)(大小 1 与 3 不同) - ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 1 is different from 3) ValueError: matmul: 输入操作数 1 在其核心维度 0 中存在不匹配,具有 gufunc 签名 (n?,k),(k,m?)-&gt;(n?,m?)(大小 13 与 1 不同) - ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 13 is different from 1) matmul:输入操作数 1 在其核心维度 0 中存在不匹配,具有 gufunc 签名 (n?,k),(k,m?)-&gt;(n?,m?)(大小 20 与 10 不同) - matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 20 is different from 10) 我得到一个 matmul:输入操作数 1 不匹配...错误...正文中的完全错误 - I'm getting a matmul: input operand 1 has a mismatch...error...full error in body 为什么我不断收到此错误以及如何解决它 - why do i keep getting this error and how do i solve it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM