python - ValueError: 操作数不能与形状 (100,) (99,) 一起在 Python 中广播错误
问题描述
我在 Python 中使用 bvp 求解器来解决 4 阶边值问题。没有显示实际方程以避免任何进一步的复杂性。我为此编写的代码已附在下面。
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,)
正如错误所暗示的那样,它之所以被提出是因为我试图对两个不同大小的数组执行一些数学运算。但我不明白为什么这里甚至会出现这个错误,考虑到上面定义的所有数组都具有相同的(100,)
. 对上述问题的任何修复将不胜感激。我已经被这个错误困扰了很长一段时间了。
PS - 我在上面的代码中定义的函数没有物理意义,完全是随机的。上面的代码只是我编写的相当复杂的代码的简化版本。因此,我不需要上述代码的正确数值解。我所需要的只是代码可以正常工作而不会出现任何错误。另外,我之前在 Python 中成功使用了 bvp 求解器。
解决方案
当我使用print(x.shape, y[0].shape)
then 时,两者都将大小更改为99
并且我会变薄,您应该使用它x.shape
来创建您的array
array = np.empty(x.shape,) # array is an 1-D array
这对我有用。但我不知道它是否给出了预期的结果。
BTW:后来我看到x.shape
,y[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)
推荐阅读
- java - javafx.scene.chart.NumberAxis 可以在没有图表的情况下使用吗?
- amazon-web-services - 如何仅轮换 iam 用户访问密钥而不轮换其密钥
- azure - 在 terraform 中使用“azurerm_virtual_machine”资源时,storage_os_disk 下的 Attach 和 FromImage 之间的区别
- java - java程序通过Windows控制台启动时不写入文件
- mysql - 为什么 JavaFX 在运行进程时会卡住?
- apache-kafka - 自定义 SMT(简单消息转换)
- android - 更新类路径 3.5.0 后。至 3.5.1
- c++ - 专用线程中 QApplication 中的输入小部件导致 QTimer 从错误的线程停止
- javascript - 数组的平均值
- javascript - Vue - 如何异步更新表数据