3

I'm trying to solve a set of nonlinear equations using the dog-leg trust-region algorithm in Matlab and Python.

In Matlab there is fsolve where this algorithm is the default, whereas for Python we specify 'dogleg' in scipy.optimize.minimize. I won't need to specify a Jacobian or Hessian for the Matlab whereas Python needs either one to solve the problem.

I don't have the Jacobian/Hessian so is there a way around this issue for Python? Or is there another function that performs the equivalent of Matlab's dog-leg method in fsolve?

Medulla Oblongata
  • 3,771
  • 8
  • 36
  • 75

1 Answers1

2

In newer versions of scipy there is the approx_fprime function. It computes a numerical approximation of the jacobian of function f at position xk using the foward step finite difference. It returns an ndarray with the partial derivate of f at positions xk.

If you can't upgrade your version of scipy, you can always copy the implementation from scipy's source.


Edit:

scipy.optimize.minimize calls approx_fprime internally if the input jac=False. So in your case, it should be enough to do the following:

scipy.optimize.minimize(fun, x0, args, method='dogleg', jac=False)

Edit

scipy does not seem to handle the jac=False condition properly so it is necessary to build a callable jac using approx_fprime as follows

jac = lambda x,*args: scipy.optimize.approx_fprime(x,fun,epsilon,*args)
scipy.optimize.minimize(fun, x0, args, method='dogleg', jac=jac)
lucianopaz
  • 1,212
  • 10
  • 17
  • Yes, you are right! Thank you for making me notice. I edited my answer and also added a part stating that minimize calls `approx_fprime` internally if `jac=False` – lucianopaz Dec 08 '16 at 17:38
  • I get an error when I specify `jac=False`: `ValueError: Jacobian is required for dogleg minimization`. – Medulla Oblongata Dec 13 '16 at 10:24
  • It seems like an error in scipy's handling of the [numeric estimation](https://github.com/scipy/scipy/blob/v0.18.0/scipy/optimize/_minimize.py#L411-L416). The error persists in current versions of scipy so it seems like it is necessary to place a bug ticket or something else. To solve your specific problem, you will have to build a callable `jac` using a `approx_fprime`. I have edited my answer accordingly. – lucianopaz Dec 13 '16 at 12:38