# [SciPy-user] fprime for L-BFGS-B

Christian K ckkart@hoc....
Thu Apr 12 19:43:17 CDT 2007

```lechtlr wrote:
> Can anyone give some clues as to how to define gradient array (fprime) for optimize.fmin_l_bfgs_b.
>
> I have attached a simple parameter estimation example below to test fmin_l_bfgs_b with and without fprime. It works with approx_grad (i.e., optimize.fmin_l_bfgs_b(resid, p0, args=(y_meas,x), fprime=None, approx_grad=True). However, when I define, fprime it returns the initial guess. I suspect that I am not defining the fprime correctly.
>
> Any help would greatly be appreciated.
>
> -Lex
>
> from numpy import *
> from scipy import optimize
>
> def evalY(p, x):
>     return x**3 * p[3] + x**2 * p[2] + x * p[1] + p[0]
>
> # function to be minimized for parameter estimation
> def resid(p, y, x):
>     err = y - evalY(p, x)
>     return dot(err,err)
>
> # fprime for fmin_bfgs
> def func_der(p, y, x):
>
>     g = zeros(4,float)
>
>     g0 = 1.0
>     g1 = x
>     g2 = x**2
>     g3 = x**3
>
>     g[0] = g0
>     g[1] = dot(g1,g1)
>     g[2] = dot(g2,g2)
>     g[3] = dot(g3,g3)
>
>     return g

This returns the derivative of 'evalY', not that of 'resid'. It should be
something like:

return -g*2*(y-evalY(p, x).sum()

Christian

```