See in-line below.

On 03-Jan-2012 alexander16 wrote:
> Dear everyone,
> the following is obviously used to compute the nth derivative,
> which seems to work
> (deriv(sqrt(1 - x^2),x,n))

Well, it doesn't seem to work for me! In fact:

  n <- 2
  (deriv(sqrt(1 - x^2),x,n))
  # Error in deriv(sqrt(1 - x^2), x, n) : object 'x' not found

Are you sure it works for you? Example with output?

Things look better if you use, say, the "formula" interface,
but you must also quote the "x":

  n <- 2
  (deriv(~ sqrt(1 - x^2),"x",n))
  # expression({
  #     .expr2 <- 1 - x^2
  #     .value <- sqrt(.expr2)
  #     .grad <- array(0, c(length(.value), 1L), list(NULL, c("x")))
  #     .grad[, "x"] <- -(0.5 * (2 * x * .expr2^-0.5))
  #     attr(.value, "gradient") <- .grad
  #     .value

And you can see that .grad[,"x"] has been assigned the espression
  -(0.5 * (2 * x * .expr2^-0.5))
but that is only the first derivative, not the second.
Note that quoting "x" in your first form doesn't work either:

    n <- 2
    (deriv(sqrt(1 - x^2),"x",n))
  # Error in deriv(sqrt(1 - x^2), "x", n) : object 'x' not found

> However, before using this, I wanted to make sure it does what
> I think it does but can't figure it out when reading the ?deriv
> info or any other documentation on  deriv for that matter:
> 
> deriv(expr, namevec, function.arg = NULL, tag = ".expr",
> hessian = FALSE, ...)
> This doesn't seem to include an "order of derivative" argument,
> in fact, in the examples section, it is outlined, how one can
> build a higher deriv. function oneself...
> 
> Any hints are much appreciated!
> alex

There is no "order of derivative" argument available. As a more
transparent test, using the function x^10 :

  deriv(~ x^10,"x")
  # expression({
  #     .value <- x^10
  #     .grad <- array(0, c(length(.value), 1L), list(NULL, c("x")))
  #     .grad[, "x"] <- 10 * x^9
  #     attr(.value, "gradient") <- .grad
  #     .value
  # })

which, as expected, gives 10*(x^9); and:

  deriv(~ x^10,"x",2)

which again gives 

  # expression({
  #     .value <- x^10
  #     .grad <- array(0, c(length(.value), 1L), list(NULL, c("x")))
  #     .grad[, "x"] <- 10 * x^9
  #     attr(.value, "gradient") <- .grad
  #     .value
  # })

i.e. the first derivative 10*(x^9) as before. So you are indeed
obliged to define a recursive function for higher-order derivatives.

Hoping this helps,
Ted.
----------------------------------
E-Mail: (Ted Harding) <ted.hard...@wlandres.net>
Date: 03-Jan-2012
Time: 10:28:27

This message was sent by XFMail

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to