[Scilab-users] Numderivative and integrate

Pinçon Bruno bruno.pincon at univ-lorraine.fr
Fri Dec 15 15:26:02 CET 2017


Le 15/12/2017 à 11:37, Hermes a écrit :
> He,
> here I put a solution for differentiation
>    in your case we obtain The duration for Numederivative = 1.18248
> and for this other: The duration for Numederivative = 0.00125101
> the FourMatDiff function could be improved.
> Is there any function in Scilab to create band Matrices?

     Hi,

   you could use sparse matrix. If (as it seems the case) your matrix
   can be defined by its diagonals maybe the following function could
   be useful for you. It lets you to build fastly a sparse matrix by 
given the
   different diagonals and if the diagonal is constant only the constant
   could be provided, for instance if you want to build the classical
   matrix with 2 on the diag and -1 on the first (upper and lower) diagonals

   n = 6;  // a really small example
   A = my_spdiags([n,n],0,2,-1,-1,1,-1)


   If one diagonal of the matrix is not a constant you should provide
   the entire vector of the diagonal values (instead of the constant).

   hth
  Bruno

  PS : I coded this function a long time ago it's possible that there is
  such an internal function in scilab now and I have not tested it on
  scilab-6.x

===================================================

   function A = my_spdiags(dims,varargin)
    // a function to build easily a sparse matrix by filling some given
    // diagonals.
    //
    // 1/ dims is a vector with matrix dimensions [number of rows, 
number of columns]
    // 2/ the special varargin keyword stands for the other input
    //    arguments. Any number of (valid) diagonals is allowed and
    //    each diagonal is introduced with (first) its
    //    diagonal number then (second) by the vector of values of the 
diagonal.
    //    When the diagonal is to be filled by a same value it is allowed to
    //    replace the vector of values by the scalar.
    //    The diagonal number is defined (as usual) by k = j-i
    //
    // So the function usage is:
    // A = my_spdiags([m,n], num_diag1, val_diag1, num_diag2,val_diag2,...)
    //
    // AUTHOR : Bruno Pinçon bruno.pincon at univ-lorraine.fr

    if ~(typeof(dims) == "constant" & length(dims)==2 & 
and(floor(dims)==dims)...
     & min(dims) >= 1 ) then
       error("bad first argument")
    end
    m = dims(1); n = dims(2);

    M = length(varargin)
    if modulo(M,2) ~= 0 then, error("bad entries..."), end
    nd = M/2;

    ij = []; val = [];

    for i = 1:2:M-1
       k = varargin(i);
       values =  varargin(i+1);
       if k < -(m-1) | k > (n-1) then
      error(msprintf("argument %d is not a good diag number",i+1))
       end
       if k >= 0 then
      nbelem = min(m,n-k);
      i = (1:nbelem)'; j = i+k;
       else
      nbelem = min(m+k,n);
      j = (1:nbelem)'; i = j-k;
       end
       nv = length(values)
       if nv==1 then       // a scalar was given, build the 
corresponding vector
      values = values*ones(nbelem,1);
       elseif nv == nbelem then
      values = values(:)
       else                // the vector has not the same length than 
the diagonal
      error(msprintf("argument %d has not the good size",i+2));
       end
       ij = [ij;[i,j]]; val = [val;values];
    end
    A = sparse(ij,val,dims)
endfunction





More information about the users mailing list