如何在R中绘制用户定义的函数?

时间:2021-01-21 12:57:02

I am supposed to write a function for Stirling Numbers of the Second Kind, given by the formula:

我应该为第二类斯特林数写一个函数,由下式给出:

如何在R中绘制用户定义的函数?

For this, I have written the following function in R:

为此,我在R中编写了以下函数:

stirling <- function(n, k)
{
  sum = 0
  for (i in 0:k)
  {
    sum = sum + (-1)^(k - i) * choose(k, i) * i^n
  }
  sum = sum / factorial(k)
  return(sum)
}

The next part of the question is to "create a plot for n = 20, k = 1,2,...,10". I did some research and I think the methods curve or plot might help me. However, I am guessing these methods are used when y is of the form f(x) (i.e. a single argument). But here, I have two arguments (n and k) in my function stirling so I am not sure how to approach this.

问题的下一部分是“为n = 20创建一个图,k = 1,2,...,10”。我做了一些研究,我认为方法曲线或情节可能对我有帮助。但是,我猜这些方法是在y为f(x)形式时使用的(即单个参数)。但是在这里,我的函数中有两个参数(n和k),所以我不知道如何处理它。

Also, I tried converting the values of k (0, 1, 2..., 10) to a vector and then passing them to stirling, but stirling won't accept vectors as input. I am not sure how to modify the code to make stirling accept vectors.

此外,我尝试将k(0,1,2 ......,10)的值转换为矢量,然后将它们传递给斯特林,但斯特林将不接受矢量作为输入。我不知道如何修改代码来制作斯特林接受向量。

Any suggestions?

1 个解决方案

#1


4  

Vectorize

As pointed out in the comments, you can vectorize to do this:

正如评论中指出的那样,您可以执行以下操作:

Vectorize creates a function wrapper that vectorizes the action of its argument FUN. Vectorize(FUN, vectorize.args = arg.names, SIMPLIFY = TRUE, USE.NAMES = TRUE)

Vectorize创建一个函数包装器,用于向量化其参数FUN的动作。 Vectorize(FUN,vectorize.args = arg.names,SIMPLIFY = TRUE,USE.NAMES = TRUE)

(vstirling <- Vectorize(stirling))
# function (n, k) 
# {
# args <- lapply(as.list(match.call())[-1L], eval, parent.frame())
# names <- if (is.null(names(args))) 
#     character(length(args))
# else names(args)
# dovec <- names %in% vectorize.args
# do.call("mapply", c(FUN = FUN, args[dovec], MoreArgs = list(args[!dovec]), 
#    SIMPLIFY = SIMPLIFY, USE.NAMES = USE.NAMES))
# }

so vstirling() is the vectorized version of stirling().

所以vstirling()是斯特林()的矢量化版本。

vstirling(20, 1:10)
 # [1] 1.000000e+00 5.242870e+05 5.806064e+08 4.523212e+10 7.492061e+11 4.306079e+12 1.114355e+13 1.517093e+13
 # [9] 1.201128e+13 5.917585e+12

Now all that is left is creating a plot:

现在剩下的就是创建一个情节:

plot(x = 1:10, y = vstirling(20, 1:10), ylab = "S(20, x)", xlab = "x")

#1


4  

Vectorize

As pointed out in the comments, you can vectorize to do this:

正如评论中指出的那样,您可以执行以下操作:

Vectorize creates a function wrapper that vectorizes the action of its argument FUN. Vectorize(FUN, vectorize.args = arg.names, SIMPLIFY = TRUE, USE.NAMES = TRUE)

Vectorize创建一个函数包装器,用于向量化其参数FUN的动作。 Vectorize(FUN,vectorize.args = arg.names,SIMPLIFY = TRUE,USE.NAMES = TRUE)

(vstirling <- Vectorize(stirling))
# function (n, k) 
# {
# args <- lapply(as.list(match.call())[-1L], eval, parent.frame())
# names <- if (is.null(names(args))) 
#     character(length(args))
# else names(args)
# dovec <- names %in% vectorize.args
# do.call("mapply", c(FUN = FUN, args[dovec], MoreArgs = list(args[!dovec]), 
#    SIMPLIFY = SIMPLIFY, USE.NAMES = USE.NAMES))
# }

so vstirling() is the vectorized version of stirling().

所以vstirling()是斯特林()的矢量化版本。

vstirling(20, 1:10)
 # [1] 1.000000e+00 5.242870e+05 5.806064e+08 4.523212e+10 7.492061e+11 4.306079e+12 1.114355e+13 1.517093e+13
 # [9] 1.201128e+13 5.917585e+12

Now all that is left is creating a plot:

现在剩下的就是创建一个情节:

plot(x = 1:10, y = vstirling(20, 1:10), ylab = "S(20, x)", xlab = "x")