如何积分线性插值函数?

2024-03-28 08:57:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个函数,它是一个相对大的数据集的插值。我使用线性插值interp1d,所以有很多像this这样的非光滑尖锐点。scipy中的quad函数将发出警告,因为存在尖锐的点。我想知道如何在没有警告的情况下进行集成?在

谢谢你!在


谢谢你的回答。在这里,我总结一些解决方法,以防其他人遇到同样的问题:

  1. 就像@Stelios所做的一样,使用points来避免警告并获得更准确的结果。在
  2. 实际上,点数通常大于quad的默认限制(limit=50),所以我选择quad(f_interp, a, b, limit=2*p.shape[0], points=p)来避免所有这些警告。在
  3. 如果a和{}不是数据集x的相同起点或终点,则p可以由p = x[where(x>=a and x<=b)]选择

Tags: 数据方法函数警告情况scipythispoints
1条回答
网友
1楼 · 发布于 2024-03-28 08:57:38

quad接受一个可选参数,称为points。根据文件:

points : (sequence of floats,ints), optional

A sequence of break points in the bounded integration interval where local difficulties of the integrand may occur (e.g., singularities, discontinuities). The sequence does not have to be sorted.

在您的例子中,“困难”points正是数据点的x坐标。下面是一个例子:

import numpy as np 
from scipy.integrate import quad
np.random.seed(123)

# generate random data set 
x = np.arange(0,10)  
y = np.random.rand(10)

# construct a linear interpolation function of the data set 
f_interp = lambda xx: np.interp(xx, x, y)

以下是数据点和f_interp的图: enter image description here

现在将quad调用为

^{2}$

返回一系列警告以及

(4.89770017785734, 1.3762838395159349e-05)

如果您提供points参数,即

quad(f_interp,0,9, points = x)

它不发出警告,结果是

(4.8977001778573435, 5.437539505167948e-14)

这也意味着与之前的调用相比,结果的准确性要高得多。在

相关问题 更多 >