[R] higher derivatives using deriv

Spencer Graves spencer.graves at structuremonitoring.com
Tue Jan 3 17:21:51 CET 2012


I suggest you look at the help pages for "expression" and "evel", then 
consider the following example in addition to the function "DD" in the 
"examples" for "deriv":


Dxy <- deriv( ~x*y*z, c('x', 'y'))
x <- 2; y <- 3; z <- 4
eval(Dxy)
Dxy.fn <- deriv(expression(x*y*z), c('x', 'y'), TRUE)
Dxy.fn
Dxy.fn(x, y)


       Please check also the arguments for the functions documented with 
help("deriv").  In particular, the arguments control whether the object 
returned is an "expression" or a function and whether the hessian is 
included.  By contrast, the function "DD" in the "examples" for 
help("deriv") includes the "order" argument you want.  Also, the help 
page for "expression" describes (perhaps too briefly) what an 
"expression" is.  Use "parse(text=...)" to convert text to expression. 
And use "eval" to evaluate an expression.


       Hope this helps.
       Spencer


On 1/3/2012 2:46 AM, (Ted Harding) wrote:
> 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.Harding at wlandres.net>
> Date: 03-Jan-2012
> Time: 10:28:27
>
> This message was sent by XFMail
>
> ______________________________________________
> R-help at 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.


-- 
Spencer Graves, PE, PhD
President and Chief Technology Officer
Structure Inspection and Monitoring, Inc.
751 Emerson Ct.
San José, CA 95126
ph:  408-655-4567
web:  www.structuremonitoring.com



More information about the R-help mailing list