# NOT RUN {
# }
# NOT RUN {
# visualize the bernstein polynomials
library(ggplot2); theme_set(theme_bw())
library(reshape2)
f <- function(x) sin(2*pi*x)
p <- bernstein_approx(f, 20)
round(p, 3)
x <- seq(0, 1, length.out = 101)
df <- data.frame(
x = rep(x, 2),
y = c(f(x), as.function(p)(x)),
which = rep(c("actual", "approx"), each = 101)
)
qplot(x, y, data = df, geom = "line", color = which)
p <- bernstein_approx(sin, 20, pi/2, 1.5*pi)
round(p, 4)
x <- seq(0, 2*pi, length.out = 101)
df <- data.frame(
x = rep(x, 2),
y = c(sin(x), as.function(p)(x)),
which = rep(c("actual", "approx"), each = 101)
)
qplot(x, y, data = df, geom = "line", color = which)
p <- bernstein_approx(dnorm, 15, -1.25, 1.25)
round(p, 4)
x <- seq(-3, 3, length.out = 101)
df <- data.frame(
x = rep(x, 2),
y = c(dnorm(x), as.function(p)(x)),
which = rep(c("actual", "approx"), each = 101)
)
qplot(x, y, data = df, geom = "line", color = which)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab