实和虚数据的曲线拟合

问题描述 投票:0回答:1

我有一个长度为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,)

我该如何克服这个问题?

谢谢。

python scipy curve-fitting
1个回答
0
投票

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

编辑:这是几乎重复的:Least squares in a set of equations with optimize.leastsq() (Python)

© www.soinside.com 2019 - 2024. All rights reserved.