简体   繁体   中英

input/output 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'm doing wrong.
The function I'm finding the root of is just an easy 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

I get the following error message:

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>'.

Well it looks like I was trying to use this routine incorrectly. This routine requires the same number of equations and variables vs. the one equation with three variables I gave it. So if the input to the function to be minimized is a 3-D array the output should be a 3-D 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 represents solving three equations simultaneously.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM