gpt4 book ai didi

python - scipysolve_ovp 的数组维度错误

转载 作者:行者123 更新时间:2023-12-01 00:36:22 25 4
gpt4 key购买 nike

我正在尝试求解边界值 problem ,页码。 221,使用 Scipy solve_bvp。代码是

from scipy.integrate import solve_bvp

def fun(x, y):
tmp = np.vstack(( np.cos(y[2]), np.sin(y[2]), y[3], (y[4]*np.cos(y[2])), 0 ))
return tmp

def bc(ya, yb):
tmp = np.array([ ya[0], ya[1], ya[3], yb[1], yb[2]+(np.pi/2) ])
return tmp

x = np.linspace(0, 0.5, 400)
y = np.zeros((5, x.size))

sol = solve_bvp(fun, bc, x, y)

我收到错误

ValueError: all the input array dimensions except for the concatenation axis must match exactly

对来源有什么想法吗?一个简单的问题可以正常运行,不会出现错误

def fun(x, y):
tmp = np.vstack((y[1], -np.exp(y[0])))
print (tmp)
return tmp

def bc(ya, yb):
tmp = np.array([ya[0], yb[0]])
print (tmp)
return tmp

x = np.linspace(0, 1, 5)

y_1 = np.zeros((2, x.size))

from scipy.integrate import solve_bvp
res_1 = solve_bvp(fun, bc, x, y_1)

最佳答案

np.vstack(...,(y[4]*np.cos(y[2])), 0 )) 中的 0 是问题所在。它没有类型转换成正确的尺寸。

from scipy.integrate import solve_bvp

def fun(x, y):
tmp = np.vstack(( np.cos(y[2]), np.sin(y[2]), y[3], (y[4]*np.cos(y[2])), np.zeros(y[3].shape) ))
return tmp

def bc(ya, yb):
tmp = np.array([ ya[0], ya[1], ya[3], yb[1], yb[2]+(np.pi/2) ])
return tmp

x = np.linspace(0, 0.5, 400)
y = np.zeros((5, x.size))

sol = solve_bvp(fun, bc, x, y)

运行没有问题。

关于python - scipysolve_ovp 的数组维度错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57728451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com