Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numerical derivatives of an arbitrarily defined function

Tags:

r

derivative

I would like to find numerical derivatives of a bivariate function.

  • The function is defined by myself
  • I need first derivatives with respect to each argument and cross second derivative

Is there a package or built-in function to do this?

like image 881
user67275 Avatar asked Aug 28 '13 16:08

user67275


People also ask

What is numerical differentiation with example?

Numerical differentiation involves the computation of a derivative of a function f from given values of f. Such formulas are basic to the numerical solution of differential equations. Defining f j ′ = f ′ ( x j ) , f j ″ = f ″ ( x j ) , where f ′ ( x ) = lim h → 0 ⁡ f ( x + h ) − f ( x ) h , one obtains the relations.

What is numerical differentiation PDF?

Numerical differentiation is the process of calculating the value of the. derivative of a function at some assigned value of x from the given set of. data points (xi, yi = f( xi )), i = 0,1,2,..., n which correspond to the values of. an unknown function y = f( x ).

What are the drawbacks of numerical differentiation?

There are several ways to treat the numerical differentiation problem. The most common and simplest way, which is used by the engineers, is finite difference. One of the disadvantages of this method is that, in the case that the data contains errors, even if we have many data, we cannot use all data.


1 Answers

Install and load the numDeriv package.

library(numDeriv)
f <- function(x) {
    a <- x[1]; b <- x[2]; c <- x[3]
    sin(a^2*(abs(cos(b))^c))
}
grad(f,x=1:3)
## [1]  0.14376097  0.47118519 -0.06301885
hessian(f,x=1:3)
##            [,1]       [,2]        [,3]
## [1,]  0.1422651  0.9374675 -0.12538196
## [2,]  0.9374675  1.8274058 -0.25388515
## [3,] -0.1253820 -0.2538852  0.05496226

(My example is trivariate rather than bivariate, but it will obviously work for a bivariate function as well.) See the help pages for more information on how the gradient and especially Hessian computations are done.

like image 117
Ben Bolker Avatar answered Oct 14 '22 19:10

Ben Bolker