我正试图解决一个边界值问题,我知道它会有贝塞尔形状。我的条件是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: divide by zero encountered in true_divide
RuntimeWarning: invalid value encountered in multiply
RuntimeWarning: invalid value encountered in add
完整回溯的这一部分(您应该包括在内)
为您提供完美的指针:在true_divide中遇到被零除的情况(提示:
... / x
)替换
与
看看当你不尝试除以0时会发生什么
相关问题 更多 >
编程相关推荐