复杂数据的曲线拟合 [英] Curve fitting of complex data

查看:424
本文介绍了复杂数据的曲线拟合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用两个共享相同参数的函数来拟合复杂的数据集。为此,我使用

I want to fit complex data set with a two functions which shared the same parameters. For this I used

def funcReal(x,a,b,c,d):
    return np.real((a + 1j*b)*(np.exp(1j*k*x - kappa1*x) - np.exp(kappa2*x)) + (c + 1j*d)*(np.exp(-1j*k*x - kappa1*x) - np.exp(-kappa2*x)))

def funcImag(x,a,b,c,d):
    return np.imag((a + 1j*b)*(np.exp(1j*k*x - kappa1*x) - np.exp(kappa2*x)) + (c + 1j*d)*(np.exp(-1j*k*x - kappa1*x) - np.exp(-kappa2*x)))`

poptReal, pcovReal = curve_fit(funcReal, x, yReal)
poptImag, pcovImag = curve_fit(funcImag, x, yImag)

此处 funcReal 是模型的实部, funcImag 是虚部, yReal 是数据的实部, yImag 数据的虚部。

Here funcReal is the real part of my model, funcImag the imaginary part, yReal the real part of the data and yImag the imaginary part of the data.

但是,两者的拟合并不能为我提供相同的实数参数和虚构的部分。

However, both fits does not give me the same parameters for the real and imaginary part.

我的问题是有一个包或方法可以实现多个数据集和多个函数的多重拟合h个共享参数?

My question is there a package or a method such that I can realized multi fits for multiple data sets and multiple functions with shared parameters?

推荐答案

要同时满足上述复杂函数的要求,我们可以将实部和虚部都视为一个坐标点,或作为向量。由于 curve_fit 并不关心在数据 x (独立数据)和 y (依赖数据),我们可以简单地拆分复杂数据,并使用 hstack 堆叠实部和虚部。参见下面的示例。

To fit both the complex function given above, we can treat the real and imaginary components as a coordinate point, or as a vector. Since curve_fit doesn't care about the order at which data points are inserted in the vectors x (independent data) and y (dependent data), we can simply split the complex data and stack the real and imaginary components using hstack. See the example below.

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

kappa1 = np.pi
kappa2 = -0.01

def long_function(x, a, b, c, d):
    return (a + 1j*b)*(np.exp(1j*k*x - kappa1*x) - np.exp(kappa2*x)) + (c + 1j*d)*(np.exp(-1j*k*x - kappa1*x) - np.exp(-kappa2*x))

def funcBoth(x, a, b, c, d):
    N = len(x)
    x_real = x[:N//2]
    x_imag = x[N//2:]
    y_real = np.real(long_function(x_real, a, b, c, d))
    y_imag = np.imag(long_function(x_imag, a, b, c, d))
    return np.hstack([y_real, y_imag])

# Create an independent variable with 100 measurements
N = 100
x = np.linspace(0, 10, N)
# True values of the dependent variable
y = long_function(x, a=1.1, b=0.3, c=-0.2, d=0.23)
# Add uniform complex noise (real + imaginary)
noise = (np.random.rand(N) + 1j * np.random.rand(N) - 0.5 - 0.5j) * 0.1
yNoisy = y + noise

# Split the measurements into a  real and imaginary part
yReal = np.real(yNoisy)
yImag = np.imag(yNoisy)
yBoth = np.hstack([yReal, yImag])

# Find the best-fit solution
poptBoth, pcovBoth = curve_fit(funcBoth, np.hstack([x, x]), yBoth)

# Compute the best-fit solution
yFit = long_function(x, *poptBoth)
print(poptBoth)

# Plot the results
plt.figure(figsize=(9, 4))

plt.subplot(121)
plt.plot(x, np.real(yNoisy), "k.", label="Noisy y")
plt.plot(x, np.real(y), "r--", label="True y")
plt.plot(x, np.real(yFit), label="Best fit")
plt.ylabel("Real part of y")
plt.xlabel("x")
plt.legend()

plt.subplot(122)
plt.plot(x, np.imag(yNoisy), "k.")
plt.plot(x, np.imag(y), "r--")
plt.plot(x, np.imag(yFit))
plt.ylabel("Imaginary part of y")
plt.xlabel("x")

plt.tight_layout()
plt.show()

Re sult:

在此示例中找到的是 a = 1.14 b = 0.375 c = -0.236 d = 0.163 ,考虑到我在此处插入的噪声幅度,它们足够接近真实参数值。

The best-fit parameters that were found in this example were a = 1.14, b = 0.375, c = -0.236, and d = 0.163, which are close enough to the true parameter values given the amplitude of the noise that I inserted here.

这篇关于复杂数据的曲线拟合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆