2016-11-26 40 views
-1
#solve for U[i],sig[i] 

def f(x): 
    u=x[0] 
    v=x[1] 
    s1=0 #sum 
    s2=0 
    for j in range (N-i,N+i+1): 
     s1=s1+Qu[j,i]/(1+u*exp(v*(N-j)*sdt)*dt) 
     s2=s2+Qd[j,i]/(1+u*exp(v*(N-j)*sdt)*dt) 

    return [Pu[i+1]-s1,Pd[i+1]-s2] 

result=fsolve(f,[0,0]) 

TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument 'f'.Shape should be (2,) but it is (2, 1).形狀失配在python

所有其他參數是公知的號碼使用fsolve時(除U,V)。

以下是一個成功的例子,我認爲我的代碼具有相同的格式。

# success example 

def f(x):         
    u=x[0] 
    v=x[1] 
    return [u+v-4, u**2+v**2-8] 

result=fsolve(f,[0,0])         

他們有相同的格式,但第一個沒有工作。

在此先感謝。

+0

編輯'f(x)',以便它返回形狀(2,)或2個元素列表(不是嵌套列表)的數組。檢查形狀(2,)和(2,1)之間的差異。 – hpaulj

+0

感謝您的回答。但你能分辨兩個代碼之間的區別嗎?爲什麼第二個工作? – Qian

回答

0

隨着你的第二個f

In [596]: def f(x):         
    ...:  u=x[0] 
    ...:  v=x[1] 
    ...:  return [u+v-4, u**2+v**2-8] 
    ...: 

我給它一個2元素的列表,並取回一個2元素的列表

In [597]: f([0,0]) 
Out[597]: [-4, -8] 

其實fsolve將您x0成2單元陣列,並作爲一個數組來處理返回

In [598]: f(np.array([0,0])) 
Out[598]: [-4, -8] 

In [599]: np.array(f(np.array([0,0]))) 
Out[599]: array([-4, -8]) 
In [600]: _.shape 
Out[600]: (2,) 

因爲所有未定義的變量(曲,Pu等),我無法演示您的第一個f。我想我可以猜出合理的形狀,但我不喜歡這樣做。

但錯誤信息表明它產生類似:

In [601]: np.array([[1],[2]]) 
Out[601]: 
array([[1], 
     [2]]) 
In [602]: _.shape 
Out[602]: (2, 1) 

我可以模仿,與一點點改變你的第二f

In [606]: def f1(x):         
    ...:  u=x[0] 
    ...:  v=x[1] 
    ...:  return [[u+v-4], [u**2+v**2-8]] 
    ...: 
In [607]: optimize.fsolve(f1,[0,0]) 
--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-607-a21691ae31f8> in <module>() 
----> 1 optimize.fsolve(f1,[0,0]) 

/usr/lib/python3/dist-packages/scipy/optimize/minpack.py in fsolve(func, x0, args, fprime, full_output, col_deriv, xtol, maxfev, band, epsfcn, factor, diag) 
    144    'diag': diag} 
    145 
--> 146  res = _root_hybr(func, x0, args, jac=fprime, **options) 
    147  if full_output: 
    148   x = res['x'] 

/usr/lib/python3/dist-packages/scipy/optimize/minpack.py in _root_hybr(func, x0, args, jac, col_deriv, xtol, maxfev, band, eps, factor, diag, **unknown_options) 
    210  if not isinstance(args, tuple): 
    211   args = (args,) 
--> 212  shape, dtype = _check_func('fsolve', 'func', func, x0, args, n, (n,)) 
    213  if epsfcn is None: 
    214   epsfcn = finfo(dtype).eps 

/usr/lib/python3/dist-packages/scipy/optimize/minpack.py in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape) 
    38     msg += "." 
    39    msg += 'Shape should be %s but it is %s.' % (output_shape, shape(res)) 
---> 40    raise TypeError(msg) 
    41  if issubdtype(res.dtype, inexact): 
    42   dt = res.dtype 

TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument 'f1'.Shape should be (2,) but it is (2, 1). 

所以這是進行測試計算,東西如f(x0),並檢查返回內容的尺寸。並抱怨,當他們不符合其預期。

記住numpy數組可以有0,1,2或更多維數。相反,MATLAB的最小值爲2d。所以在numpy中,像(2,)這樣的形狀不同於(2,1)或(1,2)。它們都有2個元素,可以相互重構,但對於許多操作來說,維度的數量很重要。

[1,2],[[1],[2]][[1,2]]是等價的列表表達式。