Let f be a function whose n+1th derivative exists on an itnerval and let c be in interval. Then, for each x in interval, there exists Z_x between x and c such that :
\(f(x)=\sum _{ n=0 }^{ }{ \frac { f^{ n }\left( c \right) }{ n! } { (x-c) }^{ n } }+ {R_n}{(x)}\)
where:
\(\\R_{ n }(x)\quad =\quad { \frac { f^{ n+1 }\left( { z }_{ x } \right) }{ (n+1)! } { (x-c) }^{ n+1 } }\\\)
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.
\(f(x)=\frac { 1 }{ \left( 1-x \right)}\)
Taylorf<-function(x,n){
y=0
for (i in 0:n) y=y+sum(x^i)
return (y)
}
Taylorf(0.5,0)
## [1] 1
Taylorf(0.5,1)
## [1] 1.5
Taylorf(0.5,2)
## [1] 1.75
Taylorf(0.5,3)
## [1] 1.875
Taylorf(0.5,4)
## [1] 1.9375
Taylorf(0.5,5)
## [1] 1.96875
Taylorf(0.5,10)
## [1] 1.999023
Taylorf(0.5,20)
## [1] 1.999999
Taylorf(0.5,21)
## [1] 2
Taylorf(0.5,22)
## [1] 2
1/(1-0.5)
## [1] 2
x <- seq(-0.7, 0.7, length.out=100)
f <- function(x) (1/(1-x))
y<-f(x)
p <- taylor(f, 0, 4)
yp <- polyval(p, x)
plot(x, y, type = "l")
lines(x, yp, col = "red")
legend('topleft', inset=.05, legend= c("Taylor Series", "f(x)=1/(1-x)"), lwd=c(2.5,2.5), col=c('red', 'black'), bty='n', cex=.75)
The graph from above showed f(0.5) closes to 2.
Now, find n s.t. the nth Taylor polynomial of \(p_n\)(0.5) approximates to f(0.5) to within 0.1 of the actual answer.
f <- function(x) 1/(1-x)
findn<- function(f){
err=10
n=0
while(err>0.1){
yacas(f) # register f with yacas
Df <- f
body(Df) <- yacas(expression(deriv(f(x))))[[1]]
err=Df(0)/factorial(n+1)*0.5^(n+1)
print(err)
n=n+1
print(n)
f<-Df
print(f)
}
return(n+2)
}
num=findn(f)
## [1] 0.5
## [1] 1
## function (x)
## 1/(1 - x)^2
## [1] 0.25
## [1] 2
## function (x)
## -(-2 * (1 - x))/(1 - x)^4
## [1] 0.125
## [1] 3
## function (x)
## (-2 * (1 - x)^4 - 2 * ((1 - x) * (-4 * (1 - x)^3)))/(1 - x)^8
## [1] 0.0625
## [1] 4
## function (x)
## ((1 - x)^8 * (2 * ((1 - x) * (-12 * (1 - x)^2) - 4 * (1 - x)^3) -
## -8 * (1 - x)^3) - (2 * ((1 - x) * (4 * (1 - x)^3)) - 2 *
## (1 - x)^4) * (-8 * (1 - x)^7))/(1 - x)^16
num
## [1] 6
From the previous taylor function caculation, taylor expansion at 6th term has lesser than 0.1 difference from 5th term.
\(f(x)={ e }^{ x }\)
Taylorf<-function(x,n){
y=0
for (i in 0:n) y=y+sum(x^(i)/factorial(i))
return (y)
}
Taylorf(1,0)
## [1] 1
Taylorf(1,1)
## [1] 2
Taylorf(1,2)
## [1] 2.5
Taylorf(1,3)
## [1] 2.666667
Taylorf(1,4)
## [1] 2.708333
Taylorf(1,8)
## [1] 2.718279
exp(1)
## [1] 2.718282
x <- seq(-5, 5, length.out=100)
f <- function(x) (exp(x))
y<-f(x)
p <- taylor(f, 0, 4)
yp <- polyval(p, x)
plot(x, y, type = "l")
lines(x, yp, col = "red")
legend('topleft', inset=.05, legend= c("Taylor Series", "f(x)=e^(x)"), lwd=c(2.5,2.5), col=c('red', 'black'), bty='n', cex=.75)
The graph from above showed f(1) closes to 2.7.
Now, find n s.t. the nth Taylor polynomial of \(p_n\)(1) approximates to f(1) to within 0.0001 of the actual answer.
library(Ryacas)
f <- function(x) exp(x)
findn<- function(f){
err=10
n=0
while(err>0.0001){
yacas(f) # register f with yacas
Df <- f
body(Df) <- yacas(expression(deriv(f(x))))[[1]]
err=Df(0)/factorial(n+1)*0.5^(n+1)
print(err)
n=n+1
print(n)
f<-Df
print(f)
}
return(n+2)
}
num=findn(f)
## [1] 0.5
## [1] 1
## function (x)
## exp(x)
## [1] 0.125
## [1] 2
## function (x)
## exp(x)
## [1] 0.02083333
## [1] 3
## function (x)
## exp(x)
## [1] 0.002604167
## [1] 4
## function (x)
## exp(x)
## [1] 0.0002604167
## [1] 5
## function (x)
## exp(x)
## [1] 2.170139e-05
## [1] 6
## function (x)
## exp(x)
num
## [1] 8
From the previous taylor function caculation, taylor expansion at 8th term has lesser than 0.0001 difference with 7th term.
\(f(x)=\ln {\left( 1+x \right)}\)
Taylorf<-function(x,n){
y=0
for (i in 1:n) y=y+sum((-1)^(i+1)*x^i/i)
return (y)
}
Taylorf(0.5,1)
## [1] 0.5
Taylorf(0.5,2)
## [1] 0.375
Taylorf(0.5,3)
## [1] 0.4166667
Taylorf(0.5,4)
## [1] 0.4010417
Taylorf(0.5,5)
## [1] 0.4072917
Taylorf(0.5,10)
## [1] 0.4054346
Taylorf(0.5,20)
## [1] 0.4054651
log(1.5)
## [1] 0.4054651
x <- seq(-1, 1, length.out=100)
f <- function(x) (log(1+x))
y<-f(x)
p <- taylor(f, 0, 5)
yp <- polyval(p, x)
plot(x, y, type = "l")
lines(x, yp, col = "red")
legend('topleft', inset=.05, legend= c("Taylor Series", "ln(1+x)"), lwd=c(2.5,2.5), col=c('red', 'black'), bty='n', cex=.75)
The graph from above showed f(0.5) closes to 0.4.
Now, find n s.t. the nth Taylor polynomial of \(p_n\)(0.5) approximates to f(0.5) to within 0.01 of the actual answer.
f <- function(x) log(1+x)
findn<- function(f){
err=10
n=0
while(err>0.01){
yacas(f) # register f with yacas
Df <- f
body(Df) <- yacas(expression(deriv(f(x))))[[1]]
err=abs(Df(0)/factorial(n+1)*0.5^(n+1))
print(err)
n=n+1
print(n)
f<-Df
print(f)
}
return(n+2)
}
num=findn(f)
## [1] 0.5
## [1] 1
## function (x)
## 1/(x + 1)
## [1] 0.125
## [1] 2
## function (x)
## -1/(x + 1)^2
## [1] 0.04166667
## [1] 3
## function (x)
## -(-2 * (x + 1))/(x + 1)^4
## [1] 0.015625
## [1] 4
## function (x)
## (2 * (x + 1)^4 - 2 * ((x + 1) * (4 * (x + 1)^3)))/(x + 1)^8
## [1] 0.00625
## [1] 5
## function (x)
## ((x + 1)^8 * (8 * (x + 1)^3 - 2 * ((x + 1) * (12 * (x + 1)^2) +
## 4 * (x + 1)^3)) - (2 * (x + 1)^4 - 2 * ((x + 1) * (4 * (x +
## 1)^3))) * (8 * (x + 1)^7))/(x + 1)^16
num
## [1] 7
From the previous taylor function caculation, taylor expansion at 7th term has lesser than 0.01 difference with 6th term.