scipy 拟合从零开始到一结束的上升曲线

3vpjnl9f  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(135)

I have two curves and I want to fit two curves on each of them; I fitted the first one (a decaying curve starting from 1 and ends 0) using this function exp(-1/b *x). However, I have a problem with fitting the second curve which is a rising curve starting from zero and ending in 1. Can everyone please help me out with a function that can fit the second curve while I can calculate the relaxation time constant of my raising curve using that function?

Also the corresponded data are here: the first column is time, and the second and third columns are related to the decaying and raising curves, respectively.
Time Decaying Curve Raising Curve
0.0000 1.0000 0.0000 0.1000 0.9900 0.0100 0.2000 0.9802 0.0198 0.3000 0.9704 0.0296 0.4000 0.9608 0.0392 0.5000 0.9512 0.0488 0.6000 0.9418 0.0582 0.7000 0.9324 0.0676 0.8000 0.9231 0.0769 0.9000 0.9139 0.0861 1.0000 0.9048 0.0952 1.1000 0.8958 0.1042 1.2000 0.8869 0.1131 1.3000 0.8781 0.1219 1.4000 0.8694 0.1306 1.5000 0.8607 0.1393 1.6000 0.8521 0.1479 1.7000 0.8437 0.1563 1.8000 0.8353 0.1647 1.9000 0.8270 0.1730 2.0000 0.8187 0.1813 2.1000 0.8106 0.1894 2.2000 0.8025 0.1975 2.3000 0.7945 0.2055 2.4000 0.7866 0.2134 2.5000 0.7788 0.2212 2.6000 0.7711 0.2289 2.7000 0.7634 0.2366 2.8000 0.7558 0.2442 2.9000 0.7483 0.2517 3.0000 0.7408 0.2592 3.1000 0.7334 0.2666 3.2000 0.7261 0.2739 3.3000 0.7189 0.2811 3.4000 0.7118 0.2882 3.5000 0.7047 0.2953 3.6000 0.6977 0.3023 3.7000 0.6907 0.3093 3.8000 0.6839 0.3161 3.9000 0.6771 0.3229 4.0000 0.6703 0.3297 4.1000 0.6637 0.3363 4.2000 0.6570 0.3430 4.3000 0.6505 0.3495 4.4000 0.6440 0.3560 4.5000 0.6376 0.3624 4.6000 0.6313 0.3687 4.7000 0.6250 0.3750 4.8000 0.6188 0.3812 4.9000 0.6126 0.3874 5.0000 0.6065 0.3935 5.1000 0.6005 0.3995 5.2000 0.5945 0.4055 5.3000 0.5886 0.4114 5.4000 0.5827 0.4173 5.5000 0.5769 0.4231 5.6000 0.5712 0.4288 5.7000 0.5655 0.4345 5.8000 0.5599 0.4401 5.9000 0.5543 0.4457 6.0000 0.5488 0.4512 6.1000 0.5434 0.4566 6.2000 0.5379 0.4621 6.3000 0.5326 0.4674 6.4000 0.5273 0.4727 6.5000 0.5220 0.4780 6.6000 0.5169 0.4831 6.7000 0.5117 0.4883 6.8000 0.5066 0.4934 6.9000 0.5016 0.4984 7.0000 0.4966 0.5034 7.1000 0.4916 0.5084 7.2000 0.4868 0.5132 7.3000 0.4819 0.5181 7.4000 0.4771 0.5229 7.5000 0.4724 0.5276 7.6000 0.4677 0.5323 7.7000 0.4630 0.5370 7.8000 0.4584 0.5416 7.9000 0.4538 0.5462 8.0000 0.4493 0.5507 8.1000 0.4449 0.5551 8.2000 0.4404 0.5596 8.3000 0.4360 0.5640 8.4000 0.4317 0.5683 8.5000 0.4274 0.5726 8.6000 0.4232 0.5768 8.7000 0.4190 0.5810 8.8000 0.4148 0.5852 8.9000 0.4107 0.5893 9.0000 0.4066 0.5934 9.1000 0.4025 0.5975 9.2000 0.3985 0.6015 9.3000 0.3946 0.6054 9.4000 0.3906 0.6094 9.5000 0.3867 0.6133 9.6000 0.3829 0.6171 9.7000 0.3791 0.6209 9.8000 0.3753 0.6247 9.9000 0.3716 0.6284 10.0000 0.3679 0.6321

fiei3ece

fiei3ece1#

这个问题看起来相当微不足道--数据中的错误如此之少,以至于它看起来像是人为构造的,这是可疑的。这可能是出于演示的目的,也可能是在应用程序中“左手不与右手交谈”,参数没有正确地从一个地方传递到另一个地方。
假设你是真诚的,你的拟合基本上是“完美的”,你的曲线基本上是彼此精确的垂直反射;并且估计I显示基本上等于拟合参数:

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

time, decaying, raising = np.array((
    0.0000, 1.0000, 0.0000, 0.1000, 0.9900, 0.0100, 0.2000, 0.9802, 0.0198, 0.3000, 0.9704, 0.0296, 0.4000, 0.9608,
    0.0392, 0.5000, 0.9512, 0.0488, 0.6000, 0.9418, 0.0582, 0.7000, 0.9324, 0.0676, 0.8000, 0.9231, 0.0769, 0.9000,
    0.9139, 0.0861, 1.0000, 0.9048, 0.0952, 1.1000, 0.8958, 0.1042, 1.2000, 0.8869, 0.1131, 1.3000, 0.8781, 0.1219,
    1.4000, 0.8694, 0.1306, 1.5000, 0.8607, 0.1393, 1.6000, 0.8521, 0.1479, 1.7000, 0.8437, 0.1563, 1.8000, 0.8353,
    0.1647, 1.9000, 0.8270, 0.1730, 2.0000, 0.8187, 0.1813, 2.1000, 0.8106, 0.1894, 2.2000, 0.8025, 0.1975, 2.3000,
    0.7945, 0.2055, 2.4000, 0.7866, 0.2134, 2.5000, 0.7788, 0.2212, 2.6000, 0.7711, 0.2289, 2.7000, 0.7634, 0.2366,
    2.8000, 0.7558, 0.2442, 2.9000, 0.7483, 0.2517, 3.0000, 0.7408, 0.2592, 3.1000, 0.7334, 0.2666, 3.2000, 0.7261,
    0.2739, 3.3000, 0.7189, 0.2811, 3.4000, 0.7118, 0.2882, 3.5000, 0.7047, 0.2953, 3.6000, 0.6977, 0.3023, 3.7000,
    0.6907, 0.3093, 3.8000, 0.6839, 0.3161, 3.9000, 0.6771, 0.3229, 4.0000, 0.6703, 0.3297, 4.1000, 0.6637, 0.3363,
    4.2000, 0.6570, 0.3430, 4.3000, 0.6505, 0.3495, 4.4000, 0.6440, 0.3560, 4.5000, 0.6376, 0.3624, 4.6000, 0.6313,
    0.3687, 4.7000, 0.6250, 0.3750, 4.8000, 0.6188, 0.3812, 4.9000, 0.6126, 0.3874, 5.0000, 0.6065, 0.3935, 5.1000,
    0.6005, 0.3995, 5.2000, 0.5945, 0.4055, 5.3000, 0.5886, 0.4114, 5.4000, 0.5827, 0.4173, 5.5000, 0.5769, 0.4231,
    5.6000, 0.5712, 0.4288, 5.7000, 0.5655, 0.4345, 5.8000, 0.5599, 0.4401, 5.9000, 0.5543, 0.4457, 6.0000, 0.5488,
    0.4512, 6.1000, 0.5434, 0.4566, 6.2000, 0.5379, 0.4621, 6.3000, 0.5326, 0.4674, 6.4000, 0.5273, 0.4727, 6.5000,
    0.5220, 0.4780, 6.6000, 0.5169, 0.4831, 6.7000, 0.5117, 0.4883, 6.8000, 0.5066, 0.4934, 6.9000, 0.5016, 0.4984,
    7.0000, 0.4966, 0.5034, 7.1000, 0.4916, 0.5084, 7.2000, 0.4868, 0.5132, 7.3000, 0.4819, 0.5181, 7.4000, 0.4771,
    0.5229, 7.5000, 0.4724, 0.5276, 7.6000, 0.4677, 0.5323, 7.7000, 0.4630, 0.5370, 7.8000, 0.4584, 0.5416, 7.9000,
    0.4538, 0.5462, 8.0000, 0.4493, 0.5507, 8.1000, 0.4449, 0.5551, 8.2000, 0.4404, 0.5596, 8.3000, 0.4360, 0.5640,
    8.4000, 0.4317, 0.5683, 8.5000, 0.4274, 0.5726, 8.6000, 0.4232, 0.5768, 8.7000, 0.4190, 0.5810, 8.8000, 0.4148,
    0.5852, 8.9000, 0.4107, 0.5893, 9.0000, 0.4066, 0.5934, 9.1000, 0.4025, 0.5975, 9.2000, 0.3985, 0.6015, 9.3000,
    0.3946, 0.6054, 9.4000, 0.3906, 0.6094, 9.5000, 0.3867, 0.6133, 9.6000, 0.3829, 0.6171, 9.7000, 0.3791, 0.6209,
    9.8000, 0.3753, 0.6247, 9.9000, 0.3716, 0.6284, 10.0000, 0.3679, 0.6321
)).reshape((-1, 3)).T

def decay_model(x: np.ndarray, b: float) -> np.ndarray:
    return np.exp(-x/b)

def raise_model(x: np.ndarray, b: float) -> np.ndarray:
    return 1 - np.exp(-x/b)

b_est_decay = -time[-1] / np.log(decaying[-1])
b_est_raise = -time[-1] / np.log(1 - raising[-1])
(b_decay,), _ = curve_fit(f=decay_model, xdata=time, ydata=decaying, p0=b_est_decay)
(b_raise,), _ = curve_fit(f=raise_model, xdata=time, ydata=raising, p0=b_est_raise)
print('b (decay) =', b_decay)
print('b (raise) =', b_raise)

fig, ax = plt.subplots()
ax.plot(time, decaying, label='decay (experiment)')
ax.plot(time, raising, label='raise (experiment)')
ax.plot(time, decay_model(time, b_decay), label='decay (fit)', linestyle='--')
ax.plot(time, raise_model(time, b_raise), label='raise (fit)', linestyle='--')
ax.legend()
plt.show()

个字符
x1c 0d1x的数据

相关问题