Sunday, 15 March 2015

Taylor approximation in R -



Taylor approximation in R -

is there function/package in r takes function f , parameter k, , returns taylor approximation of f of grade k?

you can utilize ryacas work yacas computer algebra scheme (which need install well)

using illustration vignette

library(ryacas) # run yacasinstall() if prompted install yacas # yacas("texp := taylor(x,0,3) exp(x)") ## expression(x + x^2/2 + x^3/6 + 1) # or

now, if want turn function can give values of x

mytaylor <- function(f, k, var,...){ .call <- sprintf('texp := taylor( %s, 0, %s) %s', var,k,f) result <- yacas(.call) foo <- function(..., print = false){ if(print){print(result)} eval(result, list(...))} return(foo) } # create function foo <- mytaylor('exp(x)', 3, 'x') foo(x=1:5) ## [1] 2.666667 6.333333 13.000000 23.666667 39.333333 foo(x=1:5, print = true) ## expression(x + x^2/2 + x^3/6 + 1) ## [1] 2.666667 6.333333 13.000000 23.666667 39.333333

r

No comments:

Post a Comment