Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
5.5.1 - English

Change language to:
Français - 日本語 - Português - Русский

Please note that the recommended version of Scilab is 2024.1.0. This page might be outdated.
However, this page did not exist in the previous stable version.

Scilab Help >> Differential calculus, Integration > numdiff


numerical gradient estimation at one point. This function is obsolete. Please use the numderivative function instead.

Calling Sequence

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)

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

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);

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 — cubic spline evaluation function
  • interp2d — bicubic spline (2d) evaluation function
  • splin — cubic spline interpolation
  • eval_cshep2d — bidimensional cubic shepard interpolation evaluation
  • optim — non-linear optimization routine
  • diff — Difference and discrete derivative
  • derivative — approximate derivatives of a function. This function is obsolete. Please use the numderivative function instead.
  • numderivative — approximate derivatives of a function (Jacobian or Hessian)
  • external — Scilab Object, external function or routine


5.5.0 Tagged as obsolete. Will be removed in Scilab 6.0.0.


We now discuss how a script using the numdiff function can be updated to use the numderivative function.

Consider the function:

function f=myfun(x)
f = x(1)*x(1)+x(1)*x(2)

and the point:

x = [5 8]

Therefore, the statement:

g1 = numdiff(myfun, x)

can be replaced with

g2 = numderivative(myfun, x)

If having exactly the same step is important, we force the step to the same value as in numdiff:

x = [5 8];
h = sqrt(%eps)*(1+1d-3*abs(x))
g1 = numdiff(myfun, x)
g2 = numderivative(myfun, x, h)
g1 == g2
Report an issue
<< inttrap Differential calculus, Integration ode >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Thu Oct 02 13:46:47 CEST 2014