我一直在使用来自“from scipy.optimize import root”的根函数来解决需要两个方程 f(x,y) 和 g(x,y) 的其他问题,到目前为止我还没有到目前为止发现任何障碍,整个主题是势流,这个特殊问题是关于一个涡流+一个表面上的稳定速度,下一个代码是关于找到点 P 的坐标,(Xp,YP)速度为零,在表面上有一个涡流(涡流的强度 = -550),这个涡流位于墙壁的左侧。U : 稳定速度 cv : 涡流强度 h : 涡流与表面之间的距离
import numpy as np
from scipy.optimize import root
from math import pi
cv = -550.0
U = 10.0
h = 18.0
'''
denom1 = (X + h) ** 2 + Y ** 2
denom2 = (X - h) ** 2 + Y ** 2
###########################################
# f(x,y)
###########################################
f_1_a1 = - cv * Y / denom1
f_1_a2 = cv * Y / denom2
# f(x, y)
f_1 = f_1_a1 + f_1_a2
dfx_1 = (- cv * Y) * ((-1) * (2) * (X + h)) / (denom1 ** 2)
dfx_2 = (cv * Y) * ((-1) * (2) * (X - h)) / (denom2 ** 2)
# df_x
d_f_1_x = dfx_1 + dfx_2
dfy_1 = (- cv) / denom1
dfy_2 = (- cv * Y) * (- 1) * (2) * (Y) /(denom1 ** 2)
dfy_3 = (cv) / denom2
dfy_4 = (cv * Y) * (-1) * (2 * Y) /(denom2 ** 2)
# df_y
d_f_1_y = dfy_1 + dfy_2 + dfy_3 + dfy_4
###########################################
# g(x,y)
###########################################
g_a1 = - U
g_a2 = cv * (X + h) / denom1
g_a3 = - cv * (X - h) / denom2
# g(x, y)
f_2 = g_a1 + g_a2 + g_a3
dgx_1 = cv / denom1
dgx_2 = cv * (X + h) * (-1) * (2) * (X + h) / (denom1 ** 2)
dgx_3 = (- cv) / denom2
dgx_4 = (- cv) * (X - h) * (-1) * (2) * (X - h) / denom2
dgx = dgx_1 + dgx_2 + dgx_3 + dgx_3 + dgx_4
# dg_x
d_f_2_x = dgx
dgy_1 = cv * (X + h) * (-1) * (2) * (Y) / (denom1 ** 2)
dgy_2 = (- cv) * (X - h) * (-1) * (2 * Y) / (denom2 ** 2)
dgy = dgy_1 + dgy_2
# dg_y
d_f_2_y = dgy
'''
def Proof(X, Y):
denom1 = (X + h) ** 2 + Y ** 2
denom2 = (X - h) ** 2 + Y ** 2
###########################################
# f(x,y)
###########################################
f_1_a1 = - cv * Y / denom1
f_1_a2 = cv * Y / denom2
# f(x, y)
f_1 = f_1_a1 + f_1_a2
dfx_1 = (- cv * Y) * ((-1) * (2) * (X + h)) / (denom1 ** 2)
dfx_2 = (cv * Y) * ((-1) * (2) * (X - h)) / (denom2 ** 2)
# df_x
d_f_1_x = dfx_1 + dfx_2
dfy_1 = (- cv) / denom1
dfy_2 = (- cv * Y) * (- 1) * (2) * (Y) /(denom1 ** 2)
dfy_3 = (cv) / denom2
dfy_4 = (cv * Y) * (-1) * (2 * Y) /(denom2 ** 2)
# df_y
d_f_1_y = dfy_1 + dfy_2 + dfy_3 + dfy_4
###########################################
# g(x,y)
###########################################
g_a1 = - U
g_a2 = cv * (X + h) / denom1
g_a3 = - cv * (X - h) / denom2
# g(x, y)
f_2 = g_a1 + g_a2 + g_a3
dgx_1 = cv / denom1
dgx_2 = cv * (X + h) * (-1) * (2) * (X + h) / (denom1 ** 2)
dgx_3 = (- cv) / denom2
dgx_4 = (- cv) * (X - h) * (-1) * (2) * (X - h) / denom2
dgx = dgx_1 + dgx_2 + dgx_3 + dgx_3 + dgx_4
# dg_x
d_f_2_x = dgx
dgy_1 = cv * (X + h) * (-1) * (2) * (Y) / (denom1 ** 2)
dgy_2 = (- cv) * (X - h) * (-1) * (2 * Y) / (denom2 ** 2)
dgy = dgy_1 + dgy_2
# dg_y
d_f_2_y = dgy
print "The values of u and v are:"
print f_1
print f_2
print "The derivates are:"
print dgx, dgy
print d_f_1_x, d_f_1_y
def fun_imp1(x):
X = x[0]
Y = x[1]
denom1 = (X + h) ** 2 + Y ** 2
denom2 = (X - h) ** 2 + Y ** 2
###########################################
# f(x,y)
###########################################
f_1_a1 = - cv * Y / denom1
f_1_a2 = cv * Y / denom2
# f(x, y)
f_1 = f_1_a1 + f_1_a2
dfx_1 = (- cv * Y) * ((-1) * (2) * (X + h)) / (denom1 ** 2)
dfx_2 = (cv * Y) * ((-1) * (2) * (X - h)) / (denom2 ** 2)
# df_x
d_f_1_x = dfx_1 + dfx_2
dfy_1 = (- cv) / denom1
dfy_2 = (- cv * Y) * (- 1) * (2) * (Y) /(denom1 ** 2)
dfy_3 = (cv) / denom2
dfy_4 = (cv * Y) * (-1) * (2 * Y) /(denom2 ** 2)
# df_y
d_f_1_y = dfy_1 + dfy_2 + dfy_3 + dfy_4
###########################################
# g(x,y)
###########################################
g_a1 = - U
g_a2 = cv * (X + h) / denom1
g_a3 = - cv * (X - h) / denom2
# g(x, y)
f_2 = g_a1 + g_a2 + g_a3
dgx_1 = cv / denom1
dgx_2 = cv * (X + h) * (-1) * (2) * (X + h) / (denom1 ** 2)
dgx_3 = (- cv) / denom2
dgx_4 = (- cv) * (X - h) * (-1) * (2) * (X - h) / denom2
dgx = dgx_1 + dgx_2 + dgx_3 + dgx_3 + dgx_4
# dg_x
d_f_2_x = dgx
dgy_1 = cv * (X + h) * (-1) * (2) * (Y) / (denom1 ** 2)
dgy_2 = (- cv) * (X - h) * (-1) * (2 * Y) / (denom2 ** 2)
dgy = dgy_1 + dgy_2
# dg_y
d_f_2_y = dgy
a_1 = f_1
a_2 = f_2
b_1 = d_f_1_x
b_2 = d_f_1_y
c_1 = d_f_2_x
c_2 = d_f_2_y
f = [ a_1,
a_2]
df = np.array([[b_1, b_2],
[c_1, c_2]])
return f, df
sol = root(fun_imp1, [ 1, 1], jac = True, method = 'lm')
print "x = ", sol.x
print "x0 =", sol.x[1]
print "y0 =", sol.x[0]
x_1 = sol.x[0]
x_2 = sol.x[1]
Proof(x_1, x_2)
并且程序找到的结果只有速度的一个分量为零。起初我认为这是衍生品的问题,但我没有发现任何问题。我的一个朋友曾经说过,当涡流强度过高(比如超过 150)时,有时涡流的强度会以不同的方式表现。
补充资料:
这是流线图:
使用此代码后:
import numpy as np
import matplotlib.pyplot as plt
vortex_height = 18.0
h = vortex_height
vortex_intensity = -550.0
cv = vortex_intensity
permanent_speed = 10
U1 = permanent_speed
Y, X = np.mgrid[-21:21:100j, -21:21:100j]
U = (- cv * Y) / ((X + h)**2 + (Y ** 2)) + (cv * Y) / ((X - h)**2 + (Y ** 2))
V = - U1 + (cv * (X + h)) / ((X + h)**2 + (Y ** 2)) - (cv * (X - h)) / ((X - h)**2 + (Y ** 2))
speed = np.sqrt(U*U + V*V)
plt.streamplot(X, Y, U, V, color=U, linewidth=2, cmap=plt.cm.autumn)
plt.colorbar()
plt.savefig("stream_plot.png")
plt.show()
我用这个程序得到的结果是:
>>>
x = [ 1.32580109e-01 3.98170636e+02]
x0 = 398.170635755
y0 = 0.132580109151
The values of u and v are:
-8.2830922107e-05
-10.1246349802
The derivates are:
-2.20709329055 0.000624761030349
-0.000624761030349 6.22388943399e-07
>>>
u 和 v 应该在哪里:
u = 0.0
v = 0.0
代替 :
u = -8.2830922107e-05(这个可以接受) v = -10.1246349802(这个绝对错误)
当我将其更改为“混合”时
sol = root(fun_imp1, [ 1, 1], jac = True, method = 'hybr')
我明白了:
>>>
C:\Python27\lib\site-packages\scipy\optimize\minpack.py:221: RuntimeWarning: The iteration is not making good progress, as measured by the
improvement from the last ten iterations.
warnings.warn(msg, RuntimeWarning)
x = [ -4.81817071e+02 1.96057929e+06]
x0 = 1960579.2949
y0 = -481.817070593
The values of u and v are:
2.53176901102e-12
-10.0000000052
The derivates are:
-7.14899730857e-05 5.25462578799e-15
-5.25462578799e-15 -3.87401132188e-18
>>>
我曾经得到过类似的东西,但我记不太清了,我认为在另一种情况下是因为手工对函数的推导不正确,而在当前的问题中,我没有在这方面跟踪任何错误。