I would like to find numerical derivatives of a bivariate function.
Is there a package or built-in function to do this?
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.
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 ).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With