Taylor Series

f(x) = 1/(1−x)

f(x) = e^x

f(x) = ln(1 + x)

The general form of taylor series: f(x) = f(a) + ( f’(a)(x-a) )/1! + ( f’‘(a)(x-a) )/2! + ( f’’’(a)(x-a) )/3! + ….

or it can be written in: f(x) = sum ( ( f^n(a)/n!)*(x-a)^n )

about point a up to the order of n

and where n in f^n is number of time we take derivatives.

1. Taylor Series for f(x) = 1/(1-x) = (1-x)^(-1)

when x = 0, f(0) = 1.

f^1(x) = (-1)(-1)(1-x)^(-2) = 1 f^2(x) = (-2)(-1)(1-x)^(-3) = 2 f^3(x) = (-3)(-1)(1-x)^(-4) = 6

… f^n(x) = n!

therefore,
f(x) = sum ( (f^n(a)/n! ) * (x-a)^n ) = sum ( (n!/n!) * (x-a)^n ) f(x) = sum ( (x-a)^n ) = sum (x^n)

when x < 1, f(x) is converge.

#generate values of 1/(1-x) for N term
taylorExp_f <- function(x,n) { 
    s <- rep(0,n+1)
    for (i in 0:n) {
      s[i+1] <- x^i
    }
    
    return( s)
  }

acc_of_term <- function (s) {
  t <- rep(0, length(s))
  t[1] <- s[1]
  for( i in 2:length(s)) {
    t[i] <- t[i-1] + s[i]
  }
  return (t)
}

#list first 100th terms for n = 100 when x = 0.5
p <- taylorExp_f(0.5,100)

#generate the accumuation of the list
p_acc <- acc_of_term(p)

print(paste0("The sum of the taylor series f(x) = 1/(1-x): ", str(sum(p))) )
##  num 2
## [1] "The sum of the taylor series f(x) = 1/(1-x): "
plot(p_acc,  type = "l")
legend('topleft', inset=.05, legend= "The sum of the taylor series of f(x) = 1/(1-x)"
       )

For R, we can use pracma for taylor series https://www.rdocumentation.org/packages/pracma/versions/1.9.9/topics/taylor

library(pracma)

Plot the polynomial of 1/(1-x) vs taylor series

f <- function(x){
  return ( 1 / (1-x) )
}  

p <- taylor(f, 0, 4)


x <- seq(-1.0, 1.0, length.out=100)

yf <- f(x)
yp <- polyval(p, x) #poly of f(x)

plot(x, yf, type = "l", col = "gray", lwd = 3)
lines(x, yp, col = "red")
legend('topleft', inset=.05, legend= c("Taylor Series of f(x)", " Polynomail of f(x)=1/(1+x)")
       , lwd=c(2.5,2.5), col=c('gray', 'red'), bty='n', cex=.75)

2. Taylor Series for f(x) = e^x

when x = 0, f(0) = 1.

f^1(x) = e^x = 1 f^2(x) = e^x = 1 f^3(x) = e^x = 1

… f^n(x) = 1

therefore,
f(x) = sum ( (f^n(a)/n! ) * (x-a)^n ) = sum ( (1/n!) * (x-a)^n ) f(x) = sum ( (1/n!) * x^n )

using ratio test:

n+1th term over nth term

taking limit of n for ( 1/(1+n)! ) * x^(1+n) / (1/n!) * x^n = (1/n) * x^(1) = x/n

therefore x/n is getter smaller and smaller when n approaches to infinite

when x < 1, f(x) is converge.

Plot the f(x) = e^x

f2 <- function (n) {
   return (exp(n))
}

p2 <- taylor(f2, 0,4)

x2 <- seq(-1.0, 1.0, length.out=100)

yf2 <- f2(x2)
yp2 <- polyval(p2, x2) #poly of f(x)

plot(x2, yf2, type = "l", col = "gray", lwd = 3)
lines(x2, yp2, col = "red")
legend('topleft', inset=.05, legend= c("Taylor Series of f(x)", " Polynomail of f(x)=e^x")
       , lwd=c(2.5,2.5), col=c('gray', 'red'), bty='n', cex=.75)

3. Taylor Series for f(x) = ln(1 + x)

f(x) = ln(1 + x )

when x = 0, f(0) = ln(1) = 0

f’(x) = ( 1/(1+x) ) * (1), f’(0) = 1 f’‘(x) = (-1)* (1+x)^(-2), f’‘(0) = -1 f’‘’(x) = (-1)(-2)(1+x)^(-3), f’‘’(0) = 2 f’‘’’(x) = (2)(-3) (1+x)^(-4), = -6 f’‘’’’(x) = (-6)(-4)*(1+x)^(-5), = 24

therefore,
f(x) = sum ( (f^n(a)/n! ) * (x-a)^n ) = sum ( ( -1^(n-1) * (n-1)!)/n! * (x-a)^n ) f(x) = sum ( ( -1^(n-1)/n) * x^n )

using ratio test:

n+1th term over nth term

taking limit of n for ( -1^(n+2-1)/(n+2) ) * x^(n+2) / ( -1^(n+1-1)/ (n+1) ) * x^(n+1)

    = (-1)* ( (n+1)/(n+2) ) * x^(1)
    
    when n approaches to infinite,  (n+1)/(n+2) is smaller and smaller
    

therefore, f(x) is converge when x < 1

Plot the f(x) = ln(1 + x)

f3 <- function (n) {
   return (log(1 + n))
}

p3 <- taylor(f3, 0,4)

x3 <- seq(-1.0, 1.0, length.out=100)

yf3 <- f3(x3)
yp3 <- polyval(p3, x3) #poly of f(x)

plot(x3, yf3, type = "l", col = "gray", lwd = 3)
lines(x3, yp3, col = "red")
legend('topleft', inset=.05, legend= c("Taylor Series of f(x)", " Polynomail of f(x)=ln(1+x)")
       , lwd=c(2.5,2.5), col=c('gray', 'red'), bty='n', cex=.75)