通过PCHIP功能进行集成 [英] Integrating over a PCHIP Function
问题描述
如何在R中的PCHIP(逐片三次Hermite插值多项式)函数上进行积分? pchip {pracma}返回内插的点数据,要集成,我们当然需要一个函数.我在pchip()的帮助菜单下看到待办事项:应提供一个'pchipfun'",我不知道手动生成将有多困难?还有其他建议吗?您可以将n次多项式回归拟合到插值点并将其积分以获得大致的近似值,但这很快就很混乱...
How can I integrate over a PCHIP (Piecewise Cubic Hermite Interpolation Polynomial) function in R? pchip {pracma} returns interpolated point data, and to integrate we of course need a function. I see under the help menu for pchip(), "TODO: A `pchipfun' should be provided," I don't know how hard this would be to generate manually? Any other suggestions? You could fit an nth degree polynomial regression to the interpolated points and integrate off that to get a rough approximation, but that gets messy pretty quick...
这是pchip {pracma}的源代码,它返回点而不是函数,我想返回函数更多的是数学问题,而不是R问题,但是我愿意接受任何建议!拜托!
Here's the source code for pchip {pracma} which returns points and not a function, I suppose returning a function is more of a math question not an R question, but I'm open for any and all suggestions! Please!
function (xi, yi, x)
{
h <- diff(xi)
delta <- diff(yi)/h
d <- .pchipslopes(h, delta)
n <- length(xi)
a <- (3 * delta - 2 * d[1:(n - 1)] - d[2:n])/h
b <- (d[1:(n - 1)] - 2 * delta + d[2:n])/h^2
k <- rep(1, length(x))
for (j in 2:(n - 1)) {
k[xi[j] <= x] <- j
}
s <- x - xi[k]
v <- yi[k] + s * (d[k] + s * (a[k] + s * b[k]))
return(v)
}
谢谢!
推荐答案
什么对您不起作用?您必须像这样使用 pchipfun()
定义函数:
What does not work for you? You have to define a function using pchipfun()
like this:
> library(pracma)
> xs <- linspace(0, pi, 10)
> ys <- sin(xs)
> pchipfun <- function(xi, yi) function(x) pchip(xi, yi, x)
> f <- pchipfun(xs, ys)
> integrate(f, 0, pi)
2.000749 with absolute error < 0.00017
我在R-Forge上更新了实践1.7.2,以包含pchipfun()
并向pchip()
添加了一些错误检查.
I have updated pracma 1.7.2 on R-Forge to include pchipfun()
and added some error checking to pchip()
.
这篇关于通过PCHIP功能进行集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!