Derivatives In Scilab

In this page, we review the derivatives functions in Scilab. We review the functions in Scilab and in external modules.

These examples must be used under the terms of the CeCILL.

Functions in Scilab

In this section, we review the functions which are built in Scilab.

Overview

The following table presents the functions which performs differenciation in Scilab.

diff

discrete derivative

derivat

derivatives of a polynomial or a rational polynomial

numdiff

approximate Jacobian with finite differences

derivative

approximate Jacobian and Hessian with finite differences

Finite differences

In this section, we present the derivative and numdiff functions which are both based on finite differences.

The following session is a simple example for the derivative function. We consider a function which takes x, a 3-by-1 vector, and returns y, a 2-by-1 vector. We compute the Jacobian and the Hessian matrix at the point x=[1;2;3].

function y=F(x)
    f1 = sin(x(1)*x(2))+exp(x(2)*x(3)+x(1)) 
    f2 = sum(x.^3)
    y=[f1;f2]
endfunction
x=[1;2;3];
[J,H]=derivative(F,x)

The previous session produces:

-->[J,H]=derivative(F,x)
 H  =
         column 1 to 7
    1092.996    3287.6648    2193.2663    3287.6648    9868.7896    7676.4324    2193.2663  
    6.          0.           0.           0.           12.          0.           0.         
         column 8 to 9
    7676.4324    4386.5327  
    0.           18.        
 J  =
    1095.8009    3289.4833    2193.2663  
    3.           12.          27.        

The first row of H contains the Hessian matrix of f1, while the second row of H contains the Hessian matrix of f2.

This function provides order 1, 2 and 4 formulas. It is based on a choice of the step which tries to overcome the limitations of floating point arithmetic.

The following script is an example for the numdiff function:

function y=F(x)
    f1 = sin(x(1)*x(2))+exp(x(2)*x(3)+x(1)) 
    f2 = sum(x.^3)
    y=[f1;f2]
endfunction
x=[1;2;3];
g=numdiff(F,x)

The previous session produces the following output:

-->g=numdiff(F,x)
 g  =
    1095.8009    3289.4834    2193.2663  
    3.0000003    12.          27.        

The main differences between numdiff and derivative are the following:

More details on this topic are presented in [1].

Derivatives of polynomials

The derivat function computes the derivatives of polynomials.

In the following session, we compute the first derivative of the polynomial p(s)=1/s.

-->s=poly(0,"s");
-->p = 1/s
 p  =
    1   
    -   
    s   
-->q = derivat(p)
 q  =
  - 1   
    -   
     2  
    s   

External modules

In this section, we review the external modules which are available for differenciation in Scilab.

Sciad

Benoit Hamelin developped the SCIAD module for Scilab, under the supervision of Jean-Pierre Dussault. The module is provided at:

http://www.dmi.usherb.ca/~hamelin/autodiff/html/sciad.html

This tool is based on the evaluation graph of the vectorial function Rn → Rm. This is done by overloading of operations. The list of overloaded operators is the following:

This module is provided under a BSD-like licence.

Diffcode

The Diffcode toolbox enables Scilab code differentiation using operators and primitive functions overloading.

It was developped by Xavier Jonsson and Serge Steer.

Given a Scilab code computing a variable y depending on a variable x and a direction dx it allow evaluation of y together with the directional derivative Grad(y)*dx.

It is far from complete, but supports all basic computations including matrix inversion. It is quite easy to complete adding new overloading functions in the macro directory. See the functions already defined and the help on overloading.

The diffcode module is provided on the former Toolbox center:

http://www.scilab.org/contrib/index_contrib.php?page=displayContribution&fileID=186

Scilab2C

Sci2C is a tool capable to translate Scilab code into C code.

This toolbox Bruno Jofret, Allan Simon, Raffaele Nutricato, Alberto Morea and Maria Teresa Chiarada.

It is available under ATOMS

http://atoms.scilab.org/toolboxes/scilab2c

The output C code is in a plain style and does not include any part of the Scilab interpreter thus making it small, efficient and easy to interface to the hArtes tool chain.

In other words, the generated C code is standalone and minimal in the sense that Scilab interpreter is no longer needed and only the minimal number of C files which are necessary to execute the application are generated. It follows that the generated code can be embedded in processors or used as entries for other software.

Once the C source code is generated, it may be possible to generate a C source code computing the derivatives, for example with an automatic differentiation tool. A list of such tools is provided at:

http://www.autodiff.org/

Bibliography

public: DerivativesInScilab (last edited 2011-12-08 11:26:32 by favignana)