【问题标题】:Curve_fit of real and imaginary data实部和虚部数据的曲线拟合
【发布时间】:2020-04-18 10:27:49
【问题描述】:

我有一个长度为 640 的 x 数据数组,其中只有实数数据,还有一个长度为 640 的 y 数据数组,其中每个值都有一个实部和虚部。我使用了this answer 中的代码,我将 yBoth 定义为:

yBoth = np.hstack([np.real(ydata),  np.imag(ydata)])

当我执行时,np.real(ydata)np.imag(ydata) 的长度都是 640:

from scipy.optimize import curve_fit
import numpy as np
def drag_fit_func(x, A, S, t, B):
    return A*np.exp(((x - t/2)/2*S)**2) + 1j*B*(A*((x - t/2)/S**2)*np.exp(((x - t/2)/2*S)**2))

def funcBoth(x, A, S, t, B):
    N = len(x)
    x_real = x[:N//2]
    x_imag = x[N//2:]
    y_real = np.real(drag_fit_func(x_real, A, S, t, B))
    y_imag = np.imag(drag_fit_func(x_imag, A, S, t, B))
    return np.hstack([y_real, y_imag])

yBoth = np.hstack([np.real(ydata),  np.imag(ydata)])
poptBoth, pcovBoth = curve_fit(funcBoth, xdata, yBoth)

print(poptBoth) 

我收到了这个错误:

ValueError: operands could not be broadcast together with shapes (640,) (1280,)

我该如何克服这个问题?

谢谢。

【问题讨论】:

  • 这是错字吗? I print(poptBoth)
  • @L.Clarkson 不,这不是我执行的错字:'print(poptBoth)',然后我得到了错误。
  • 啊,那是您链接的问题,我以为您正在打印 yBoth 数组
  • 是的 :) 我想要 poptBoth 值
  • 这是因为 x 数据有 640 个点,但 yBoth 列表的元素数量是 x 数据 1280 的两倍(每个 y 数据元素都有实值和虚值,因此值的数量是两倍)。复制 x 数据肯定会停止错误,但我不知道这是否会给你正确的 curve_fit 行为。

标签: python scipy curve-fitting


【解决方案1】:

ISTM 的最小二乘问题是您同时将数据的实部和虚部拟合到模型函数的实部和虚部。 (可以说)最直接的方法是使用 minimum_squares 并提供返回残差数组的成本函数。

编辑:这是一个近乎重复的内容:Least squares in a set of equations with optimize.leastsq() (Python)

【讨论】:

  • 谢谢@ev-br!实际上我没有得到完整的想法,如果可能的话,您能否详细说明一些代码?感谢您的帮助:)
猜你喜欢
  • 2020-10-17
  • 1970-01-01
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 2019-03-15
  • 1970-01-01
  • 2016-03-09
  • 2010-10-09
相关资源
最近更新 更多