繁体   English   中英

ValueError: 操作数无法与形状 (100,) (99,) 一起广播 Python

[英]ValueError: operands could not be broadcast together with shapes (100,) (99,) error in Python

我正在使用 Python 中的 bvp 求解器来求解四阶边值问题。 没有显示实际方程以避免任何进一步的复杂性。 我为此编写的代码已附在下面。

import numpy as np
from scipy.integrate import solve_bvp
import matplotlib.pyplot as plt

def xmesh(k1):      # xmesh definition
 return np.linspace(0,L,k1)

def solveit(constant):
   
   def fun(x,y):             # this function returns all the derivatives of y(x)
    array=np.empty(100,)     # array is an 1-D array
    array.fill(1)
    def array_function():
        return array     
    var= array_function()    # var is an 1-D array
    rhs= var+y[0]            # rhs is an 1-D array
    return [y[1],y[2],y[3],rhs]

   def bc(ya,yb):            # boundary conditions 
    return np.array([ya[0],ya[1],yb[0],yb[1]])
  
   init= np.zeros((4,len(xmesh(100))))       # initial value for the bvp solver
   sol= solve_bvp(fun,bc,xmesh(100),init,tol=1e-6,max_nodes=5000)
   arr= sol.sol(xmesh(100))[0] 
   return arr
arr= solveit(0.1)

每次尝试运行上述代码时,我都会遇到以下错误: operands could not be broadcast together with shapes (100,) (99,) 上述错误的堆栈跟踪也附在下面。

ValueError                                Traceback (most recent call last)
<ipython-input-52-62db777e3281> in <module>()
     24    arr= sol.sol(xmesh(100))[0]
     25    return arr
---> 26 arr= solveit(0.1)

6 frames
<ipython-input-52-62db777e3281> in solveit(constant)
     21 
     22    init= np.zeros((4,len(xmesh(100))))       # initial value for the bvp solver
---> 23    sol= solve_bvp(fun,bc,xmesh(100),init,tol=1e-6,max_nodes=5000)
     24    arr= sol.sol(xmesh(100))[0]
     25    return arr

/usr/local/lib/python3.6/dist-packages/scipy/integrate/_bvp.py in solve_bvp(fun, bc, x, y, p, S, fun_jac, bc_jac, tol, max_nodes, verbose, bc_tol)
   1084                                        fun_jac_wrapped, bc_jac_wrapped, x, h)
   1085         y, p, singular = solve_newton(n, m, h, col_fun, bc_wrapped, jac_sys,
-> 1086                                       y, p, B, tol, bc_tol)
   1087         iteration += 1
   1088 

/usr/local/lib/python3.6/dist-packages/scipy/integrate/_bvp.py in solve_newton(n, m, h, col_fun, bc, jac, y, p, B, bvp_tol, bc_tol)
    439     n_trial = 4
    440 
--> 441     col_res, y_middle, f, f_middle = col_fun(y, p)
    442     bc_res = bc(y[:, 0], y[:, -1], p)
    443     res = np.hstack((col_res.ravel(order='F'), bc_res))

/usr/local/lib/python3.6/dist-packages/scipy/integrate/_bvp.py in col_fun(y, p)
    324 
    325     def col_fun(y, p):
--> 326         return collocation_fun(fun, y, p, x, h)
    327 
    328     def sys_jac(y, p, y_middle, f, f_middle, bc0):

/usr/local/lib/python3.6/dist-packages/scipy/integrate/_bvp.py in collocation_fun(fun, y, p, x, h)
    311     y_middle = (0.5 * (y[:, 1:] + y[:, :-1]) -
    312                 0.125 * h * (f[:, 1:] - f[:, :-1]))
--> 313     f_middle = fun(x[:-1] + 0.5 * h, y_middle, p)
    314     col_res = y[:, 1:] - y[:, :-1] - h / 6 * (f[:, :-1] + f[:, 1:] +
    315                                               4 * f_middle)

/usr/local/lib/python3.6/dist-packages/scipy/integrate/_bvp.py in fun_p(x, y, _)
    648     if k == 0:
    649         def fun_p(x, y, _):
--> 650             return np.asarray(fun(x, y), dtype)
    651 
    652         def bc_wrapped(ya, yb, _):

<ipython-input-52-62db777e3281> in fun(x, y)
     14         return array
     15     var= array_function()    # var is an 1-D array
---> 16     rhs= var+y[0]            # rhs is an 1-D array
     17     return [y[1],y[2],y[3],rhs]
     18 

ValueError: operands could not be broadcast together with shapes (100,) (99,) 

正如错误所暗示的那样,它之所以被提出是因为我试图对两个不同大小的 arrays 执行一些数学运算。 但是我不明白为什么这里甚至会出现这个错误,因为上面定义的所有 arrays 都具有相同的(100,)形状。 对上述问题的任何修复将不胜感激。 我已经被这个错误困扰了很长一段时间了。

PS - 我在上面的代码中定义的函数没有物理意义,完全是随机的。 上面的代码只是我编写的相当复杂的代码的简化版本。 因此,我不需要上述代码的正确数值解。 我所需要的只是代码可以正常工作而不会出现任何错误。 另外,我之前在 Python 中成功使用了 bvp 求解器。

当我使用print(x.shape, y[0].shape)然后在某个时刻都将大小更改为99并且我瘦了你应该使用x.shape创建你的array

 array = np.empty(x.shape,) # array is an 1-D array

这对我有用。 但我不知道它是否给出了预期的结果。

顺便说一句:后来我看到x.shapey[0].shape甚至可以将大小更改为3564


import numpy as np
from scipy.integrate import solve_bvp
import matplotlib.pyplot as plt

L = 100

def xmesh(k1):
    """xmesh definition"""
    return np.linspace(0, L, k1)

def solveit(constant):
   
    def fun(x, y):             
        """returns all the derivatives of y(x)"""
        array = np.empty(x.shape,) # array is an 1-D array
        array.fill(1)
        rhs = array + y[0]         # rhs is an 1-D array
        return [y[1], y[2], y[3], rhs]
    
    def bc(ya, yb):
        """boundary conditions"""
        return np.array([ya[0], ya[1], yb[0], yb[1]])
      
    init = np.zeros((4, len(xmesh(100))))       # initial value for the bvp solver
    sol = solve_bvp(fun, bc, xmesh(100), init, tol=1e-6, max_nodes=5000)
    arr = sol.sol(xmesh(100))[0]
    
    return arr
    
arr = solveit(0.1)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM