scipy curve_fit 不拟合数据;没有抛出错误

发布于 2025-01-12 14:56:09 字数 2490 浏览 1 评论 0原文

Data to fit

我正在尝试使用以下函数(高斯与指数的卷积)来拟合我的数据(参见附图)衰变)。然而,无论我最初的猜测是什么,拟合效果都不好。我不知道为什么?有人可以帮忙吗?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
plt.rcParams["font.size"] = 16
plt.rcParams['figure.figsize'] = (10, 5)

#u is the location (mean) of Gaussian (determines the rise of feature)
#d is FWHM of the Gaussian (determines the width of convolution)

def one_exp_fit_function(t1, d, u, k1):
    d_bar = d/(2*np.sqrt(np.log(2)))
    i = ((d_bar*np.sqrt(2*np.pi))**(-1))*np.exp(-np.log(2)*(((2*(t1-u))/d)**2))
    f = np.exp(-(k1)*t1)
    k = np.convolve(i, f, mode='same')
    return k

p0 = [0.15, 908.1, 0.0029]


popt, pcov = curve_fit(one_exp_fit_function, Time, Intensity, p0)

输出:

>>> popt
array([2.36403281e-01, 9.08178303e+02, 2.68108346e-03])

>>>pcov
array([[ 1.30221168e-03,  7.18606249e-04, -5.22339673e-06],
   [ 7.18606249e-04,  4.48554164e-04, -3.15605374e-06],
   [-5.22339673e-06, -3.15605374e-06,  2.25596318e-08]])

拟合数据和原始数据如下所示: Fitted data

这是我试图拟合的数据(查看附图的外观) 时间
907.6, 907.61, 907.62, 907.63, 907.64, 907.65, 907.66, 907.67, 907.68, 907.69, 907.7, 907.71, 907.72, 907.73, 907.74, 907.75, 907.76, 907.77, 907.78, 907.79, 907.8, 907.81, 907.82, 907.83, 907.84, 907.85, 907.86, 907.87, 907.88, 907.89, 907.9, 907.91, 907.92, 907.93, 907.94, 907.95, 907.96, 907.97, 907.98, 907.99, 908.0, 908.01, 908.02, 908.03, 908.04, 908.05, 908.06, 908.07, 908.08, 908.09, 908.1, 908.11, 908.12, 908.13, 908.14, 908.15, 908.16, 908.17, 908.18, 908.19, 908.2, 908.21, 908.22, 908.23, 908.24

强度

0.057805967, 0.5065527, -0.3501974, 0.036141705, 0.035738964, 0.23803276, 0.6114219, -0.3501108, 0.07225589, 0.18216568, 0.25470826, 0.20328628, 0.091083646, 0.5306828, 0.87142694, 1.1959167, 1.2584509, 1.5770732, 1.5870125, 1.844547, 1.5854758, 2.0418897, 3.0503955, 2.5744607, 2.6155453, 2.9460716, 3.3898244, 3.5842943, 3.2935236, 3.3901217, 4.2156982, 3.4158673, 4.341926, 4.3702006, 3.9515905, 4.3101573, 4.2822328, 4.8445034, 4.2415953, 4.7157598, 4.856417, 4.842477, 4.6342535, 4.820195, 5.348257, 4.5149198, 4.8444476, 4.681948, 4.8084526, 4.7724895, 4.3835893, 4.362606, 4.529179, 4.8953743, 4.4109926, 4.924057, 5.0648174, 4.793385, 4.6049685, 4.6032906, 5.037164, 5.379293, 4.9395213, 4.7802463, 4.234617

Data to fit

I am trying to fit my data (see attached picture) using the following function (convolution of a Gaussian with exponential decays). However, the fit is not good no matter what my initial guess is. I'm not sure why? Can anyone help?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
plt.rcParams["font.size"] = 16
plt.rcParams['figure.figsize'] = (10, 5)

#u is the location (mean) of Gaussian (determines the rise of feature)
#d is FWHM of the Gaussian (determines the width of convolution)

def one_exp_fit_function(t1, d, u, k1):
    d_bar = d/(2*np.sqrt(np.log(2)))
    i = ((d_bar*np.sqrt(2*np.pi))**(-1))*np.exp(-np.log(2)*(((2*(t1-u))/d)**2))
    f = np.exp(-(k1)*t1)
    k = np.convolve(i, f, mode='same')
    return k

p0 = [0.15, 908.1, 0.0029]


popt, pcov = curve_fit(one_exp_fit_function, Time, Intensity, p0)

Output:

>>> popt
array([2.36403281e-01, 9.08178303e+02, 2.68108346e-03])

>>>pcov
array([[ 1.30221168e-03,  7.18606249e-04, -5.22339673e-06],
   [ 7.18606249e-04,  4.48554164e-04, -3.15605374e-06],
   [-5.22339673e-06, -3.15605374e-06,  2.25596318e-08]])

Here's how the fitted data and original data look like:
Fitted data

Here is the data I'm trying to fit (look at the attached image for how it looks)
Time
907.6,
907.61,
907.62,
907.63,
907.64,
907.65,
907.66,
907.67,
907.68,
907.69,
907.7,
907.71,
907.72,
907.73,
907.74,
907.75,
907.76,
907.77,
907.78,
907.79,
907.8,
907.81,
907.82,
907.83,
907.84,
907.85,
907.86,
907.87,
907.88,
907.89,
907.9,
907.91,
907.92,
907.93,
907.94,
907.95,
907.96,
907.97,
907.98,
907.99,
908.0,
908.01,
908.02,
908.03,
908.04,
908.05,
908.06,
908.07,
908.08,
908.09,
908.1,
908.11,
908.12,
908.13,
908.14,
908.15,
908.16,
908.17,
908.18,
908.19,
908.2,
908.21,
908.22,
908.23,
908.24

Intensity

0.057805967,
0.5065527,
-0.3501974,
0.036141705,
0.035738964,
0.23803276,
0.6114219,
-0.3501108,
0.07225589,
0.18216568,
0.25470826,
0.20328628,
0.091083646,
0.5306828,
0.87142694,
1.1959167,
1.2584509,
1.5770732,
1.5870125,
1.844547,
1.5854758,
2.0418897,
3.0503955,
2.5744607,
2.6155453,
2.9460716,
3.3898244,
3.5842943,
3.2935236,
3.3901217,
4.2156982,
3.4158673,
4.341926,
4.3702006,
3.9515905,
4.3101573,
4.2822328,
4.8445034,
4.2415953,
4.7157598,
4.856417,
4.842477,
4.6342535,
4.820195,
5.348257,
4.5149198,
4.8444476,
4.681948,
4.8084526,
4.7724895,
4.3835893,
4.362606,
4.529179,
4.8953743,
4.4109926,
4.924057,
5.0648174,
4.793385,
4.6049685,
4.6032906,
5.037164,
5.379293,
4.9395213,
4.7802463,
4.234617

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文