Curve_fit 个实部和虚部数据
Curve_fit of real and imaginary data
我有一个长度为 640 的 x 数据数组和一个长度为 640 的 y 数据数组,其中每个值都有实部和虚部。我使用了 中的代码,其中我将 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,)
我该如何克服这个问题?
谢谢。
ISTM 最小二乘问题是您同时将数据的实部和虚部拟合到模型函数的实部和虚部。
(可以说)最直接的方法是使用 least_squares 并提供 returns 残差数组的成本函数。
编辑:这里几乎是重复的:Least squares in a set of equations with optimize.leastsq() (Python)
我有一个长度为 640 的 x 数据数组和一个长度为 640 的 y 数据数组,其中每个值都有实部和虚部。我使用了
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,)
我该如何克服这个问题?
谢谢。
ISTM 最小二乘问题是您同时将数据的实部和虚部拟合到模型函数的实部和虚部。 (可以说)最直接的方法是使用 least_squares 并提供 returns 残差数组的成本函数。
编辑:这里几乎是重复的:Least squares in a set of equations with optimize.leastsq() (Python)