![](/img/trans.png)
[英]Python scipy fsolve “mismatch between the input and output shape of the 'func' argument”
[英]fsolve - mismatch between input and output
我正在嘗試用三個未知數來解決一個方程組過多的問題。 我可以通過for循環調用方程組,從而在MATLAB中使用fsolve和lsqnonlin獲得解決方案。
但是在使用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
有人可以建議我錯了嗎? 先感謝您。
異常意味着fnz()
函數調用的結果與輸入g
維數不同,后者是3個元素的列表,或者可以看作形狀(3,)
的array
。
為了說明這個問題,如果我們定義:
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個元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.