scipy.optimize.fsolve中的输入/输出错误 [英] input/output error in scipy.optimize.fsolve

查看:69
本文介绍了scipy.optimize.fsolve中的输入/输出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在scipy中使用root-finder时似乎出现错误.我想知道是否有人可以指出我在做错什么.
我正在寻找根源的功能只是一个简单的示例,并不是特别重要.

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.

如果我使用scipy 0.9.0运行此代码:

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

我收到以下错误消息:

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

推荐答案

看起来我在尝试错误地使用此例程.此例程需要相同数量的方程式和变量,而我给它提供了带有三个变量的一个方程式.因此,如果要最小化的函数的输入是3-D数组,则输出应该是3-D数组.这段代码有效:

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.

这篇关于scipy.optimize.fsolve中的输入/输出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆