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.

1
  • It looks like you are trying to minimise the 3D function error? Commented Aug 30, 2012 at 15:42

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Oh, thanks. Still I would like an answer to - what method for to use for 2D minimization?
@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).
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
@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.
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.