gpt4 book ai didi

optimization - 使用 root 优化后,结果不是它们应该的样子

转载 作者:行者123 更新时间:2023-12-03 17:23:19 26 4
gpt4 key购买 nike

我一直在使用来自“from scipy.optimize import root”的root函数来解决其他需要两个方程f(x,y)和g(x,y)的问题,到目前为止我还没有直到现在发现任何障碍,
整个主题是势流,这个特殊问题是关于表面上的涡流+稳定速度,
下一个代码是关于在速度为零的情况下找到点 P, (Xp, YP) 的坐标,在表面上有一个涡流(涡流强度 = -550),并且这个涡流位于 a 的左侧墙。
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)

并且程序找到的结果中只有一个速度分量为零。
起初我以为是导数的问题,但我没有发现任何问题。我的一个 friend 曾经说过,当涡流强度过高时(例如超过 150),有时涡流的强度会表现出不同的表现。

补充资料:

这是流线图:

plot of the streamlines

使用此代码后:
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(这是绝对错误的)

当我将其更改为“hybr”时
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
>>>

我曾经得到过类似的东西,但我记不太清了,我认为在另一种情况下是因为手动对函数进行了错误的推导,在目前的问题中,我没有跟踪到这方面的任何错误。

最佳答案

您使用 method='lm' ,根据文档,它仅在最小二乘意义上求解方程。使用 method="hybr" , 你得到 sol.success == False .

很可能,您的 jacobian 是不正确的,因为在 jac=False 中找到了根.

编辑 :您的雅可比行列式似乎是错误的,至少:


x = np.array([3, 3.])
dx = np.array([1.3, 0.3])
eps = 1e-5
dx = 1e-5 * dx / np.linalg.norm(dx)

df_num = (np.array(fun_imp1(x + dx/2)[0]) - np.array(fun_imp1(x - dx/2)[0])) / eps
df_cmp = fun_imp1(x)[1].dot(dx)/eps

print df_num
print df_cmp

版画

[-1.43834392 -0.69055079]
[ -1.43834392 -1024.60208799]

始终根据数值微分检查雅可比矩阵非常有用。

关于optimization - 使用 root 优化后,结果不是它们应该的样子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19461602/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com