# [SciPy-user] non-linear multi-variate optimization

Chris Colbert sccolbert@gmail....
Sat Jul 18 12:08:00 CDT 2009

```I'm using fmin_l_bfgs_b

here's the code of my objective function, its the equation of a
superquadric with 11 free variables (a1, a2, a3, e1, e2, px, py, pz,
phi, theta, tsai) the variables (xw, yw, zw) are length N vectors
representing the world coordinates to which I'm fitting the
superquadric.  (i know i spelled psi wrong, i need to change it :) )

def superQuadricFit((a1, a2, a3, e1, e2, phi, theta, tsai, px, py, pz),

*args):

a1 = float(a1)

a2 = float(a2)

a3 = float(a3)

e1 = float(e1)

e2 = float(e2)

phi = float(phi)

theta = float(theta)

tsai = float(tsai)

px = float(px)

py = float(py)

pz = float(pz)

xw = args[0]

yw = args[1]

zw = args[2]

cphi = math.cos(phi)

ctheta = math.cos(theta)

ctsai = math.cos(tsai)

sphi = math.sin(phi)

stheta = math.sin(theta)

stsai = math.sin(tsai)

nx = cphi * ctheta * ctsai - sphi * stsai

ny = sphi * ctheta * ctsai + cphi * stsai

nz = -stheta * ctsai

ox = -cphi * ctheta * stsai - sphi * ctsai

oy = -sphi * ctheta * stsai + cphi * ctsai

oz = stheta * stsai

ax = cphi * stheta

ay = sphi * stheta

az = ctheta

f1 = ((nx * xw + ny * yw + nz * zw - px * nx - py * ny - pz * nz) / a1)

f2 = ((ox * xw + oy * yw + oz * zw - px * ox - py * oy - pz * oz) / a2)

f3 = ((ax * xw + ay * yw + az * zw - px * ax - py * ay - pz * az) / a3)

F = ((f1**2)**(1/e2) + (f2**2)**(1/e2))**(e2/e1) + (f3**2)**(1/e1)

err = (math.sqrt(a1 * a2 * a3) * (F**(e1) - 1))**2

sumerr = err.sum()

print err

return sumerr

So I would think the gradient should express the steepness of the
function wrt the 11 variables, and that steepness will be different at
every point (xw, yw, zw)_i . I cant see how to give any useful
gradient information in a single N-length vector.

Chris

On Sat, Jul 18, 2009 at 12:53 PM, Sebastian
Walter<sebastian.walter@gmail.com> wrote:
> What I've been trying to say is:
>
> The function you want to optimize must be of the form
>
> f: R^N -> R
>      x  -> y = f(x)
>
> i.e. mapping to the real numbers.
>
> then the derivative f'(x) is in R^N
> i.e. numpy.shape(f'(x)) == (N,)
>
>
> If you could be more precise on which optimization function you are
> trying to use and what your objective function looks like it would be
> easier to help.
>
>
> Sebastian
>
>
>
>
>
>
>
>
> On Sat, Jul 18, 2009 at 6:43 PM, Chris Colbert<sccolbert@gmail.com> wrote:
>> I'm not quite understanding what you're saying. According to this:
>> the Gradient and Jacobian are one in the same.
>>
>> what would be the form of the gradient in my case?
>>
>> Thanks for the help!
>>
>> Chris
>>
>> On Wed, Jul 15, 2009 at 3:32 AM, Sebastian
>> Walter<sebastian.walter@gmail.com> wrote:
>>> The gradient g is only defined for functions f: R^N --> R and is
>>> simply an array with shape (N,)
>>>
>>> what you sketched in your post is the Jacobian J of a function f: R^N --> R^M
>>> Typically, the Jacobian J is defined to have the shape (M,N), but
>>> there are exceptions.
>>>
>>> hope that helps a little
>>> Sebastian
>>>
>>>
>>>
>>> On Wed, Jul 15, 2009 at 2:00 AM, Chris Colbert<sccolbert@gmail.com> wrote:
>>>> The routines for non-linear optimization in scipy.optimize take an
>>>> argument for a function that computes the gradient.
>>>>
>>>> What should be the format of return value of this function? I am
>>>> assuming that its the gradient of the functions with repect to the
>>>> independent variables in row vector format.
>>>>
>>>> for example say we have:
>>>>
>>>> f(x,y,z; a1, a2, a3) where a1, a2, and a3 are the independent variables.
>>>>
>>>> Should the gradient of N x,y,z points then be of the form:
>>>>
>>>> df/da  =   [[df(X0)/da1, df(X0)/da2, df(X0)/da3],
>>>>               [df(X1)/da1, df(X1)/da2, df(X1)/da3],
>>>>               ....
>>>>               [df(Xn)/da1, df(Xn)/da2, df(Xn)/da3]]
>>>>
>>>> where Xn is the set of (xn, yn, zn) ?
>>>>
>>>> cheers!
>>>>
>>>> Chris
>>>> _______________________________________________
>>>> SciPy-user mailing list
>>>> SciPy-user@scipy.org
>>>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>>>
>>> _______________________________________________
>>> SciPy-user mailing list
>>> SciPy-user@scipy.org
>>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>>
>> _______________________________________________
>> SciPy-user mailing list
>> SciPy-user@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
```