1
我有一個複數的數據集,我希望能夠找到最適合數據的參數。你可以使用Python中的scipy實現的leastsq來適合複雜數據中的數據嗎?python scipy leastsq適合複數
例如,我的代碼是這樣的:
import cmath
from scipy.optimize import leastsq
def residuals(p,y,x):
L,Rs,R1,C=p
denominator=1+(x**2)*(C**2)*(R1**2)
sim=complex(Rs+R1/denominator,x*L-(R1**2)*x*C/denominator)
return(y-sim)
z=<read in data, store as complex number>
x0=np.array[1, 2, 3, 4]
res = leastsq(residuals,x0, args=(z,x))
然而,residuals
不喜歡和我的複數的工作,我得到的錯誤:
File "/tmp/tmp8_rHYR/___code___.py", line 63, in residuals
sim=complex(Rs+R1/denominator,x*L-(R1**_sage_const_2)*x*C/denominator)
File "expression.pyx", line 1071, in sage.symbolic.expression.Expression.__complex__ (sage/symbolic/expression.cpp:7112)
TypeError: unable to simplify to complex approximation
我猜我只需要使用浮動/雙打而不是複雜的數字。在那種情況下,我怎樣才能分別評估真實和複雜的零件,然後將它們歸併爲一個單一的錯誤指標,以便返回residuals
?