In this assignment we will explore 3 typical Taylor Series.
We will first find the Taylor Serie centered at \({ x }_{ 0 }=0\) (McLaurin Serie) for a given function and then write a program to randomly check various value of x in the domain of function and calulate the result using the function definition and the Taylor Serie.
We will first look at function \(f\left( x \right) =\frac { 1 }{ 1-x }\) define over interval (-1,1).
We will expand the Taylor Serie centered at \({ x }_{ 0 }=0\). We will start by finding the first few terms \(f\left( x \right) ,\quad f^{ ' }\left( x \right) ,\quad f^{ (2) }\left( x \right) ,\quad f^{ (3) }\left( x \right) ,\quad and\quad extrapolate\quad f^{ (n) }\left( x \right)\).
We will first find the derivatives and then evaluate them at \({ x }_{ 0 }=0\). Since we have, \(f\left( x \right) =\frac { 1 }{ 1-x }\), let us denote u(x)=1-x, and u’(x)=-1, we can write f(x) as 1/u(x). We will apply chain rules to find derivatives.
\(f^{ ' }\left( x \right) =-1\cdot { u }^{ -2 }\cdot (-1)={ u }^{ -2 }={ (1-x) }^{ -2 }\)
\(f^{ (2) }\left( x \right) =-2\cdot { u }^{ -3 }\cdot (-1)=2\cdot { u }^{ -3 }=2\cdot { (1-x) }^{ -3 }\)
\(f^{ (3) }\left( x \right) =2\cdot (-3)\cdot { u }^{ -4 }\cdot (-1)=2\cdot 3\cdot { u }^{ -4 }=2\cdot 3\cdot { (1-x) }^{ -4 }=3!\cdot { (1-x) }^{ -4 }\)
Extrapolating for n derivatives, we have;
\(f^{ (n) }\left( x \right) =(n-1)!\cdot (-n)\cdot { u }^{ -(n+1) }\cdot (-1)=(n-1)!\cdot n\cdot { u }^{ -(n+1) }=n!\cdot { (1-x) }^{ -(n+1) }\)
Hence since we are evaluating the Serie at \({ x }_{ 0 }=0\), we can write the Tayor Serie for \(f\left( x \right) =\frac { 1 }{ 1-x }\) as follows;
\(\frac { 1 }{ 1-x } =f\left( 0 \right) +f^{ (1) }\left( 0 \right) (x-0)+\frac { 1 }{ 2! } f^{ (2) }\left( 0 \right) { (x-0) }^{ 2 }+\frac { 1 }{ 3! } f^{ (3) }\left( 0 \right) { (x-0) }^{ 3 }+\quad ...\quad +\frac { 1 }{ n! } f^{ (n) }\left( 0 \right) { (x-0) }^{ n }\)
We will now replace the derivative by their value for 0. This will give us;
\(\frac { 1 }{ 1-x } =1+x+\frac { 2! }{ 2! } 1{ x }^{ 2 }+\frac { 3! }{ 3! } 1{ x }^{ 3 }+\quad ...\quad +\frac { n! }{ n! } 1{ x }^{ n }\)
Simplifying the terms and using sigma notations, we obtain the following;
\(\frac { 1 }{ 1-x } =\sum _{ n=0 }^{ \infty }{ { x }^{ n } } ,\quad for\quad x\epsilon (-1,1)\)
We will now look at the funtion \(f\left( x \right) ={ e }^{ x }\) define over Real numbers.
We will expand the Taylor Serie centered at \({ x }_{ 0 }=0\). We will start by finding the first few terms \(f\left( x \right) ,\quad f^{ ' }\left( x \right) ,\quad f^{ (2) }\left( x \right) ,\quad f^{ (3) }\left( x \right) ,\quad and\quad extrapolate\quad f^{ (n) }\left( x \right)\).
We will first find the derivatives and then evaluate them at \({ x }_{ 0 }=0\). Since \(f^{ ' }\left( x \right) ={ e }^{ x }\) as well, we will have:
\(f^{ ' }\left( x \right) ={ e }^{ x }\)
\(f^{ (2) }\left( x \right) ={ e }^{ x }\)
\(f^{ (3) }\left( x \right) ={ e }^{ x }\)
Extrapolating for n derivatives, we have;
\(f^{ (n) }\left( x \right) ={ e }^{ x }\)
Hence since we are evaluating the Serie at \({ x }_{ 0 }=0\), we can write the Tayor Serie for \(f\left( x \right) ={ e }^{ x }\) as follows:
\(\frac { 1 }{ 1-x } =f\left( 0 \right) +f^{ (1) }\left( 0 \right) (x-0)+\frac { 1 }{ 2! } f^{ (2) }\left( 0 \right) { (x-0) }^{ 2 }+\frac { 1 }{ 3! } f^{ (3) }\left( 0 \right) { (x-0) }^{ 3 }+\quad ...\quad +\frac { 1 }{ n! } f^{ (n) }\left( 0 \right) { (x-0) }^{ n }\)
We will now replace the derivative by their value for 0. This will give us;
\({ e }^{ x }={ e }^{ 0 }+{ e }^{ 0 }x+\frac { { e }^{ 0 } }{ 2! } { x }^{ 2 }+\frac { { e }^{ 0 } }{ 3! } { x }^{ 3 }+....+\frac { { e }^{ 0 } }{ n! } { x }^{ n }\)
since \({ e }^{ 0 }=1\), this becomes:
\({ e }^{ x }=1+x+\frac { { x }^{ 2 } }{ 2! } +\frac { { x }^{ 3 } }{ 3! } +\quad ...\quad +\frac { { x }^{ n } }{ n! }\)
Simplifying the terms and using sigma notations, we obtain the following;
\({ e }^{ x }=\sum _{ n=0 }^{ \infty }{ \frac { { x }^{ n } }{ n! } }\)
Finally, we will now consider the function \(f\left( x \right) =\ln { (1+x) } \quad with\quad x\epsilon (-1,1)\).
We will expand the Taylor Serie centered at \({ x }_{ 0 }=0\). We will start by finding the first few terms \(f\left( x \right) ,\quad f^{ ' }\left( x \right) ,\quad f^{ (2) }\left( x \right) ,\quad f^{ (3) }\left( x \right) ,\quad and\quad extrapolate\quad f^{ (n) }\left( x \right)\).
We will first find the derivatives and then evaluate them at \({ x }_{ 0 }=0\). Since we have, \(f\left( x \right) =\ln { (1+x) }\), let us denote u(x)=1+x, and u’(x)=1, we can write f(x) as ln(u). We will apply chain rules to find derivatives.
\(f^{ (1) }\left( x \right) =\frac { 1 }{ u } \cdot 1=\frac { 1 }{ 1+x }\)
\(f^{ (2) }\left( x \right) =(-1){ u }^{ -2 }(1)=-\frac { 1 }{ { (1+x) }^{ 2 } }\)
\(f^{ (3) }\left( x \right) =(-1)(-2){ u }^{ -3 }(1)=\frac { 2 }{ { (1+x) }^{ 3 } }\)
\(f^{ (4) }\left( x \right) =(2)(-3){ u }^{ -4 }(1)=-\frac { 3! }{ { (1+x) }^{ 4 } }\)
Extrapolating for n derivatives, we have;
\(f^{ (n) }\left( x \right) ={ (-1) }^{ n-1 }(n-2)!(n-1){ u }^{ -n) }(1)={ (-1) }^{ n-1 }\frac { (n-1)! }{ { (1+x) }^{ n } }\)
Hence since we are evaluating the Serie at \({ x }_{ 0 }=0\), we can write the Tayor Serie for \(f\left( x \right) =\ln { (1+x) }\) as follows:
\(\frac { 1 }{ 1-x } =f\left( 0 \right) +f^{ (1) }\left( 0 \right) (x-0)+\frac { 1 }{ 2! } f^{ (2) }\left( 0 \right) { (x-0) }^{ 2 }+\frac { 1 }{ 3! } f^{ (3) }\left( 0 \right) { (x-0) }^{ 3 }+\quad ...\quad +\frac { 1 }{ n! } f^{ (n) }\left( 0 \right) { (x-0) }^{ n }\)
We will now replace the derivative by their value for 0. This will give us;
\(\ln { (1+x)= } 0+1x-1\frac { { x }^{ 2 } }{ 2! } +2!\frac { { x }^{ 3 } }{ 3! } -3!\frac { { x }^{ 4 } }{ 4! } +\quad ...\quad +{ (-1) }^{ n-1 }(n-1)!\frac { { x }^{ n } }{ n! }\)
Simplifying the terms and using sigma notations, we obtain the following;
\(\ln { (1+x)= } \sum _{ n=1 }^{ \infty }{ { (-1) }^{ n-1 }\frac { { x }^{ n } }{ n } }\)
We will now compare the results for calculation based on functions and taylor series for every functions.
First we will write programs to represent each function and each Taylor Series.
### First Function
func_1 <- function(x){
result <- 1/(1-x)
return(result)
}
func_taylor_1 <- function(x, d=50){
i <- 0
result <- 0
while (i <= d){
result <- result + x^i
i<-i+1
}
return(result)
}
### 2nd Function
func_2 <- function(x){
result <- exp(x)
return(result)
}
func_taylor_2 <- function(x, d=50){
i <- 0
result <- 0
while (i <= d){
result <- result + (x^i/factorial(i))
i<-i+1
}
return(result)
}
### 3rd Function
func_3 <- function(x){
result <- log(1+x)
return(result)
}
func_taylor_3 <- function(x, d=50){
i <- 1
result <- 0
while (i <= d){
result <- result + ((-1)^(i+1))*(x^i/i)
i<-i+1
}
return(result)
}
We will now write a function that will allow us to compare the calcualtions between each function and their corresponding Taylor Series.
test_taylor <- function(func_indicator=1, degree=50, n=100){
# This function will test the taylor series for various functions denoted by func_indicator
# For function 1 or 3, the range for x values will be (-1,1) exclusive
remove <-c(-1,1)
if (func_indicator == 1 || func_indicator==3){
x_universe <- seq(-1,1,length.out = n+2)
x_universe <-setdiff(x_universe, remove)
}else{
x_universe <- seq(-100.0,100.0,length.out = n)
}
func_results <- vector(mode='numeric', length=n)
taylor_results <-vector(mode='numeric', length=n)
for (i in 1:n){
x<-x_universe[i]
if(func_indicator == 1){
func_results[i] <- func_1(x)
taylor_results[i] <- func_taylor_1(x, degree)
}else if (func_indicator == 2){
func_results[i] <-func_2(x)
taylor_results[i] <- func_taylor_2(x, degree)
}else{
func_results[i] <-func_3(x)
taylor_results[i] <- func_taylor_3(x, degree)
}
} #End of For loop
r_list<-list(x_universe, func_results, taylor_results)
return(r_list)
}
We will now test by calling this test function…
# Set allowed Error
error_allowed <- 0.0001
# Testing function1
res1_1 <- test_taylor(1, 10, 50)
res1_2 <- test_taylor(1, 50, 50)
x_values1_1 <- res1_1[[1]]
func_values1_1 <- res1_1[[2]]
taylor_values1_1 <- res1_1[[3]]
x_values1_2 <- res1_2[[1]]
func_values1_2 <- res1_2[[2]]
taylor_values1_2 <- res1_2[[3]]
compare_values1_1 <- func_values1_1 - taylor_values1_1
compare_values1_2 <- func_values1_2 - taylor_values1_2
compare_values1_1 <= error_allowed
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [12] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [23] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [34] TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [45] FALSE FALSE FALSE FALSE FALSE FALSE
compare_values1_2 <= error_allowed
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [12] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [23] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [34] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [45] TRUE TRUE FALSE FALSE FALSE FALSE
As expected, we get better results when we performed Taylor Series with more terms (10 vs 50). We will repeat the test for the other 2 fucntions.
# Testing function2
res2_1 <- test_taylor(2, 10, 50)
res2_2 <- test_taylor(2, 50, 50)
x_values2_1 <- res2_1[[1]]
func_values2_1 <- res2_1[[2]]
taylor_values2_1 <- res2_1[[3]]
x_values2_2 <- res2_2[[1]]
func_values2_2 <- res2_2[[2]]
taylor_values2_2 <- res2_2[[3]]
compare_values2_1 <- func_values2_1 - taylor_values2_1
compare_values2_2 <- func_values2_2 - taylor_values2_2
compare_values2_1 <= error_allowed
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [12] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [23] TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [34] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [45] FALSE FALSE FALSE FALSE FALSE FALSE
compare_values2_2 <= error_allowed
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [12] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [23] TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE
## [34] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [45] FALSE FALSE FALSE FALSE FALSE FALSE
# Testing function3
res3_1 <- test_taylor(3, 10, 50)
res3_2 <- test_taylor(3, 50, 50)
x_values3_1 <- res3_1[[1]]
func_values3_1 <- res3_1[[2]]
taylor_values3_1 <- res3_1[[3]]
x_values3_2 <- res3_2[[1]]
func_values3_2 <- res3_2[[2]]
taylor_values3_2 <- res3_2[[3]]
compare_values3_1 <- func_values3_1 - taylor_values3_1
compare_values3_2 <- func_values3_2 - taylor_values3_2
compare_values3_1 <= error_allowed
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [12] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [23] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [34] TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
## [45] FALSE FALSE FALSE FALSE FALSE FALSE
compare_values3_2 <= error_allowed
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [12] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [23] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [34] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [45] TRUE TRUE TRUE TRUE FALSE FALSE