泰勒展开式绘图
1. f(x)=tanx,n=4
2. Tn(1/(1+x))=sum(-1)^k x^k
范例是sinx
p <- function(x,m){ # m must be odd.
# m <- 5 展开到的项次
if ((m%%2)==1){
s <- x
for (n in 2:((m+1)/2)){
s <- s+((-1)^(n-1))*(x^(2*n-1))/factorial(2*n-1)
}
s
}
}
curve(sin(x),-pi,pi,lwd=2)
abline(h=0,lwd=2,add=T,lty=2)
curve(p(x,1),add=T,lwd=2,col="darkgreen")
curve(p(x,3),add=T,lwd=2,col="blue")
curve(p(x,5),add=T,lwd=2,col="red")
curve(sin(x)-p(x,1),-pi,pi,lwd=2,col="darkgreen")
curve(sin(x)-p(x,3),add=T,lwd=2,col="blue")
curve(sin(x)-p(x,5),add=T,lwd=2,col="red")
but if ((m%%2)==1) 是甚么意思?
2我改成
p <- function(x,m){# m must be odd.
# m <- 3 展开到的项次
if ((m%%1)==1){
s <- x
for (n in 0:m)){
s <- s+((-1)^n)*(x^n)
}
s
}
}
就会出现问题?要怎么改