任意阶多项式的函数(首选符号方法)

Function for polynomials of arbitrary order (symbolic method preferred)

我从我的数据中找到了多项式系数:

R <- c(0.256,0.512,0.768,1.024,1.28,1.437,1.594,1.72,1.846,1.972,2.098,2.4029)
Ic <- c(1.78,1.71,1.57,1.44,1.25,1.02,0.87,0.68,0.54,0.38,0.26,0.17)
NN <- 3
ft <- lm(Ic ~ poly(R, NN, raw = TRUE))
pc <- coef(ft)

所以我可以创建一个多项式函数:

f1 <- function(x) pc[1] + pc[2] * x + pc[3] * x ^ 2 + pc[4] * x ^ 3

例如,取导数:

g1 <- Deriv(f1)

如何创建一个通用函数,这样它就不必为每个新的多项式次数重写NN

我原来的答案可能不是你真正想要的,因为它是数字而不是象征性的。这是符号解决方案。

## use `"x"` as variable name
## taking polynomial coefficient vector `pc`
## can return a string, or an expression by further parsing (mandatory for `D`)
f <- function (pc, expr = TRUE) {
  stringexpr <- paste("x", seq_along(pc) - 1, sep = " ^ ")
  stringexpr <- paste(stringexpr, pc, sep = " * ")
  stringexpr <- paste(stringexpr, collapse = " + ")
  if (expr) return(parse(text = stringexpr))
  else return(stringexpr)
  }

## an example cubic polynomial with coefficients 0.1, 0.2, 0.3, 0.4
cubic <- f(pc = 1:4 / 10, TRUE)

## using R base's `D` (requiring expression)
dcubic <- D(cubic, name = "x")
# 0.2 + 2 * x * 0.3 + 3 * x^2 * 0.4

## using `Deriv::Deriv`
library(Deriv)

dcubic <- Deriv(cubic, x = "x", nderiv = 1L)
# expression(0.2 + x * (0.6 + 1.2 * x))

Deriv(f(1:4 / 10, FALSE), x = "x", nderiv = 1L)  ## use string, get string
# [1] "0.2 + x * (0.6 + 1.2 * x)"

当然,Deriv更容易获得高阶导数。我们可以简单地设置nderiv。然而,对于 D,我们必须使用递归(参见 ?D 的示例)。

Deriv(cubic, x = "x", nderiv = 2L)
# expression(0.6 + 2.4 * x)

Deriv(cubic, x = "x", nderiv = 3L)
# expression(2.4)

Deriv(cubic, x = "x", nderiv = 4L)
# expression(0)

如果我们使用表达式,我们将能够稍后计算结果。例如,

eval(cubic, envir = list(x = 1:4))  ## cubic polynomial
# [1]  1.0  4.9 14.2 31.3

eval(dcubic, envir = list(x = 1:4))  ## its first derivative
# [1]  2.0  6.2 12.8 21.8

以上暗示我们可以为函数包装一个表达式。使用函数有几个优点,一个是我们可以使用 curveplot.function.

来绘制它
fun <- function(x, expr) eval.parent(expr, n = 0L)

注意,fun 的成功要求 expr 是符号 x 的表达式。例如,如果 expr 是根据 y 定义的,我们需要用 function (y, expr) 定义 fun。现在让我们使用 curve 在范围 0 < x < 5:

上绘制 cubicdcubic
curve(fun(x, cubic), from = 0, to = 5)  ## colour "black"
curve(fun(x, dcubic), add = TRUE, col = 2)  ## colour "red"

最方便的方法,当然是定义一个单一的函数FUN而不是做f+fun组合。这样,我们也就不用担心ffun.

使用的变量名的一致性问题了。
FUN <- function (x, pc, nderiv = 0L) {
  ## check missing arguments
  if (missing(x) || missing(pc)) stop ("arguments missing with no default!")
  ## expression of polynomial
  stringexpr <- paste("x", seq_along(pc) - 1, sep = " ^ ")
  stringexpr <- paste(stringexpr, pc, sep = " * ")
  stringexpr <- paste(stringexpr, collapse = " + ")
  expr <- parse(text = stringexpr)
  ## taking derivatives
  dexpr <- Deriv::Deriv(expr, x = "x", nderiv = nderiv)
  ## evaluation
  val <- eval.parent(dexpr, n = 0L)
  ## note, if we take to many derivatives so that `dexpr` becomes constant
  ## `val` is free of `x` so it will only be of length 1
  ## we need to repeat this constant to match `length(x)`
  if (length(val) == 1L) val <- rep.int(val, length(x))
  ## now we return
  val
  }

假设我们要计算系数为 pc <- c(0.1, 0.2, 0.3, 0.4) 的三次多项式及其在 x <- seq(0, 1, 0.2) 上的导数,我们可以简单地做:

FUN(x, pc)
# [1] 0.1000 0.1552 0.2536 0.4144 0.6568 1.0000

FUN(x, pc, nderiv = 1L)
# [1] 0.200 0.368 0.632 0.992 1.448 2.000

FUN(x, pc, nderiv = 2L)
# [1] 0.60 1.08 1.56 2.04 2.52 3.00

FUN(x, pc, nderiv = 3L)
# [1] 2.4 2.4 2.4 2.4 2.4 2.4

FUN(x, pc, nderiv = 4L)
# [1] 0 0 0 0 0 0

现在绘图也很容易:

curve(FUN(x, pc), from = 0, to = 5)
curve(FUN(x, pc, 1), from = 0, to = 5, add = TRUE, col = 2)
curve(FUN(x, pc, 2), from = 0, to = 5, add = TRUE, col = 3)
curve(FUN(x, pc, 3), from = 0, to = 5, add = TRUE, col = 4)

由于我使用符号导数的最终解决方案最终太长,因此我使用单独的会话进行数值计算。对于多项式,我们可以这样做,导数是明确已知的,因此我们可以对它们进行编码。注意,这里不会使用 R 表达式;一切都直接通过函数完成。

所以我们先生成从0次到p - n次的多项式基,然后乘以系数和阶乘。这里用outerpoly方便

## use `outer`
g <- function (x, pc, nderiv = 0L) {
  ## check missing aruments
  if (missing(x) || missing(pc)) stop ("arguments missing with no default!")
  ## polynomial order p
  p <- length(pc) - 1L
  ## number of derivatives
  n <- nderiv
  ## earlier return?
  if (n > p) return(rep.int(0, length(x)))
  ## polynomial basis from degree 0 to degree `(p - n)`
  X <- outer(x, 0:(p - n), FUN = "^")
  ## initial coefficients
  ## the additional `+ 1L` is because R vector starts from index 1 not 0
  beta <- pc[n:p + 1L]
  ## factorial multiplier
  beta <- beta * factorial(n:p) / factorial(0:(p - n))
  ## matrix vector multiplication
  drop(X %*% beta)
  }

我们还是用符号解中定义的例子xpc

x <- seq(0, 1, by = 0.2)
pc <- 1:4 / 10

g(x, pc, 0)
# [1] 0.1000 0.1552 0.2536 0.4144 0.6568 1.0000

g(x, pc, 1)
# [1] 0.200 0.368 0.632 0.992 1.448 2.000

g(x, pc, 2)
# [1] 0.60 1.08 1.56 2.04 2.52 3.00

g(x, pc, 3)
# [1] 2.4 2.4 2.4 2.4 2.4 2.4

g(x, pc, 4)
# [1] 0 0 0 0 0 0

结果与符号解中FUN的结果一致。

同样,我们可以使用 curve:

绘制 g
curve(g(x, pc), from = 0, to = 5)
curve(g(x, pc, 1), from = 0, to = 5, col = 2, add = TRUE)
curve(g(x, pc, 2), from = 0, to = 5, col = 3, add = TRUE)
curve(g(x, pc, 3), from = 0, to = 5, col = 4, add = TRUE)

在演示了我们如何自己解决这个问题后,现在考虑使用 R 包 polynom。作为一个小包,它旨在实现单变量多项式的构造、导数、积分、算术和求根。本包完全用R语言编写,没有任何编译代码。

## install.packages("polynom")
library(polynom)

我们仍然考虑之前使用的三次多项式示例。

pc <- 1:4 / 10

## step 1: making a "polynomial" object as preparation
pcpoly <- polynomial(pc)
#0.1 + 0.2*x + 0.3*x^2 + 0.4*x^3 

## step 2: compute derivative
expr <- deriv(pcpoly)

## step 3: convert to function
g1 <- as.function(expr)

#function (x) 
#{
#    w <- 0
#    w <- 1.2 + x * w
#    w <- 0.6 + x * w
#    w <- 0.2 + x * w
#    w
#}
#<environment: 0x9f4867c>

注意,一步一步构造,得到的函数里面有所有的参数。它只需要 x 值的单个参数。相反,其他两个答案中的函数也将系数和导数阶数作为强制参数。我们可以调用这个函数

g1(seq(0, 1, 0.2))
# [1] 0.200 0.368 0.632 0.992 1.448 2.000

为了生成我们在其他两个答案中看到的相同图表,我们还得到了其他导数:

g0 <- as.function(pcpoly)  ## original polynomial

## second derivative
expr <- deriv(expr)
g2 <- as.function(expr)
#function (x) 
#{
#    w <- 0
#    w <- 2.4 + x * w
#    w <- 0.6 + x * w
#    w
#}
#<environment: 0x9f07c68>

## third derivative
expr <- deriv(expr)
g3 <- as.function(expr)
#function (x) 
#{
#    w <- 0
#    w <- 2.4 + x * w
#    w
#}
#<environment: 0x9efd740>

也许你已经注意到我没有指定nderiv,而是递归一次取1个导数。这可能是这个包的缺点。它不利于高阶导数。

现在我们可以画图了

## As mentioned, `g0` to `g3` are parameter-free
curve(g0(x), from = 0, to = 5)
curve(g1(x), add = TRUE, col = 2)
curve(g2(x), add = TRUE, col = 3)
curve(g3(x), add = TRUE, col = 4)