Scilab Home page | Wiki | Bug tracker | Forge | Mailing list archives | ATOMS | File exchange
Change language to: English - Français - 日本語 - Русский

Ajuda do Scilab >> Equações Diferenciais > numdiff

numdiff

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

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.

Examples

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

// [  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]

• interp — função de avaliação de spline cúbico
• interp2d — função de avaliação spline bicúbica (2d)
• splin — interpolação por spline cúbico
• eval_cshep2d — avaliação por interpolação cúbica bidimensional de Shepard
• optim — non-linear optimization routine
• diff — diferença e derivada discreta
• 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 — objeto Scilab, função ou rotina externa

History

 Versão Descrição 5.5.0 Tagged as obsolete. Will be removed in Scilab 6.0.0.

Appendix

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

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