Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Last active June 4, 2026 08:49
Show Gist options
  • Select an option

  • Save abikoushi/f28d055c8736c40fdec5ff66bee34cbb to your computer and use it in GitHub Desktop.

Select an option

Save abikoushi/f28d055c8736c40fdec5ff66bee34cbb to your computer and use it in GitHub Desktop.
An example of Taylor expansion
g <- function(a,t){
##(exp(a*t)-1)/a
expm1(a*t)/a
}
gapprox1 <- function(a,t){
t + 0.5*(t^2)*a
}
gapprox2 <- function(a,t){
t + 0.5*(t^2)*a + (1/6)*(t^3)*a^2
}
cols <- c("black","steelblue","firebrick")
png(width=600,height = 500)
par(mfrow=c(1,2))
curve(g(x,1), col=cols[1], xlab = "a", ylab = "expm1(a*t)/a")
curve(gapprox1(x,1),add=TRUE, lty=2, col=cols[2])
curve(gapprox2(x,1),add=TRUE, lty=3, col=cols[3])
legend("topleft", c("exact","1st-order","2nd-order"), col = cols, lty=1:3,lwd=1.5)
curve(g(x,1) - gapprox1(x,1), lty=2, col=cols[2], xlab = "a", ylab = "residuals")
curve(g(x,1) - gapprox2(x,1), lty=3, col=cols[3],add=TRUE)
legend("topleft", c("1st-order","2nd-order"), col = cols[2:3], lty=3:3,lwd=1.5)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment