簡體   English   中英

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 站點的示例。

我有警告,例如:

運行時警告:在 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM