8.8 exercise 26

To create the Taylor series of the function \(f(x) = e^{-x}\) we utilize the Taylor series expansion formula for the exponential function. The Taylor series for \(e^x\) at \(x = 0\) is given by:

\[ e^x = \sum_{n=0}^{\infty} \frac{x^n}{n!} = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + ... \] To create the Taylor series for \(f(x) = e^{-x}\) replace \(x\) with \(-x\) \[ e^{-x} = \sum_{n=0}^{\infty} \frac{-x^n}{n!} = 1 - x + \frac{x^2}{2!} - \frac{x^3}{3!} + ... \] I wrote a function to generate the Taylor series

taylorSeries <- function(f){
  function(x){
    res <- 1
    n <- 1
    while(n <= f){
      c <- (((-x)^n) / factorial(n))
      res <- res + c
      n <- n + 1
    }
    res
  }
}