我有一个用未知常量的整数来描述的数据集,我正尝试使用python的curve_fit来确定它。但是,被积函数包含一个与数据集相乘的函数
def integrand(tm, Pm, args):
dt, alpha1, alpha2 = args
return Pm*(1-np.e**( -(alpha1 * (tm+dt))) )*np.e**(-(alpha2 * (tm+dt)))其中Pm是脉冲响应、脉冲数据图像和积分曲线的采集数据的一维阵列

橙色曲线表示脉冲数据,蓝色曲线表示积分应计算的值。
tm是积分的变量,dt,alpha1,alpha2是未知常数,积分的范围是从0到tm。
对这类积分进行曲线拟合的最好方法是什么,或者可能是求解未知常数的其他方法?
发布于 2018-11-15 03:31:00
从数据集的长度来看,其目的似乎是将被积数(T)拟合到输出(t+dt)。scipy optimize模块中有几个函数可用于执行此操作。作为一个简单的例子,我们展示了一个使用scipy.optimize.leastsqr()的实现。有关更多详细信息,请参阅scipy optimize上的教程
基本方案是创建一个函数,该函数对独立坐标上的模型函数求值,并返回一个包含残差的数值数组,即模型和每个点的观测值之间的差。leastsq()找到一组参数的值,这些参数最小化残差的平方和。
我们注意到,作为警告,拟合可能对初始猜测敏感。模拟退火通常用于寻找可能的全局最小值,并在细化拟合之前提供拟合参数的粗略估计。此处用于初始猜测的值仅用于概念目的。
from scipy.optimize import leastsq
import numpy as np
# Read the data files
Pm = np.array( [ float(v) for v in open( "impulse_data.txt", "r" ).readlines() ] )
print type(Pm), Pm.shape
tm = np.array( [ float(v) for v in open( "Impulse_time_axis.txt", "r" ).readlines() ] )
print type(tm), tm.shape
output = np.array( [ float(v) for v in open( "Output_data.txt", "r" ).readlines() ] )
print type(output), output.shape
tout = np.array( [ float(v) for v in open( "Output_time_axis.txt", "r" ).readlines() ] )
print type(tout), tout.shape
# Define the function that calculates the residuals
def residuals( coeffs, output, tm ):
dt, alpha1, alpha2 = coeffs
res = np.zeros( tm.shape )
for n,t in enumerate(tm):
# integrate to "t"
value = sum( Pm[:n]*(1-np.e**( -(alpha1 * (tm[:n]+dt))) )*np.e**(-(alpha2 * (tm[:n]+dt))) )
# retrieve output at t+dt
n1 = (np.abs(tout - (t+dt) )).argmin()
# construct the residual
res[n] = output[n1] - value
return res
# Initial guess for the parameters
x0 = (10.,1.,1.)
# Run the least squares routine
coeffs, flag = leastsq( residuals, x0, args=(output,tm) )
# Report the results
print( coeffs )
print( flag )https://stackoverflow.com/questions/53306632
复制相似问题