fsolve-输入和输出之间不匹配 [英] fsolve - mismatch between input and output
问题描述
我正在尝试解决带有三个未知数的方程组的问题.我可以通过for循环调用方程组,从而在MATLAB中使用fsolve和lsqnonlin获得解决方案.
I'm trying to solve an overdetmined system of equations with three unknowns. I'm able to get solution with fsolve and lsqnonlin in MATLAB by calling the system of equations through a for loop.
但是在使用scipy的python中,我收到以下错误消息:
But in python using scipy, I'm getting the following error message:
fsolve: there is a mismatch between the input and output shape of the 'func' argument 'fnz'
代码如下:
from xlrd import open_workbook
import numpy as np
from scipy import optimize
g = [0.5,1,1.5]
wb = open_workbook('EThetaValuesA.xlsx')
sheet=wb.sheet_by_index(0)
y=sheet.col_values(0,1)
t1=sheet.col_values(1,1)
t2=sheet.col_values(2,1)
t3=sheet.col_values(3,1)
def fnz(g):
i=0
sol=[0 for i in range(len(t1))]
x1 = g[0]
x2 = g[1]
x3 = g[2]
print len(t1)
for i in range(len(t1)):
# various set of t1,t2 and t3 gives the various eqns
print i
sol[i]=x1+t1[i]/(x2*t2[i]+x3*t3[i])-y[i]
return sol
Anz = optimize.fsolve(fnz,g)
print Anz
任何人都可以建议我错了吗?预先谢谢你.
Could anyone please suggest where I'm wrong? Thank you in advance.
推荐答案
该异常表示 fnz()
函数调用的结果与输入的 g
,它是3个元素的列表,或者可以看作是形状为(3,)
的 array
.
The exception means that the result from fnz()
function call does not has the same dimension as the input g
, which is a list of 3 elements, or can be seen as an array
of shape (3,)
.
为说明问题,如果我们定义:
To illustrate the problem, if we define:
def fnz(g):
return [2,3,5]
Anz = optimize.fsolve(fnz,g)
不会有这样的例外.但这会:
There will not be such an exception. But this will:
def fnz(g):
return [2,3,4,5]
Anz = optimize.fsolve(fnz,g)
fnz()
的结果应该与 t1
的长度相同,我敢肯定它的长度超过3个元素.
The result from fnz()
should have the same length as t1
, which I am sure is longer than 3 elements.
这篇关于fsolve-输入和输出之间不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!