solve_bvp 没有给出正确的解决方案
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 站点的示例。
我有这样的警告:
RuntimeWarning: divide by zero encountered in true_divide
RuntimeWarning: invalid value encountered in multiply
RuntimeWarning: invalid value encountered in add
完整追溯的这一部分(您应该包括在内)
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 时会发生什么。
我正在尝试求解一个边值问题,我知道它具有类似贝塞尔的形状。我的条件是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
完整追溯的这一部分(您应该包括在内)
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 时会发生什么。