numerical gradient estimation at one point
g = numdiff(fun, x) g = numdiff(fun, x, dx)
an external, Scilab function or list. See below for calling sequence, see also external for details about external functions. f: Rn --> Rp
a vector of the n
coordinates of the single point at which the gradient is sought.
a vector, the finite difference step. Default value is
dx = sqrt(%eps)*(1+1d-3*abs(x))
.
a matrix, the estimated gradient at the locus x
.
Given a function fun(x)
from
Rn to Rp computes the p x n
matrix
g
such that
g(i,j) = [(df_i)/(dx_j)](x)
using finite difference methods. Uses an order 1 formula.
Without parameters, the function fun
calling sequence is
y = fun(x)
, with x
∈ Rn and y
∈ Rp, and numdiff
can be called as
g = numdiff(fun, x)
. Else the function fun
calling
sequence must be y = fun(x, param_1, pararm_2, ..., param_q)
.
If parameters param_1, param_2, ..., param_q
exist then
numdiff
can be called as follow
g = numdiff(list(fun, param_1, param_2, ..., param_q), x)
.
See the derivative with respect to numerical accuracy issues and comparison between the two algorithms.
// Example 1 (without parameters) // myfun is a function from R^2 to R: (x(1), x(2)) |--> myfun(x) function f=myfun(x) f = x(1)*x(1) + x(1)*x(2) endfunction x = [5 8]; g = numdiff(myfun, x) // The exact gradient (i.e first component = derivate with respect to x(1) // and second component = derivate with respect to x(2)) is: exact = [2*x(1)+x(2) x(1)] // Example 2 (with parameters) // myfun is a function from R to R: x |--> myfun(x) // myfun contains 3 parameters: a, b and c function f=myfun(x, a, b, c) f = (x+a)^c + b endfunction a = 3; b = 4; c = 2; x = 1; g2 = numdiff(list(myfun, a, b, c), x) // The exact gradient, i.e derivate with respiect to x, is: exact2 = c*(x+a)^(c-1) // Example 3 (f: R^3 --> R^3) // myfun is a function from R^2 to R^2: (x(1), x(2), x(3)) |--> (myfun(x)(1), myfun(x)(2), mfun(x)(3)) function f=myfun(x) f(1) = x(1) * x(1); f(2) = x(1) * x(2) * x(3); f(3) = 2*x(1) + 2*x(2) + 2*x(3); endfunction x = [5 8 10]; g = numdiff(myfun, x) // The exact gradient is: // [ df_1/dx_1 df_1/dx_2 df_1/dx_3 ; // df_2/dx_1 df_2/dx_2 df_2/dx_3 ; // df_3/dx_1 df_3/dx_2 df_3/dx_3 ; ] exact3 = [2*x(1) 0 0 ; x(2)*x(3) x(1)*x(3) x(1)*x(2) ; 2 2 2] | ![]() | ![]() |
We now discuss how a script using the numdiff
function can be
updated to use the numderivative
function.
Consider the function:
and the point:
Therefore, the statement:
can be replaced with
If having exactly the same step is important, we force the step to the same value as in
numdiff
:
Version | Description |
5.5.0 | Tagged as obsolete. Will be removed in Scilab 6.0.0. |
5.5.2 | numdiff was removed after Scilab 5.5.2.
numderivative replaces it. |