我在试着解一个带有三个未知数的超定方程组。我可以在MATLAB中使用fsolve和lsqnonlin,通过for循环调用方程组来求解。
但是在使用scipy的python中,我得到了以下错误消息:
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有人能指出我哪里错了吗?提前谢谢你。
发布于 2014-03-15 03:32:59
异常意味着fnz()函数调用的结果与输入的g不具有相同的维度,输入的array是3个元素的列表,或者可以被视为形状(3,)的形状。
为了说明这个问题,如果我们定义:
def fnz(g):
return [2,3,5]
Anz = optimize.fsolve(fnz,g)不会有这样的例外。但这将会:
def fnz(g):
return [2,3,4,5]
Anz = optimize.fsolve(fnz,g)来自fnz()的结果应该与t1具有相同的长度,我确信它比3个元素更长。
https://stackoverflow.com/questions/22413454
复制相似问题