1

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

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.