Approximate 1/x using simple functions
Purpose
To approximate using 1/x by eries of simple functions
> ks <- 1:5 > cols <- rainbow(length(ks)) > plot.new() > j <- 1 > for (j in seq_along(ks)) { + xlim <- c(0, 10) + ylim <- c(0, 20) + k <- ks[j] + x <- seq(0, 1, 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 <- 2 + for (i in 2:n.intervals) { + test <- c(intervals[i], intervals[(i + 1)]) + test <- sort(1/(test), decreasing = F) + condition <- z$x >= test[1] & z$x < test[2] + z[condition, 2] <- vals[i] + } + condition <- z$x < 1/(tail(intervals, 1)) + z[condition, 2] <- k + par(new = T) + plot(z$x, z$y, type = "l", xlim = c(0, 1), ylim = c(0, 20), + xlab = "", ylab = "", lwd = 2, col = cols[j]) + } > par(new = T) > x <- seq(0.1, 5, 0.1) > plot(x, 1/x, type = "l", xlim = c(0, 1), ylim = c(0, 20), col = "black", + main = expression(paste(y == 1/x, " 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 == 1/x)) > legend("topleft", legend = label, fill = c((cols), "black"), + cex = 1) |