在scipy.optimize中分配fmin的输出

Kur*_*eek 1 python mathematical-optimization scipy

我有一个单一变量的函数,我想找到最小值,以及变量的值,其中达到最小值.目前我通过以下Python脚本实现此目的:

import numpy as np
from scipy.optimize import fmin
import math

x1=0.
y1=800.
x2=1100.
y2=-800.

v1=2000.
v2=4000.

def T(xi):
    time=sqrt((x1-xi)**2+y1**2)/v1+sqrt((x2-xi)**2+y2**2)/v2
    return time

fmin(T,0)
Run Code Online (Sandbox Code Playgroud)

运行此脚本会生成以下回显:

import numpy as np
from scipy.optimize import fmin
import math

x1=0.
y1=800.
x2=1100.
y2=-800.

v1=2000.
v2=4000.

def T(xi):
    time=sqrt((x1-xi)**2+y1**2)/v1+sqrt((x2-xi)**2+y2**2)/v2
    return time

fmin(T,0)
Optimization terminated successfully.
         Current function value: 0.710042
         Iterations: 41
         Function evaluations: 82
Out[24]: array([ 301.9498125])
Run Code Online (Sandbox Code Playgroud)

所以函数的最小值是~0.71,并且对于~302的参数值可以得到.但是,我想按如下方式分配这些值:

(Tmin,xmin)=fmin(T,0)
Optimization terminated successfully.
         Current function value: 0.710042
         Iterations: 41
         Function evaluations: 82
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
C:\Users\Kurt.Peek\<ipython-input-25-aec613726d59> in <module>()
----> 1 (Tmin,xmin)=fmin(T,0)

ValueError: need more than 1 value to unpack
Run Code Online (Sandbox Code Playgroud)

所以我得到一个错误"ValueError:需要超过1个值来解包".有人知道如何防止这个错误并分配这两个输出吗?

unu*_*tbu 5

fmin有一个full_output=True参数:

xopt, fopt, iter, funcalls, warnflag = fmin(T,0, full_output=True, disp=False)
print(xopt, fopt)
# (array([ 301.9498125]), 0.71004171552448025)
Run Code Online (Sandbox Code Playgroud)