This week, we’ll work out some Taylor Series expansions of popular functions.
For each function, only consider its valid ranges as
indicated in the notes when you are computing the Taylor Series
expansion.
Please submit your assignment as an R- Markdown
document.
1. f (x) = (1−x)
f <- function(x) {
1 - x
}
x <- seq(-2, 2, by = 0.1)
y <- f(x)
plot(x, y, type = 'l', col = 'blue', main = "Plot of [f(x) = 1 - x] on its own", xlab = "x", ylab = "f(x)")
legend("topright", legend=c("f(x) = 1 - x"), col=c("blue"))
plot(x, y, type = 'l', col = 'blue', main = "Plot of [f(x) = 1 - x] with the Taylor expansion", xlab = "x", ylab = "f(x)")
lines(x, y, col = 'red')
legend("topright", legend=c("f(x) = 1 - x", "Taylor Expansion"), col=c("blue", "red"), lty=1, cex=0.8)
Both the function and its Taylor expansion are plotted. First plot shows the function plot only. The second plot is the plot of the function and its Taylor expansion but they look identical because the expansion is exact for all x. Which means the Taylor expansion is valid for all real numbers.
2. f (x) = e^x
f <- function(x) {
exp(x)
}
# Taylor approximation of e^x up to the 4th order
t_approx <- function(x) {
1 + x + (x^2/2) + (x^3/factorial(3)) + (x^4/factorial(4))
}
x <- seq(-2, 2, by = 0.1)
y <- f(x)
t_values <- t_approx(x)
plot(x, y, type = 'l', col = 'blue', main = "Plot of f(x) = e^x and its Taylor Expansion", xlab = "x", ylab = "y")
lines(x, t_values, type = 'l', col = 'red', lwd = 2)
legend("topright", legend=c("f(x) = e^x", "Taylor Expansion (4th Order)"), col=c("blue", "red"), lty=1, cex=0.8)
From this plot we see that the Taylor expansion of e^x to the 4th order approximates the actual function within the range (-2, 2). The approximation (red line) follows closely the function (blue line) especially near x = 0.
3. f (x) = ln(1 + x)
f <- function(x) {
log(1 + x)
}
# Taylor series approximation up to the 4th order
t_approx <- function(x) {
x - (x^2/2) + (x^3/3) - (x^4/4)
}
# valid range from -0.99 to 1
x <- seq(-0.99, 1, by = 0.01)
y <- f(x)
t_values <- t_approx(x)
plot(x, y, type = 'l', col = 'blue', main = "Plot of f(x) = ln(1 + x) and its Taylor Expansion", xlab = "x", ylab = "f(x)")
lines(x, t_values, type = 'l', col = 'red')
legend("topright", legend=c("f(x) = ln(1 + x)", "Taylor Expansion (4th Order)"), col=c("blue", "red"), lty=1, cex=0.8)
The plot shows us that the Taylor series expansion (red line) of ln(1+x) up to the fourth order provides a reasonably accurate approximation near x = 0, but the accuracy decreases significantly as x approaches -1, diverging from the actual function (blue line).
4. f(x)=x^(1/2)
f <- function(x) {
sqrt(x)
}
# Taylor series approximation of square root x around x = 1 up to the 3rd order
t_approx <- function(x) {
1 + 1/2*(x-1) - 1/8*(x-1)^2 + 1/16*(x-1)^3 + 1/32*(x-1)^4
}
x <- seq(0, 2, by = 0.01)
y <- f(x)
t_values <- t_approx(x)
plot(x, y, type = 'l', col = 'blue', main = "Plot of f(x) = sqrt(x) and its Taylor Expansion", xlab = "x", ylab = "f(x)")
lines(x, t_values, type = 'l', col = 'red')
legend("topright", legend=c("f(x) = sqrt(x)", "Taylor Expansion (3rd Order)"), col=c("blue", "red"), lty=1, cex=0.8)
The plot shows that the Taylor expansion (red line) of \(f(x) = \sqrt{x}\) around x = 1 provides a reasonable approximation near the expansion point but deviates from the actual function (blue line) as x moves towards zero, showing us the limited accuracy of the third-order expansion at lower values of x.