I / O error in scipy.optimize.fsolve

I seem to be getting an error when I use the root finder in scipy. I was wondering if anyone could point out what I am doing wrong.
The function I find at the root is just a light example and not particularly important.

If I run this code with scipy 0.9.0:

import numpy as np
from scipy.optimize import fsolve

tmpFunc = lambda xIn: (xIn[0]-4)**2 + (xIn[1]-5)**2 + (xIn[2]-7)**3

x0 = [3,4,5]
xFinal = fsolve(tmpFunc, x0 )

print xFinal

      

The following error message appears:

Traceback (most recent call last):
  File "tmpStack.py", line 7, in <module>
    xFinal = fsolve(tmpFunc, x0 )
  File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 115, in fsolve
    _check_func('fsolve', 'func', func, x0, args, n, (n,))
  File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 26, in _check_func
    raise TypeError(msg)
TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument '<lambda>'.

      

+1


source to share


1 answer


Well, it looks like I was trying to misuse this procedure. This procedure requires the same number of equations and variables compared to the one three-variable equation I gave him. Therefore, if the input for the function to be minimized is a 3D array, the output must be a 3D array. This code works:

import numpy as np
from scipy.optimize import fsolve

tmpFunc = lambda xIn: np.array( [(xIn[0]-4)**2 + xIn[1], (xIn[1]-5)**2 - xIn[2]) \
, (xIn[2]-7)**3 + xIn[0] ] )

x0 = [3,4,5]
xFinal = fsolve(tmpFunc, x0 )

print xFinal

      



Which is the solution of three equations at the same time.

+3


source







All Articles