我有一个8个微分方程组,有8个边界条件,我试图用scipy的solve_bvp解决。 Bellow你可以找到我使用
的代码import numpy as np
from scipy.integrate import solve_bvp
def fun(x, y):
x_dot = y[2]
y_dot = y[1]
vx_dot = 53441.25/(4106.89-15.23*x)*(-y[6]/np.sqrt(y[6]**2+y[7]**2))
vy_dot = 53441.25/(4106.89-15.23*x)*(-y[7]/np.sqrt(y[6]**2+y[7]**2))-3.986e14/(y[1]+6371.0e3)**2
lambda1_dot = 0
lambda2_dot = 0
lambda3_dot = -y[4]
lambda4_dot = -y[5]
return np.vstack((x_dot, y_dot, vx_dot, vy_dot, lambda1_dot, lambda2_dot, lambda3_dot, lambda4_dot))
def bc(ya, yb):
return np.array([ya[0]-113021.9, yb[1]-500e3, yb[2]-7616.5, yb[3], yb[4], ya[5]-lambda2_0, ya[6]-lambda3_0, ya[7]-lambda4_0])
lambda1_0 = 0.0
lambda2_0 = 0.1
lambda3_0 = 0.1
lambda4_0 = 0.1
tf = 300
x = np.linspace(0, tf, tf*2)
y = np.zeros((8, x.size))
y[0] = 113021.9
y[1] = np.linspace(127734.0, 500.0e3, tf*2)
y[2] = np.linspace(1854.2, 7616.5, tf*2)
y[3] = np.linspace(1479.1, 0, tf*2)
y[4] = lambda1_0
y[5] = lambda2_0
y[6] = lambda3_0
y[7] = lambda4_0
res_a = solve_bvp(fun, bc, x, y)
然而,它看起来有问题,因为我收到以下错误消息: ValueError:除了连接轴之外的所有输入数组维度必须完全匹配
如何解决这个问题?
答案 0 :(得分:0)
您需要考虑
这一事实res_a = solve_bvp(fun, bc, x, y)
通过函数y
运行完整数组fun
,因此每个状态变量包含np.array
,用于同时进行多点评估。返回值必须与此任务兼容,除了初始化y_dot = np.zeros_like(y)
之外,实现该任务的最简单方法是通过
lambda1_dot = 0*y[4]
lambda2_dot = 0*y[5]