获得两个超出其定义范围的自变量的连续模型

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

我有很多次times=[58418 58422 58424 58426 58428 58430 58540 58542 58650 58654 58656 58658 58660 58662 58664 58666 58668 58670 58672 58674 58768 58770 58772 58774 58776 58778 58780 58782 58784 58786 58788 58790 58792 58794 58883 58884 58886 58888 58890 58890 58892 58894 58896 58898 58904]

和相应的观察量y_obs =[ 0.00393986 0.00522288 0.00820794 0.01102782 0.00411525 0.00297762 0.00463183 0.00602662 0.0114886 0.00176694 0.01241464 0.01316199 0.01108201 0.01056611 0.0107585 0.00723887 0.0082614 0.01239229 0.00148118 0.00407329 0.00626722 0.01026926 0.01408419 0.02638901 0.02284189 0.02142943 0.02274845 0.01315814 0.01155898 0.00985705 0.00476936 0.00130343 0.00350376 0.00463576 -0.00610933 0.00286234 0.00845177 0.00849791 0.0151215 0.0151215 0.00967625 0.00802465 0.00291534 0.00819779 0.00366089]和相对错误:

y_obs_err = [6.12189334e-05 6.07487598e-05 4.66365096e-05 4.48781264e-05 5.55250430e-05 6.18699105e-05 6.35339947e-05 6.21108524e-05 5.55636135e-05 7.66087180e-05 4.34256323e-05 3.61131000e-05 3.30783270e-05 2.41312040e-05 2.85080015e-05 2.96644612e-05 4.58662869e-05 5.19419065e-05 6.00479888e-05 6.62586953e-05 3.64830945e-05 2.58120956e-05 1.83249104e-05 1.59433858e-05 1.33375408e-05 1.29714326e-05 1.26025166e-05 1.47293107e-05 2.17933175e-05 2.21611713e-05 2.42946630e-05 3.61296843e-05 4.23009806e-05 7.23405476e-05 5.59390368e-05 4.68144974e-05 3.44773949e-05 2.32907036e-05 2.23491451e-05 2.23491451e-05 2.92956472e-05 3.28665479e-05 4.41214301e-05 4.88142073e-05 7.19116984e-05]

我定义了一个函数,该函数根据时间计算y的值,许多参数,以及另一个自变量

p= [ 2.82890497 3.75014266 5.89347542 7.91821558 2.95484056 2.13799544 3.32575733 4.32724456 8.2490644 1.26870083 8.91397925 9.45059128 7.95712563 7.58669608 7.72483557 5.19766853 5.93186433 8.89793105 1.06351782 2.92471065 4.49999613 7.37354766 10.11275281 18.94787684 16.40097363 15.38679306 16.33387783 9.44782842 8.29959664 7.07757293 3.42450524 0.93588962 2.515773 3.32857547 7.180216 2.05522399 6.06855409 6.1016838 10.8575614 10.8575614 6.94775991 5.76187014 2.09327787 5.88619335 2.62859611]

这里定义函数来计算y变量:

import numpy as np
import matplotlib.pyplot as plt

from lmfit import Model
import scipy.integrate as it
import scipy.constants as scc
def new_f_function(t, sum, f0, a, b,c, T0):
    n= 2 * np.pi * sum 

    obs_f = f0 + it.cumtrapz(-a * p**c + b, t-T0, initial=0)
    new_f = obs_f*(1+sum/scc.c)
    return new_f

我创建一个模型,并用第一次猜测来初始化我的参数:

# Create Model
model = Model(p_obs, independent_vars=['t'])

# Initialize Parameter
params = model.make_params()

params['sum'].value = 1.483 
params['sum'].min = 1.47
params['sum'].max = 1.50

.... # and so on for the others

然后我适合模型

`result = model.fit(y, params, weights=(1./y_err)), t=times, scale_covar=False)`

以获得最佳拟合参数result.best_fit。最后,我可以用

绘制最合适的图

fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(3, 6), sharex='all') ax2.plot(times, result.best_fit, label='best fit')

我的问题是:如何在未定义变量p的位置(例如,我没有数据)绘制/定义函数?我想问题类似于这里的问题:Scipy curve_fit: how to plot the fitted curve beyond the data points?

但是在函数具有两个自变量的情况下,尽管我可以为x轴变量定义一个额外的数据范围,也就是时间,但是对于观察到的变量p,我不能做同样的事情。

python matplotlib curve-fitting lmfit
3个回答
0
投票
在该函数具有两个自变量的情况下,尽管我可以为x轴变量定义一个额外的数据范围,也就是时间,但是我不能对观察到的变量p做同样的事情。

如果您有函数f(x,y)= z,则有两个独立的输入变量x和y和一个输出变量(如果我是对的,这是您观察到的变量。)>

您可以通过创建一个2d数组(根据需要,与拟合之前的时间无关)来绘制此图,将每个单点传递给函数并绘制所有已校准的z。显然,这不能在正常的x-y图中绘制。一种方法是3d图或2d彩色图。

不确定我是否正确。


0
投票
您的model对象存储了您适合的函数,并且该适合的最佳参数存储在result.best_values属性中。

0
投票
我认为您要求在除用于拟合的那些点之外的其他点上评估最佳拟合模型。 FWIW,这是一件好事,需要在拟合范围之外进行内插或外推。
© www.soinside.com 2019 - 2024. All rights reserved.