4

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

where Tformulated is obtained as follows :

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

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)

I am not sure even if fmin_powell is the correct way to go.

Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
Harshad
  • 51
  • 1
  • 1
  • 4

1 Answers1

4

Here's a simplest example:

from scipy.optimize import fmin

def minf(x):
  return x[0]**2 + (x[1]-1.)**2

print fmin(minf,[1,2])

[out]:

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.

alvas
  • 115,346
  • 109
  • 446
  • 738
ev-br
  • 24,968
  • 9
  • 65
  • 78
  • Oh, thanks. Still I would like an answer to - what method for to use for 2D minimization? – Harshad Aug 30 '12 at 16:18
  • 1
    @user1636363 This is for 2D (`x` is a length 2 array), confusingly your function (error) is 3D and you haven't defined `E(n,k)`. – Andy Hayden Aug 30 '12 at 16:22
  • E(n,k) is the function written as "error x,y,w : ...." . Here the value of 'w' is fixed to an integer value before every time the function is called. (I actually pass an integer value to the function in place for 'w' every time I start the minimization). So, this is a 2D minimization problem; sorry for the confusion – Harshad Aug 30 '12 at 16:38
  • @user1636363: Which method to use is problem-dependent. Which one is the best in your particular case, I can't tell. What I would do, I'd start from the simplest one (`fmin`), and if it's not enough, then try more sophisticated ones. – ev-br Aug 30 '12 at 17:54
  • 1
    I am a bit confused about the descriptions of methods of the scipy.optimize modules; I can't figure out which ones are for the 2D minimizations – Harshad Aug 30 '12 at 18:25
  • @Harshad: they all accept list arguments, just like I've shown in a simplest example. – ev-br Aug 31 '12 at 10:22
  • @Zhenya:Thanks for the help, got it now – Harshad Oct 02 '12 at 05:55