Home > Mobile >  How to assign the Orthogonal Polynomials to functions?
How to assign the Orthogonal Polynomials to functions?

Time:12-05

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?

CodePudding user response:

You could convert the polynomials into functions using as.function, e.g. 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.

  • Related