在使用root优化后,结果不是它们应该是什么

时间:2013-10-19 02:14:30

标签: optimization python-2.7 scipy

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


添加信息:

这是精简的情节:

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
>>>

我有类似的东西,但我记不太清楚了,我认为在另一种情况下,由于手工推导功能不好,而在目前的问题中,我没有跟踪任何错误方面。

1 个答案:

答案 0 :(得分:3)

您使用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]

总是检查雅可比人对数字差异化是非常有用的。