schwartz
schwartz

Reputation: 475

Derivative in R and finding value at a point

I have t^3 / sqrt(1 - t^2) Which I want to find the third derivative to and then finding the value at t=0. I want to do this in R. I have tried

dddft <- function(t) {D(D(D(expression((t^3) / (sqrt(1-t^2))), "t"), "t"), "t")}

And then I try

dddft(0)

But doing this just gives me a long expression and not at t=0.

What do I do wrong?

Upvotes: 2

Views: 424

Answers (1)

jay.sf
jay.sf

Reputation: 72758

Evaluate expression with eval() that it can be run.

dddft <- function(t) eval(D(D(D(expression((t^3) / (sqrt(1-t^2))), "t"), "t"), "t"))

dddft(0)
# [1] 6

To avoid painful nesting, you could use this function (see ?D):

DD <- function(expr, name, order = 1) {
  if(order < 1) stop("'order' must be >= 1")
  if(order == 1) D(expr, name)
  else DD(D(expr, name), name, order - 1)
}

dddft <- function(x) eval(DD(expression(x^3 / sqrt(1 - x^2)), "x", 3))
dddft(0)
# [1] 6

Upvotes: 1

Related Questions