Integrating over a PCHIP Function

陌路散爱 提交于 2019-12-13 07:34:16

问题


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...

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)
}

Thanks!


回答1:


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

I have updated pracma 1.7.2 on R-Forge to include pchipfun() and added some error checking to pchip().



来源:https://stackoverflow.com/questions/25478393/integrating-over-a-pchip-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!