if(.Platform$OS.type=="windows") invisible(Sys.setlocale("LC_CTYPE","English_United States.utf8"))
# Run from this script's folder. Base R only; CSV source is read without modification.
source <- read.csv("math11-sources/korea-2014-2024-observed-model-taylor-errors.csv",check.names=FALSE)
poly <- function(x,n) sum(x^(0:n)/factorial(0:n))
results <- numeric()
for(x in c(-2,-1,0,.2,1,2)) for(n in c(1,2,6)) results[paste0("exp_",format(x,trim=TRUE,scientific=FALSE),"_n",n)] <- poly(x,n)
for(x in c(1,4,4.4,7)){ h <- x-4;results[paste0("sqrt_",format(x,trim=TRUE))] <- 2+h/4-h*h/64 }
for(x in c(-.9,-.5,.5,.9)) for(n in c(5,20)) results[paste0("log_",format(x,trim=TRUE),"_n",n)] <- sum((-1)^((1:n)+1)*x^(1:n)/(1:n))
A <- 29470.6732991655;k <- log(37048.4264001496/A)/10
for(year in c(2014,2020,2024)){
 h <- year-2014;exact <- A*exp(k*h);linear <- A*(1+k*h);quadratic <- linear+A*k*k*h*h/2
 for(name in c("exact","linear","quadratic","bound")) results[paste0("gdp_",year,"_",name)] <- switch(name,exact=exact,linear=linear,quadratic=quadratic,bound=exact*(k*h)^3/6)
}
results["gdp_2020_residual"] <- 33215.92989281-A*exp(k*6)
write.csv(data.frame(quantity=names(results),value=unname(results)),"math11-r-results.csv",row.names=FALSE)
cat("MATH-11:",nrow(source),"source years;",length(results),"quantities\n")
