Assignment Instructions

Compute the Taylor Series expansion of each function. Only consider the ranges indicated.

Taylor Polynomials

A Taylor series is a series expansion of a function about a point. If the real function \(f\) has \(n\) derivatives at point \(a\), then the \(n\)th Taylor polynomial for \(f\) at \(a\) is given below. If \(a=0\), the expansion is also known as a Maclaurin series.

\[{ P }_{ n }\left( x \right) =\sum _{ n=0 }^{ \infty }{ \frac { f^{ \left( n \right) }\left( a \right) { \left( x-a \right) }^{ n } }{ n! } }\]

Accuracy Evaluation Program

The following function takes the value of \(x\), the original function \(f(x)\), the Taylor approximation function \({ P }_{ n }\left( x \right)\), and the initial index value of \(n\) as inputs and then iterates through multiple powers of \(n\). It then outputs the results for comparison, plots the original function, and shows points the results of approximations for \(n=1,10\).

evaluate <- function(X, f, P, n_i) {
  summary <- data.frame(matrix(NA, 11, 4))
  colnames(summary) = c("x", "n", "f(x)", "P(x)")
  for(i in 1:11){
    n <- n_i + i - 1
    summary[i, 1] = X
    summary[i, 2] = n
    summary[i, 3] = eval(parse(text=gsub("X", X, f)))
    summary[i, 4] = eval(parse(text=paste0(P, "(", X, ",", n, ")")))
  }
  print(summary)
  par(mfrow=c(1, 2))
  curve(eval(parse(text = gsub("X", "x", f)), envir = list(x = x)),
        from = -1, to = 1, ylab = "f(x)", main = paste("Function =", f))
  y <- ifelse(n_i == 0, 2, 1)
  curve(eval(parse(text = gsub("X", "x", f)), envir = list(x = x)),
        from = -1, to = 1, ylab = "f(x)", 
        main = paste("Taylor Approximation at", X), 
        xlim = (c(X - 0.5, X + 0.5)), 
        ylim = (c(summary[y, 3] - 0.5, summary[y, 3] + 0.5)))
  points(X, summary[y, 4], col=2, pch=16)
  text(X + 0.25, summary[y, 4], "n=1")
  points(X, summary[(y + 9), 4], col=3, pch=16)
  text(X + 0.25, summary[(y + 9), 4], "n=10")  
}

Reciprocal Function

\[f\left( x \right) =\frac { 1 }{ 1-x } \approx \sum _{ n=0 }^{ \infty }{ { x }^{ n } } ,\quad x\in \left( -1,1 \right)\]

Taylor Approximation Function

reciprocal <- function(x, n) {
  sum(x^(0:n))
}

Taylor Approximation Accuracy

evaluate(0.5, "1 / (1 - X)", "reciprocal", 0)
##      x  n f(x)     P(x)
## 1  0.5  0    2 1.000000
## 2  0.5  1    2 1.500000
## 3  0.5  2    2 1.750000
## 4  0.5  3    2 1.875000
## 5  0.5  4    2 1.937500
## 6  0.5  5    2 1.968750
## 7  0.5  6    2 1.984375
## 8  0.5  7    2 1.992188
## 9  0.5  8    2 1.996094
## 10 0.5  9    2 1.998047
## 11 0.5 10    2 1.999023

Exponential Function

\[f\left( x \right) ={ e }^{ x } \approx \sum _{ n=0 }^{ \infty }{ \frac { { x }^{ n } }{ n! } } ,\quad x\in \mathbb{R}\]

Taylor Approximation Function

exponential <- function(x, n) {
  sum(x^(0:n) / factorial(0:n))
}

Taylor Approximation Accuracy

evaluate(0.5, "exp(X)", "exponential", 0)
##      x  n     f(x)     P(x)
## 1  0.5  0 1.648721 1.000000
## 2  0.5  1 1.648721 1.500000
## 3  0.5  2 1.648721 1.625000
## 4  0.5  3 1.648721 1.645833
## 5  0.5  4 1.648721 1.648438
## 6  0.5  5 1.648721 1.648698
## 7  0.5  6 1.648721 1.648720
## 8  0.5  7 1.648721 1.648721
## 9  0.5  8 1.648721 1.648721
## 10 0.5  9 1.648721 1.648721
## 11 0.5 10 1.648721 1.648721

Logarithmic Function

\[f\left( x \right) =\ln { \left( 1+x \right) } \approx \sum _{ n=0 }^{ \infty }{ { \left( -1 \right) }^{ n+1 }\frac { { x }^{ n } }{ n } } ,\quad x\in \left( -1,1 \right]\]

Taylor Approximation Function

logarithmic <- function(x, n) {
  sum((-1)^((1:n) + 1) * x^(1:n) / 1:n)
}

Taylor Approximation Accuracy

evaluate(0.5, "log(1 + X)", "logarithmic", 1)
##      x  n      f(x)      P(x)
## 1  0.5  1 0.4054651 0.5000000
## 2  0.5  2 0.4054651 0.3750000
## 3  0.5  3 0.4054651 0.4166667
## 4  0.5  4 0.4054651 0.4010417
## 5  0.5  5 0.4054651 0.4072917
## 6  0.5  6 0.4054651 0.4046875
## 7  0.5  7 0.4054651 0.4058036
## 8  0.5  8 0.4054651 0.4053153
## 9  0.5  9 0.4054651 0.4055323
## 10 0.5 10 0.4054651 0.4054346
## 11 0.5 11 0.4054651 0.4054790

References

http://s1.daumcdn.net/editor/fp/service_nc/pencil/Pencil_chromestore.html

http://mathworld.wolfram.com/TaylorSeries.html

http://www.statmethods.net/advgraphs/parameters.html

http://mathworld.wolfram.com/ExponentialFunction.html

http://people.math.sc.edu/girardi/m142/handouts/10sTaylorPolySeries.pdf

https://www.wolframalpha.com/input/?i=taylor+series+expansion+of+1%2F(1-x)

https://www.wolframalpha.com/input/?i=taylor+series+expansion+of+e%5Ex

https://www.wolframalpha.com/input/?i=taylor+series+expansion+of+ln(1%2Bx)