【问题标题】:How to assign the Orthogonal Polynomials to functions?如何将正交多项式分配给函数?
【发布时间】:2022-01-10 12:32:06
【问题描述】:

我正在尝试绘制 Laguerre 的正交多项式以进行班级分配。我想创建 10 个函数,每个函数都由索引 i 分配给一个特定的多项式。


for (i in 1:10){
  opl[i] <- function(x) {opl[3]}
}

然后使用curve() 绘制它。 但它不起作用。 laguerre.polynomials() 函数将多项式作为列表提供给您,我认为问题在于我的循环无法通过索引从列表中提取项目并将其分配给函数。

有什么想法吗?

【问题讨论】:

    标签: r function math plot polynomials


    【解决方案1】:

    您可以使用as.function 将多项式转换为函数,例如由

    library(orthopolynom)
    library(ggplot2)
    
    opl <- laguerre.polynomials(10)
    
    opl_functions <- lapply(opl, as.function)
    
    # x interval
    x <- seq(-1, 1, 0.05)
    
    # plot the first two polynomials
    ggplot(data.frame(x), aes(x = x, y = y)) +      # basic graphical object
        geom_line(aes(y = opl_functions[[1]](x)), colour = "red") +  # first layer
        geom_line(aes(y = opl_functions[[2]](x)), colour = "blue") # second layer
    
    # and so on ...
    

    opl_functions 的第 i 个元素是第 i 个多项式,取决于 x。然后可以使用它来绘制多项式。

    【讨论】:

    • 谢谢!以及如何将它们全部绘制在同一个图中?
    • 这样的多层情节可以使用ggplot来实现。我扩展了我的答案。
    猜你喜欢
    • 2010-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多