简体   繁体   中英

How to assign the Orthogonal Polynomials to functions?

Im trying to plot Laguerre's orthogonal polynomials for class assignment. I thought to create 10 functions, each function assigned to a specific polynomial by the index i.


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

and then use curve() to plot it. But it's not working. the laguerre.polynomials() function gives you the polynomials as a list and I think that the problem is that my loop can't extract items from a list by the index and assign it to the function.

Any ideas on how to do it?

You could convert the polynomials into functions using as.function , eg by

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

The i-th element of opl_functions is then the i-th polynomial, depending on x . This can then be used in order to plot the polynomials.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM