Python中的二维优化(最小化)(使用scipy.optimize) [英] Two dimensional Optimization (minimization) in Python (using scipy.optimize)
问题描述
我正在尝试优化(最小化)如下定义的二维函数E(n,k)
:
I am trying to optimize (minimize) a two dimensional function E(n,k)
defined as follows:
error=lambda x,y,w: (math.log(abs(Tformulated(x,y,w))) - math.log(abs(Tw[w])))**2 + (math.atan2(Tformulated(x,y,w).imag,Tformulated(x,y,w).real) - math.atan2(Tw[w].imag,Tw[w].real))**2
其中Tformulated
的获取方式如下:
def Tformulated(n,k,w):
z=1j
L=1
C=0.1
RC=(w*L)/C
n1=complex(1,0)
n3=complex(1,0)
n2=complex(n,k)
FP=1/(1-(((n2-n1)/(n2+n1))*((n2-n3)/(n2+n3))*math.exp(-2*z*n2*RC)))
Tform=((2*n2*(n1+n3))/((n2+n1)*(n2+n3)))*(math.exp(-z*(n2-n1)*RC))*FP
return Tform
和Tw
是先前计算的具有复杂值元素的列表.
我确切想做的是针对w
的每个值(用于错误x,y,w ....").我想将x
&的值的函数错误"最小化. y
. w
的范围是1到2048.因此,它基本上是一个2D最小化问题.我曾经尝试过编程(尽管我被困在使用哪种方法以及如何使用它)上;我的代码如下:
and Tw
is a list previously calculated having complex valued elements.
What I am exactly trying to do is for each value of w
(used in "error x,y,w ....") I want to minimize the function "error" for the values of x
& y
. w
ranges from 1 to 2048. So, it is basically a 2D minimization problem. I have tried programming on my part (though I am getting stuck at what method to use and how to use it); my code is as follows :
temp=[]
i=range(5)
retval = fmin_powell(error , x ,y, args=(i) , maxiter=100 ,maxfun=100)
temp.append(retval)
即使fmin_powell
是正确的方法,我也不确定.
I am not sure even if fmin_powell
is the correct way to go.
推荐答案
这是最简单的示例:
from scipy.optimize import fmin
def minf(x):
return x[0]**2 + (x[1]-1.)**2
print fmin(minf,[1,2])
[输出]:
Optimization terminated successfully.
Current function value: 0.000000
Iterations: 44
Function evaluations: 82
[ -1.61979362e-05 9.99980073e-01]
这里可能的陷阱是最小化例程期望列表作为参数.有关所有详细信息,请参见文档.不知道是否可以直接最小化复数值函数,可能需要分别考虑实部和虚部.
A possible gotcha here is that the minimization routines are expecting a list as an argument. See the docs for all the gory details. Not sure if you can minimize complex-valued functions directly, you might need to consider the real and imaginary parts separately.
这篇关于Python中的二维优化(最小化)(使用scipy.optimize)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!