我正在尝试解决一个已知具有贝塞尔形状的边值问题。我的条件是u [0] = 1,u [40] = v [0] = v [40] = 0。但是,对于U,这给了我x = 1的平线。谁能告诉我我做错了什么?提前致谢。这是我的代码:
#Parameters
mu=0
eta = 1 #0.3
Dlt= 0.1 #.5
lbd= -1
alp= 1
Vz=1
def dU_dx(x, U):
# Let's try to make U as vector such that u=U[0],y=U[1],v=U[2] and z=U[3].
#This function should return [u',y',v', z']
return [U[1],
lbd*Dlt*U[2]+alp*(U[3]+U[2]/x)+(Vz-mu)*U[0] - U[1]*(1/x),
U[3],
(-lbd*Dlt*U[0]-alp*(U[1])+(-Vz-mu)*U[2] -U[3]*(1/x)+1/x**2*U[2])/eta]
def bc1(ya1,yb1):
return np.array([ya1[0]-1,yb1[0],ya1[1],yb1[1]-0.5])
#Define the initial mesh with 5 nodes:
x = np.linspace(0, 40, 5) #(0, 1, 10)
#This problem is known to have two solutions. To obtain both of them, we use two different initial guesses for y. We denote them by subscripts a and b.
U = np.zeros((4, x.size))
U[0] = 1
U[1] = 0
U[2] = 0
U[3] = 0.5
#Now we are ready to run the solver.
from scipy.integrate import solve_bvp
res_a = solve_bvp(dU_dx, bc1, x, U)
#Let’s plot the two found solutions. We take an advantage of having the solution in a spline form to produce a smooth plot.
x_plot = np.linspace(10**-6, 40, 1000)
y_plot_a = res_a.sol(x_plot)[0]
y_plot_b = res_a.sol(x_plot)[2]
import matplotlib.pyplot as plt
plt.plot(x_plot, y_plot_a, label='y_a')
plt.plot(x_plot, y_plot_b, label='y_b')
plt.legend()
plt.xlabel("x")
plt.ylabel("y")
plt.show()
我的代码基于scipy.integrate.solve_bvp网站上的示例。
我有警告,例如:
RuntimeWarning:除以true_divide中遇到的零
RuntimeWarning:在乘法运算中遇到无效的值
RuntimeWarning:在添加中遇到无效的值
答案 0 :(得分:1)
完整追溯的这一部分(您应该包括在内)
RuntimeWarning: invalid value encountered in true_divide
lbd * Dlt * U[2] + alp * (U[3] + U[2] / x) + (Vz - mu) * U[0] - U[1] * (1 / x),
RuntimeWarning: divide by zero encountered in true_divide
lbd * Dlt * U[2] + alp * (U[3] + U[2] / x) + (Vz - mu) * U[0] - U[1] * (1 / x),
...
为您提供了完美的指针:除以true_divide 中遇到的零(提示:... / x
)。
替换
x = np.linspace(0, 40, 5)
使用
x = np.linspace(1, 40, 5)
看看当您不尝试除以0时会发生什么。