python scipy leastsq fit with complex numbers

假如想象 提交于 2019-12-02 07:51:05

The least squares function in scipy wants a real residual returned because it is difficult to compare complex values (e.g. is 1+2j greater or less than 2+1j?). Remember the residual is essentially a measure of the quality of the set of parameters passed in, it tells leastsq how close to the true fit it is.

What you can do is add the error (y-sim) in quadrature, appending these lines after you calculate 'sim' in your residuals function:

a = y-sim
return a.real**2 + a.imag**2

So long as y and sim are both np.array's of complex's then this will work and is relatively efficient.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!