Approximation using Simple Functions
Purpose
To approximate using a series of simple functions
> ks <- 1:5 > cols <- rainbow(length(ks)) > plot.new() > j <- 1 > for (j in seq_along(ks)) { + xlim <- c(0, 5) + ylim <- c(0, 20) + k <- ks[j] + x <- seq(0, 5, 0.001) + z <- data.frame(x = x, y = 0) + n.intervals <- k * 2^k + intervals <- seq(0, k, 1/(2^k)) + ys <- 1:n.intervals + vals <- (ys - 1)/2^k + i <- 1 + for (i in 1:n.intervals) { + test <- c(intervals[i], intervals[(i + 1)]) + test <- sqrt(test) + condition <- z$x >= test[1] & z$x < test[2] + z[condition, 2] <- vals[i] + } + condition <- z$x >= sqrt(tail(intervals, 1)) + z[condition, 2] <- k + par(new = T) + plot(z$x, z$y, type = "l", xlim = c(0, 2), ylim = c(0, 2), + xlab = "", ylab = "", lwd = 2, col = cols[j]) + } > par(new = T) > x <- seq(0, 5, 0.1) > plot(x, x^2, type = "l", xlim = c(0, 2), ylim = c(0, 2), col = "black", + main = expression(paste(y == x^2, "approximation")), lty = "dashed", + lwd = 3, xlab = "", ylab = "") > label <- c(expression(paste(phi, 1)), expression(paste(phi, 2)), + expression(paste(phi, 3)), expression(paste(phi, 4)), expression(paste(phi, + 5)), expression(y == x^2)) > legend("topleft", legend = label, fill = c((cols), "black"), + cex = 1) |