我在为某个测量装置的测量误差建模。这就是data的外观:低频多项式上的高频正弦波纹。我的模型也能捕捉到涟漪。在
拟合误差的曲线应为:误差(x)=a0+a1*x+a2*x^2+。。。一个*x^n+Asin(x/lambda)。多项式的阶数是未知的。我的计划是从1-9迭代n并选择一个具有最高的F-value。在
到目前为止,我玩过numpy.polyfit
和{numpy.polyfit
仅适用于多项式,因此虽然我可以生成“最佳拟合”多项式,但无法确定正弦项的参数A和lambda。scipy.optimize.curve_fit
如果我已经知道了误差(x)多项式部分的多项式的阶数,那么它的效果会很好。在
有没有一种聪明的方法可以同时使用numpy.polyfit
和{
下面是我如何使用numpy.polyfit
选择最佳多项式的代码:
def GetErrorPolynomial(X, Y):
maxFval = 0.0
for i in range(1, 10): # i is the order of the polynomial (max order = 9)
error_func = np.polyfit(X, Y, i)
error_func = np.poly1d(error_func)
# F-test (looking for the largest F value)
numerator = np.sum(np.square(error_func(X) - np.mean(Y))) / i
denominator = np.sum(np.square(Y - error_func(X))) / (Y.size - i - 1)
Fval = numerator / denominator
if Fval > maxFval:
maxFval = Fval
maxFvalPolynomial = error_func
return maxFvalPolynomial
下面是我如何使用curve_fit
的代码:
它“硬编码”为二次函数,但我想选择“最佳”顺序,就像我上面用np.polyfit
所做的那样
使用Scikit-learn Linear Regression
下面是一个代码示例,我使用一个3次多项式执行线性回归,该多项式通过值为1和零导数的点0。您只需将create_vector函数与所需函数相适应。在
我终于找到了一种方法来模拟涟漪,并能回答我自己的问题。这个2006 paper对与我的数据集相似的波纹进行曲线拟合。在
首先,我做了一个最小二乘多项式拟合,然后从原始数据中减去这个多项式曲线。这只给我留下了涟漪。应用傅立叶变换,我选择了主频,让我重建正弦波纹。然后我简单地把这些波纹加到我刚开始得到的多项式曲线上。是这样的。在
我从散点图中提取数据进行分析,发现多项式+正弦似乎不是一个最佳模型,因为低阶多项式没有很好地遵循数据的形状,高阶多项式在数据极值处表现出Runge的高曲率现象。(“0.1)可能是(a)和(b)方程中的一个很好的(a)和(0.1)的峰值(b)的搜索结果。在
这是一个图形化的Python曲线拟合器,在文件的顶部我加载了我提取的数据,所以你需要用实际的数据替换它。该算法使用scipy的差分进化遗传算法模块来估计非线性fitter的初始参数值,该算法使用拉丁超立方体算法确保参数空间的彻底搜索,并要求搜索范围。在这里,这些界限是从数据的最大值和最小值获取的。在
从拟合曲线中减去模型预测值后,您将只剩下要建模的正弦分量。我注意到在x=275附近似乎还有一个额外的窄的低振幅峰值。在
更新- 如果高频正弦分量是常数(我不知道),那么仅用几个周期对一小部分数据进行建模就足以确定拟合模型正弦波部分的方程和初始参数估计。在这里,我做了这个,结果如下:
根据以下方程式:
^{pr2}$结合这两个模型使用实际数据,而不是我的散点图提取的数据,应该接近您需要的。在
相关问题 更多 >
编程相关推荐