[英]solve_bvp not giving the correct solution
我正在尝试解决一个边界值问题,我知道它具有类似贝塞尔的形状。 我的条件是 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 站点的示例。
我有警告,例如:
运行时警告:在 true_divide 中遇到除以零
运行时警告:乘法中遇到无效值
运行时警告:在添加中遇到无效值
完整回溯的这一部分(您应该包括在内)
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 时会发生什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.