I'm trying to graph a function (from user input) and some of its Taylor Series approximations in R. I'm having no trouble graphing the user's function, but have failed several times trying to graph its approximations. My latest attempt involved the pracma package's taylor() function, but still to no avail. Here is my code:
output$plot1 <- renderPlot({
tempTxt2 <- parse(text=input$userTxtVar)
f1 <- Vectorize(function(x){ return(eval(tempTxt2))})
a <- as.numeric(input$userPoint)
taylor1 = Vectorize(function(x){return((taylor(f1, a, n=2)))})
curve(expr = f1, from = 0, to =3)
curve(taylor1, col = 2, add = TRUE) #line that has the error
})
Normally this error is resolved by using Vectorize(), but no luck here. I guess I don't understand the taylor() function; could anyone help?
If you check the help page for
tayloror go through your function line by line, you will recognize that the 'taylor ' function does not return the value of the taylor expansion, but a vector of polynomial coefficients. ApplyingVectorizeto it does not make sense.The
taylor1function is vectorized becausepolyval(the "Horner scheme") automatically generates a vectorized function, calculating a polynomial at all input values.