【问题标题】:R: How to get piecewise coefficients of an interpolation spline for analytical integration?R:如何获得用于分析积分的插值样条的分段系数?
【发布时间】:2018-07-27 01:46:03
【问题描述】:

动机

我在数值上评估一个深度嵌套的多重积分。在每个嵌套级别,我得到下一层的积分向量,将其乘以密度函数向量,得到该级别的被积函数 y 的向量。 x 值的间距不均匀。

被积函数是弯曲的,梯形积分不够准确,所以我想做一个允许曲率的积分。辛普森规则不适用,因为横坐标不是均匀分布的。所以我建议做一个三次样条插值,然后通过解析计算每个段中三次的积分来计算样条函数的积分。

问题

我一直在研究 splinesplinefun 以及 splines2 包中的函数。但是我找不到任何可以告诉我三次多项式系列的系数的东西——每个节之间的一个段。

如果有人能指出一个函数,该函数可以进行样条插值并提供三次系数数组,我将不胜感激。

谢谢。

【问题讨论】:

    标签: r interpolation spline integral polynomials


    【解决方案1】:

    这是在这里扩展我的新答案的好机会:How to save and load spline interpolation functions in R? 使用分段参数化,很容易计算分段积分。

    这是一个用于计算的(矢量化)函数:

    ## a function for integration on a piece
    piecewise_int <- function (hi, yi, bi, ci, di) {
      yi * hi + bi * hi ^ 2 / 2 + ci * hi ^ 3 / 3 + di * hi ^ 4 / 4
      }
    

    下面,我会以那个线程中的小例子,展示如何整合样条。

    ## the small example in the linked thread
    set.seed(0)
    xk <- c(0, 1, 2)
    yk <- round(runif(3), 2)
    f <- splinefun(xk, yk, "natural")  ## natural cubic spline
    construction_info <- environment(f)$z
    
    ## information for integration
    int_info <- with(construction_info,
                     list(h = diff(x), y = y[-n], b = b[-n], c = c[-n], d = d[-n])
                     )
    
    ## cubic spline integration on all pieces
    integral <- sum(do.call(piecewise_int, int_info))
    #[1] 0.81375
    

    我们也可以进行数值积分来验证这个结果。

    integrate(f, 0, 2)
    #0.81375 with absolute error < 9e-15
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-20
      • 2018-01-17
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 2012-12-14
      相关资源
      最近更新 更多