numdiff numerical gradient estimation at one point Calling Sequence g = numdiff(fun, x [,dx]) Arguments fun an external, Scilab function or list. See below for calling sequence, see also external for details about external functions. f: Rn --> Rp x a vector of the `n` coordinates of the single point at which the gradient is sought. dx a vector, the finite difference step. Default value is `dx = sqrt(%eps)*(1+1d-3*abs(x))`. g a matrix, the estimated gradient at the locus x. Description Given a function `fun(x)` from Rn to Rp computes the `p x n` matrix g such that 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. Examples 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] ]]> See Also interp interp2d splin eval_cshep2d optim diff derivative external