ASSIGNMENT 14 - TAYLOR SERIES

QUESTION

This week, we’ll work out some Taylor Series expansions of popular functions.

  • \(f(x) = \frac{1}{1-x}\)
  • \(f(x) = e^x\)
  • \(f(x) = ln(1 + x)\)

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 a R-Markdown document.

ANSWER

Taylor’s Theorem states that any function that is infinitely differentiable can be represented as a polynomial of the following form:

\[f(x) \approx \sum_{n=0}^{\infty} \frac{f^{(n)} (a)}{n!} (x-a)^n\]

Answer 1 : \(f(x) = \frac{1}{1-x}\)

Derivation

The first three derivatives for \(f(x) = \frac{1}{1-x}\) are \[f'(x) = \frac{1}{(1-x)^2} \\ f''(x) = \frac{2}{(1-x)^3} \\ f'''(x) = \frac{6}{(1-x)^4}\]

This can be generalized as \[f^{(n)}(x) = \frac{n!}{(1-x)^{n+1}}\]

Substituting this into the general equation, \[\frac{1}{1-x} \approx \sum_{n=0}^{\infty} \frac{n!}{(1-a)^{n+1}} \times \frac{(x-a)^n}{n!} = \frac{(x-a)^n}{(1-a)^n}\]

At \(a=0\), this becomes \[\frac{1}{1-x} \approx \sum_{n=0}^{\infty} x^n\]

Convergence

Using the ratio series for infinite series: \[\frac{A_{n+1}}{A_n} = \frac{x^{n+1}}{x^n} = x \\ \lim_{n \to \infty} x = x\]

So the series converges for \(|x| < 1\).

Validation

The true values and series approximations are plotted for \(n=25\):

taylor1 <- function(x, n) {
  sum <- 0
  for(i in 0:n) {
    sum <- sum + x^i
  }
  sum
}

It can be seen in the plot above that there is some divergence between the function and its Taylor series approximation at the edges of the range (-1, 1).

library(pracma)
## 
## Attaching package: 'pracma'
## The following object is masked from 'package:car':
## 
##     logit
## The following object is masked from 'package:purrr':
## 
##     cross
c = -5
f <- function(x) {1/(1-x)} 
A1 <- taylor(f, x0=c, 5)
A1
## [1]    1.000293   25.007447  250.076532 1250.401437 3126.111100 3126.512977
plot(A1)


Answer 2 : \(f(x) = e^x\)

Derivation

The derivative of \(f(x) = e^x\) is \(e^x\), so any derivative \(f^{(n)}(x) = e^x\).

\[f(x) = e^x = e^a + e^a (x-a) + e^a (x-a)^2 + e^a (x-a)^3+ ...\]

\[f(0)=1\] \[f'(0)/1!*(x)* 1 =1*x=x\] \[f'(0)=x\] \[f''(0)/2!*(x)^2 =1/2*(x)^2 =x^2/2\] \[f''(0) = x^2/2\] \[f'''(0)/3!*(x) 3 =1/6*(x)^ 3 =x^3/6\] \[f'''=x^3/6\]

So the Taylor series for \(f(x) = e^x\)

\[f(x)= 1 + x + \frac {x^2}{2} + \frac {x^3}{6} + \frac {x^4} {24} + ..\] \[ \sum _{n=0}^{\infty} \frac{x^n}{n!} \]

Substituting this into the general equation,

\[e^x \approx \sum_{n=0}^{\infty} e^x \times \frac{(x-a)^n}{n!} = \frac{e^x (x-a)^n}{n! (1-a)^n}\]

At \(a=0\), this becomes \[\frac{1}{1-x} \approx \sum_{n=0}^{\infty} \frac{x^n}{n!}\]

\[f(x) = e^x = e^0 + e^0 (x-0) + e^0 (x-0)^2 + e^0 (x-0)^3+ ...\] \[f(x) = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \frac{x^4}{4!} + ...\] \[f(x)\quad =\sum _{ n=0 }^{ \infty }{ \frac { x^{ n } }{ n! } ,\quad x\quad \in \quad R }\]

Convergence

Using the ratio series for infinite series: \[\frac{A_{n+1}}{A_n} = \frac{x^{n+1}}{(n+1)!} \frac{n!}{x^n} = \frac{x}{n+1} \\ \lim_{n \to \infty} \frac{x}{n+1} = 0\]

So the series converges for all values of x.

Validation

The true values and series approximations are plotted for \(n=25\):

taylor2 <- function(x, n) {
  sum <- 0
  for(i in 0:n) {
    sum <- sum + x^i / factorial(i)
  }
  sum
}

It can be seen in the plot above that the function and its taylor series approximation entirely overlap.

c = -5
f <- function(x) {exp(x)} 
A2 <- taylor(f, x0=c, 5)
A2
## [1]  0.008334245  0.208636869  2.090299109 10.480131597 26.309539362
## [6] 26.485006242
plot(A2)


Answer 3 : \(f(x) = ln(1 + x)\)

Derivation

The first four derivatives for \(f(x) = \ln(1+x)\) are \[f'(x) = \frac{1}{1+x} \\ f''(x) = \frac{-1}{(1+x)^2} \\ f'''(x) = \frac{2}{(1+x)^3} \\ f''''(x) = \frac{-6}{(1+x)^4}\]

This can be generalized as \[f^{(n)}(x) = (-1)^{n-1} \frac{(n-1)!}{(1+x)^{n}}\]

\[f(x) + \frac {f'(x)} {1!} x^1 + \frac {f''(x)} {2!} x^2 \] \[f(x) = f(0) + f'(0)(x - c) + \frac {f''(c)}{2!} (x-c)^2 + \frac {f'''(c)}{3!} (x- c)^3 + \]

Substituting this into the general equation, \[\ln(1+x) \approx \sum_{n=0}^{\infty} (-1)^{n+1} \frac{(n-1)!}{(1+a)^{n}} \times \frac{(x-a)^n}{n!} = (-1)^{n+1} \frac{(x-a)^n}{n (1+a)^n}\]

At \(a=0\), this becomes \[\ln(1+x) \approx \sum_{n=0}^{\infty} (-1)^{n+1} \frac{x^n}{n}\]

So the Taylor series for \(f(x)=ln(1+x)\) is: \[ f(x) = x - \frac {1} {2} x^2 + \frac {1}{3} x^3 - \frac {1}{4} x^4 + ... \] \[ \sum _{n=1}^{\infty} (-1)^{n+1} * \frac{x^n}{n} \]

Convergence

Using the ratio series for infinite series: \[\frac{A_{n+1}}{A_n} = - \frac{x^{n+1}}{n} \frac{n}{x^n} = - \frac{-xn}{n+1} \\ \lim_{n \to \infty} \frac{-xn}{n+1} = -x\]

So the series converges for \(|x| < 1\).

Validation

The true values and series approximations are plotted for \(n=25\):

taylor3 <- function(x, n) {
  sum <- 0
  for(i in 1:n) { # starting at n=1 since n=0 causes Inf
    sum <- sum + (-1)^(i + 1) * x^i / i
  }
  sum
}

As in the first plot, there is some divergence between the function and its Taylor series approximation at the lower edge of the valid range near -1.

c = 0
f <- function(x) {log(1+x)} 
A3 <- taylor(f, x0=c, 5)
A3
## [1]  0.2000413 -0.2500044  0.3333339 -0.5000000  1.0000000  0.0000000
plot(A3)