This is octave.info, produced by makeinfo version 7.3 from octave.texi.

INFO-DIR-SECTION Math
START-INFO-DIR-ENTRY
* Octave: (octave).             Interactive language for numerical computations.
END-INFO-DIR-ENTRY

Copyright © 1996-2026 The Octave Project Developers

   Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.

   Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided that the
entire resulting derived work is distributed under the terms of a
permission notice identical to this one.

   Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions.


File: octave.info,  Node: Iterative Techniques,  Next: Real Life Example,  Prev: Sparse Linear Algebra,  Up: Sparse Matrices

22.3 Iterative Techniques Applied to Sparse Matrices
====================================================

The left division ‘\’ and right division ‘/’ operators, discussed in the
previous section, use direct solvers to resolve a linear equation of the
form ‘X = A \ B’ or ‘X = B / A’.  Octave also includes a number of
functions to solve sparse linear equations using iterative techniques.

 -- : X = pcg (A, B, TOL, MAXIT, M1, M2, X0, ...)
 -- : X = pcg (A, B, TOL, MAXIT, M, [], X0, ...)
 -- : [X, FLAG, RELRES, ITER, RESVEC, EIGEST] = pcg (A, B, ...)

     Solve the linear system of equations ‘A * X = B’ by means of the
     Preconditioned Conjugate Gradient iterative method.

     The input arguments are:

        • A is the matrix of the linear system and it must be square.  A
          can be passed as a matrix, function handle, or inline function
          ‘Afcn’ such that ‘Afcn(x) = A * x’.  Additional parameters to
          ‘Afcn’ may be passed after X0.

          A has to be Hermitian and Positive Definite (HPD).  If ‘pcg’
          detects A not to be positive definite, a warning is printed
          and the FLAG output is set.

        • B is the right-hand side vector.

        • TOL is the required relative tolerance for the residual error,
          ‘B - A * X’.  The iteration stops if
          ‘norm (B - A * X)’ ≤ ‘TOL * norm (B)’.  If TOL is omitted or
          empty, then a tolerance of 1e-6 is used.

        • MAXIT is the maximum allowed number of iterations; if MAXIT is
          omitted or empty then a value of 20 is used.

        • M is a HPD preconditioning matrix.  For any decomposition ‘M =
          P1 * P2’ such that ‘inv (P1) * A * inv (P2)’ is HPD, the
          conjugate gradient method is formally applied to the linear
          system ‘inv (P1) * A * inv (P2) * Y = inv (P1) * B’, with ‘X =
          inv (P2) * Y’ (split preconditioning).  In practice, at each
          iteration of the conjugate gradient method a linear system
          with matrix M is solved with ‘mldivide’.  If a particular
          factorization ‘M = M1 * M2’ is available (for instance, an
          incomplete Cholesky factorization of A), the two matrices M1
          and M2 can be passed and the relative linear systems are
          solved with the ‘mldivide’ operator.  Note that a proper
          choice of the preconditioner may dramatically improve the
          overall performance of the method.  Instead of matrices M1 and
          M2, the user may pass two functions which return the results
          of applying the inverse of M1 and M2 to a vector.  If M1 is
          omitted or empty ‘[]’, then no preconditioning is applied.  If
          no factorization of M is available, M2 can be omitted or left
          [], and the input variable M1 can be used to pass the
          preconditioner M.

        • X0 is the initial guess.  If X0 is omitted or empty then the
          function sets X0 to a zero vector by default.

     The arguments which follow X0 are treated as parameters, and passed
     in an appropriate manner to any of the functions (A or M1 or M2)
     that have been given to ‘pcg’.  See the examples below for further
     details.

     The output arguments are:

        • X is the computed approximation to the solution of
          ‘A * X = B’.  If the algorithm did not converge, then X is the
          iteration which has the minimum residual.

        • FLAG reports on the convergence:

             • 0: The algorithm converged to within the prescribed
               tolerance.

             • 1: The algorithm did not converge and it reached the
               maximum number of iterations.

             • 2: The preconditioner matrix is singular.

             • 3: The algorithm stagnated, i.e., the absolute value of
               the difference between the current iteration X and the
               previous is less than ‘EPS * norm (X,2)’.

             • 4: The algorithm detects that the input (preconditioned)
               matrix is not HPD.

        • RELRES is the ratio of the final residual to its initial
          value, measured in the Euclidean norm.

        • ITER indicates the iteration of X which it was computed.
          Since the output X corresponds to the minimal residual
          solution, the total number of iterations that the method
          performed is given by ‘length(resvec) - 1’.

        • RESVEC describes the convergence history of the method.
          ‘RESVEC (I, 1)’ is the Euclidean norm of the residual, and
          ‘RESVEC (I, 2)’ is the preconditioned residual norm, after the
          (I-1)-th iteration, ‘I = 1, 2, ..., ITER+1’.  The
          preconditioned residual norm is defined as ‘R' * (M \ R)’
          where ‘R = B - A * X’, see also the description of M.  If
          EIGEST is not required, only ‘RESVEC (:, 1)’ is returned.

        • EIGEST returns the estimate for the smallest ‘EIGEST(1)’ and
          largest ‘EIGEST(2)’ eigenvalues of the preconditioned matrix
          ‘P = M \ A’.  In particular, if no preconditioning is used,
          the estimates for the extreme eigenvalues of A are returned.
          ‘EIGEST(1)’ is an overestimate and ‘EIGEST(2)’ is an
          underestimate, so that ‘EIGEST(2) / EIGEST(1)’ is a lower
          bound for ‘cond (P, 2)’, which nevertheless in the limit
          should theoretically be equal to the actual value of the
          condition number.

     Let us consider a trivial problem with a tridiagonal matrix

          n = 10;
          A = toeplitz (sparse ([1, 1], [1, 2], [2, 1], 1, n));
          b = A * ones (n, 1);
          M1 = ichol (A); # in this tridiagonal case it corresponds to chol (A)'
          M2 = M1';
          M = M1 * M2;
          Afcn = @(x) A * x;
          Mfcn = @(x) M \ x;
          M1fcn = @(x) M1 \ x;
          M2fcn = @(x) M2 \ x;

     EXAMPLE 1: Simplest use of ‘pcg’

          x = pcg (A, b)

     EXAMPLE 2: ‘pcg’ with a function which computes ‘A * X’

          x = pcg (Afcn, b)

     EXAMPLE 3: ‘pcg’ with a preconditioner matrix M

          x = pcg (A, b, 1e-06, 100, M)

     EXAMPLE 4: ‘pcg’ with a function as preconditioner

          x = pcg (Afcn, b, 1e-6, 100, Mfcn)

     EXAMPLE 5: ‘pcg’ with preconditioner matrices M1 and M2

          x = pcg (A, b, 1e-6, 100, M1, M2)

     EXAMPLE 6: ‘pcg’ with functions as preconditioners

          x = pcg (Afcn, b, 1e-6, 100, M1fcn, M2fcn)

     EXAMPLE 7: ‘pcg’ with as input a function requiring an argument

            function y = Ap (A, x, p) # compute A^p * x
               y = x;
               for i = 1:p
                 y = A * y;
               endfor
            endfunction
          Apfcn = @(x, p) Ap (A, x, p);
          x = pcg (Apfcn, b, [], [], [], [], [], 2);

     EXAMPLE 8: explicit example to show that ‘pcg’ uses a split
     preconditioner

          M1 = ichol (A + 0.1 * eye (n)); # factorization of A perturbed
          M2 = M1';
          M = M1 * M2;

          ## reference solution computed by pcg after two iterations
          [x_ref, fl] = pcg (A, b, [], 2, M)

          ## split preconditioning
          [y, fl] = pcg ((M1 \ A) / M2, M1 \ b, [], 2)
          x = M2 \ y # compare x and x_ref

     References:

       1. C.T. Kelley, ‘Iterative Methods for Linear and Nonlinear
          Equations’, SIAM, 1995.  (the base PCG algorithm)

       2. Y. Saad, ‘Iterative Methods for Sparse Linear Systems’, PWS,
          1996.  (condition number estimate from PCG) Revised version of
          this book is available online at
          <https://www-users.cs.umn.edu/~saad/books.html>

     See also: *note sparse: XREFsparse, *note pcr: XREFpcr, *note
     gmres: XREFgmres, *note bicg: XREFbicg, *note bicgstab:
     XREFbicgstab, *note cgs: XREFcgs.

 -- : X = pcr (A, B, TOL, MAXIT, M, X0, ...)
 -- : [X, FLAG, RELRES, ITER, RESVEC] = pcr (...)

     Solve the linear system of equations ‘A * X = B’ by means of the
     Preconditioned Conjugate Residuals iterative method.

     The input arguments are

        • A can be either a square (preferably sparse) matrix or a
          function handle, inline function or string containing the name
          of a function which computes ‘A * X’.  In principle A should
          be symmetric and non-singular; if ‘pcr’ finds A to be
          numerically singular, you will get a warning message and the
          FLAG output parameter will be set.

        • B is the right hand side vector.

        • TOL is the required relative tolerance for the residual error,
          ‘B - A * X’.  The iteration stops if ‘norm (B - A * X) <= TOL
          * norm (B - A * X0)’.  If TOL is empty or is omitted, the
          function sets ‘TOL = 1e-6’ by default.

        • MAXIT is the maximum allowable number of iterations; if ‘[]’
          is supplied for MAXIT, or ‘pcr’ has less arguments, a default
          value equal to 20 is used.

        • M is the (left) preconditioning matrix, so that the iteration
          is (theoretically) equivalent to solving by ‘pcr’ ‘P * X = M \
          B’, with ‘P = M \ A’.  Note that a proper choice of the
          preconditioner may dramatically improve the overall
          performance of the method.  Instead of matrix M, the user may
          pass a function which returns the results of applying the
          inverse of M to a vector (usually this is the preferred way of
          using the preconditioner).  If ‘[]’ is supplied for M, or M is
          omitted, no preconditioning is applied.

        • X0 is the initial guess.  If X0 is empty or omitted, the
          function sets X0 to a zero vector by default.

     The arguments which follow X0 are treated as parameters, and passed
     in a proper way to any of the functions (A or M) which are passed
     to ‘pcr’.  See the examples below for further details.

     The output arguments are

        • X is the computed approximation to the solution of ‘A * X =
          B’.

        • FLAG reports on the convergence.  ‘FLAG = 0’ means the
          solution converged and the tolerance criterion given by TOL is
          satisfied.  ‘FLAG = 1’ means that the MAXIT limit for the
          iteration count was reached.  ‘FLAG = 3’ reports a ‘pcr’
          breakdown, see [1] for details.

        • RELRES is the ratio of the final residual to its initial
          value, measured in the Euclidean norm.

        • ITER is the actual number of iterations performed.

        • RESVEC describes the convergence history of the method, so
          that ‘RESVEC (i)’ contains the Euclidean norms of the residual
          after the (I-1)-th iteration, ‘I = 1,2, ..., ITER+1’.

     Let us consider a trivial problem with a diagonal matrix (we
     exploit the sparsity of A)

          n = 10;
          A = sparse (diag (1:n));
          b = rand (N, 1);

     EXAMPLE 1: Simplest use of ‘pcr’

          x = pcr (A, b)

     EXAMPLE 2: ‘pcr’ with a function which computes ‘A * X’.

          function y = apply_a (x)
            y = [1:10]' .* x;
          endfunction

          x = pcr ("apply_a", b)

     EXAMPLE 3: Preconditioned iteration, with full diagnostics.  The
     preconditioner (quite strange, because even the original matrix A
     is trivial) is defined as a function

          function y = apply_m (x)
            k = floor (length (x) - 2);
            y = x;
            y(1:k) = x(1:k) ./ [1:k]';
          endfunction

          [x, flag, relres, iter, resvec] = ...
                             pcr (A, b, [], [], "apply_m")
          semilogy ([1:iter+1], resvec);

     EXAMPLE 4: Finally, a preconditioner which depends on a parameter
     K.

          function y = apply_m (x, varargin)
            k = varargin{1};
            y = x;
            y(1:k) = x(1:k) ./ [1:k]';
          endfunction

          [x, flag, relres, iter, resvec] = ...
                             pcr (A, b, [], [], "apply_m"', [], 3)

     Reference:

     W. Hackbusch, ‘Iterative Solution of Large Sparse Systems of
     Equations’, section 9.5.4, Springer, 1994.

     See also: *note sparse: XREFsparse, *note pcg: XREFpcg.

   The speed with which an iterative solver converges to a solution can
be accelerated with the use of a pre-conditioning matrix M.  In this
case the linear equation ‘M^-1 * X = M^-1 * A \ B’ is solved instead.
Typical pre-conditioning matrices are partial factorizations of the
original matrix.

 -- : L = ichol (A)
 -- : L = ichol (A, OPTS)

     Compute the incomplete Cholesky factorization of the sparse square
     matrix A.

     By default, ‘ichol’ uses only the lower triangle of A and produces
     a lower triangular factor L such that L*L' approximates A.

     The factor given by this routine may be useful as a preconditioner
     for a system of linear equations being solved by iterative methods
     such as PCG (Preconditioned Conjugate Gradient).

     The factorization may be modified by passing options in a structure
     OPTS.  The option name is a field of the structure and the setting
     is the value of field.  Names and specifiers are case sensitive.

     type
          Type of factorization.

          "nofill" (default)
               Incomplete Cholesky factorization with no fill-in
               (IC(0)).

          "ict"
               Incomplete Cholesky factorization with threshold dropping
               (ICT).

     diagcomp
          A non-negative scalar ALPHA for incomplete Cholesky
          factorization of ‘A + ALPHA * diag (diag (A))’ instead of A.
          This can be useful when A is not positive definite.  The
          default value is 0.

     droptol
          A non-negative scalar specifying the drop tolerance for
          factorization if performing ICT.  The default value is 0 which
          produces the complete Cholesky factorization.

          Non-diagonal entries of L are set to 0 unless

          ‘abs (L(i,j)) >= droptol * norm (A(j:end, j), 1)’.

     michol
          Modified incomplete Cholesky factorization:

          "off" (default)
               Row and column sums are not necessarily preserved.

          "on"
               The diagonal of L is modified so that row (and column)
               sums are preserved even when elements have been dropped
               during the factorization.  The relationship preserved is:
               ‘A * e = L * L' * e’, where e is a vector of ones.

     shape

          "lower" (default)
               Use only the lower triangle of A and return a lower
               triangular factor L such that L*L' approximates A.

          "upper"
               Use only the upper triangle of A and return an upper
               triangular factor U such that ‘U'*U’ approximates A.

     EXAMPLES

     The following problem demonstrates how to factorize a sample
     symmetric positive definite matrix with the full Cholesky
     decomposition and with the incomplete one.

          A = [ 0.37, -0.05,  -0.05,  -0.07;
               -0.05,  0.116,  0.0,   -0.05;
               -0.05,  0.0,    0.116, -0.05;
               -0.07, -0.05,  -0.05,   0.202];
          A = sparse (A);
          nnz (tril (A))
          ans =  9
          L = chol (A, "lower");
          nnz (L)
          ans =  10
          norm (A - L * L', "fro") / norm (A, "fro")
          ans =  1.1993e-16
          opts.type = "nofill";
          L = ichol (A, opts);
          nnz (L)
          ans =  9
          norm (A - L * L', "fro") / norm (A, "fro")
          ans =  0.019736

     Another example for decomposition is a finite difference matrix
     used to solve a boundary value problem on the unit square.

          nx = 400; ny = 200;
          hx = 1 / (nx + 1); hy = 1 / (ny + 1);
          Dxx = spdiags ([ones(nx, 1), -2*ones(nx, 1), ones(nx, 1)],
                         [-1 0 1 ], nx, nx) / (hx ^ 2);
          Dyy = spdiags ([ones(ny, 1), -2*ones(ny, 1), ones(ny, 1)],
                         [-1 0 1 ], ny, ny) / (hy ^ 2);
          A = -kron (Dxx, speye (ny)) - kron (speye (nx), Dyy);
          nnz (tril (A))
          ans =  239400
          opts.type = "nofill";
          L = ichol (A, opts);
          nnz (tril (A))
          ans =  239400
          norm (A - L * L', "fro") / norm (A, "fro")
          ans =  0.062327

     References for implemented algorithms:

     [1] Y. Saad, "Preconditioning Techniques", ‘Iterative Methods for
     Sparse Linear Systems’, PWS Publishing Company, 1996.

     [2] M. Jones, P. Plassmann, ‘An Improved Incomplete Cholesky
     Factorization’, 1992.

     See also: *note chol: XREFchol, *note ilu: XREFilu, *note pcg:
     XREFpcg.

 -- : LUA = ilu (A)
 -- : LUA = ilu (A, OPTS)
 -- : [L, U] = ilu (...)
 -- : [L, U, P] = ilu (...)

     Compute the incomplete LU factorization of the sparse square matrix
     A.

     ‘ilu’ returns a unit lower triangular matrix L, an upper triangular
     matrix U, and optionally a permutation matrix P, such that ‘L*U’
     approximates ‘P*A’.

     The factors given by this routine may be useful as preconditioners
     for a system of linear equations being solved by iterative methods
     such as BICG (BiConjugate Gradients) or GMRES (Generalized Minimum
     Residual Method).

     The factorization may be modified by passing options in a structure
     OPTS.  The option name is a field of the structure and the setting
     is the value of field.  Names and specifiers are case sensitive.

     ‘type’
          Type of factorization.

          "nofill" (default)
               ILU factorization with no fill-in (ILU(0)).

               Additional supported options: ‘milu’.

          "crout"
               Crout version of ILU factorization (ILUC).

               Additional supported options: ‘milu’, ‘droptol’.

          "ilutp"
               ILU factorization with threshold and pivoting.

               Additional supported options: ‘milu’, ‘droptol’, ‘udiag’,
               ‘thresh’.

     ‘droptol’
          A non-negative scalar specifying the drop tolerance for
          factorization.  The default value is 0 which produces the
          complete LU factorization.

          Non-diagonal entries of U are set to 0 unless

          ‘abs (U(i,j)) >= droptol * norm (A(:,j))’.

          Non-diagonal entries of L are set to 0 unless

          ‘abs (L(i,j)) >= droptol * norm (A(:,j))/U(j,j)’.

     ‘milu’
          Modified incomplete LU factorization:

          "row"
               Row-sum modified incomplete LU factorization.  The
               factorization preserves row sums: ‘A * e = L * U * e’,
               where e is a vector of ones.

          "col"
               Column-sum modified incomplete LU factorization.  The
               factorization preserves column sums: ‘e' * A = e' * L *
               U’.

          "off" (default)
               Row and column sums are not necessarily preserved.

     ‘udiag’
          If true, any zeros on the diagonal of the upper triangular
          factor are replaced by the local drop tolerance ‘droptol *
          norm (A(:,j))/U(j,j)’.  The default is false.

     ‘thresh’
          Pivot threshold for factorization.  It can range between 0
          (diagonal pivoting) and 1 (default), where the maximum
          magnitude entry in the column is chosen to be the pivot.

     If ‘ilu’ is called with just one output, the returned matrix is ‘L
     + U - speye (size (A))’, where L is unit lower triangular and U is
     upper triangular.

     With two outputs, ‘ilu’ returns a unit lower triangular matrix L
     and an upper triangular matrix U.  For OPTS.type == "ilutp", one of
     the factors is permuted based on the value of OPTS.milu.  When
     OPTS.milu == "row", U is a column permuted upper triangular factor.
     Otherwise, L is a row-permuted unit lower triangular factor.

     If there are three named outputs and OPTS.milu != "row", P is
     returned such that L and U are incomplete factors of ‘P*A’.  When
     OPTS.milu == "row", P is returned such that L and U are incomplete
     factors of ‘A*P’.

     EXAMPLES

          A = gallery ("neumann", 1600) + speye (1600);
          opts.type = "nofill";
          nnz (A)
          ans = 7840

          nnz (lu (A))
          ans = 126478

          nnz (ilu (A, opts))
          ans = 7840

     This shows that A has 7,840 nonzeros, the complete LU factorization
     has 126,478 nonzeros, and the incomplete LU factorization, with 0
     level of fill-in, has 7,840 nonzeros, the same amount as A.  Taken
     from: <https://www.mathworks.com/help/matlab/ref/ilu.html>

          A = gallery ("wathen", 10, 10);
          b = sum (A, 2);
          tol = 1e-8;
          maxit = 50;
          opts.type = "crout";
          opts.droptol = 1e-4;
          [L, U] = ilu (A, opts);
          x = bicg (A, b, tol, maxit, L, U);
          norm (A * x - b, inf)

     This example uses ILU as preconditioner for a random FEM-Matrix,
     which has a large condition number.  Without L and U BICG would not
     converge.

     See also: *note lu: XREFlu, *note ichol: XREFichol, *note bicg:
     XREFbicg, *note gmres: XREFgmres.


File: octave.info,  Node: Real Life Example,  Prev: Iterative Techniques,  Up: Sparse Matrices

22.4 Real Life Example using Sparse Matrices
============================================

A common application for sparse matrices is in the solution of Finite
Element Models.  Finite element models allow numerical solution of
partial differential equations that do not have closed form solutions,
typically because of the complex shape of the domain.

   In order to motivate this application, we consider the boundary value
Laplace equation.  This system can model scalar potential fields, such
as heat or electrical potential.  Given a medium Omega with boundary
dOmega.  At all points on the dOmega the boundary conditions are known,
and we wish to calculate the potential in Omega.  Boundary conditions
may specify the potential (Dirichlet boundary condition), its normal
derivative across the boundary (Neumann boundary condition), or a
weighted sum of the potential and its derivative (Cauchy boundary
condition).

   In a thermal model, we want to calculate the temperature in Omega and
know the boundary temperature (Dirichlet condition) or heat flux (from
which we can calculate the Neumann condition by dividing by the thermal
conductivity at the boundary).  Similarly, in an electrical model, we
want to calculate the voltage in Omega and know the boundary voltage
(Dirichlet) or current (Neumann condition after diving by the electrical
conductivity).  In an electrical model, it is common for much of the
boundary to be electrically isolated; this is a Neumann boundary
condition with the current equal to zero.

   The simplest finite element models will divide Omega into simplexes
(triangles in 2-D, pyramids in 3-D).

   The following example creates a simple rectangular 2-D electrically
conductive medium with 10 V and 20 V imposed on opposite sides
(Dirichlet boundary conditions).  All other edges are electrically
isolated.

        node_y = [1;1.2;1.5;1.8;2]*ones(1,11);
        node_x = ones(5,1)*[1,1.05,1.1,1.2, ...
                  1.3,1.5,1.7,1.8,1.9,1.95,2];
        nodes = [node_x(:), node_y(:)];

        [h,w] = size (node_x);
        elems = [];
        for idx = 1:w-1
          widx = (idx-1)*h;
          elems = [elems; ...
            widx+[(1:h-1);(2:h);h+(1:h-1)]'; ...
            widx+[(2:h);h+(2:h);h+(1:h-1)]' ];
        endfor

        E = size (elems,1);  # No. of simplices
        N = size (nodes,1);  # No. of vertices
        D = size (elems,2);  # dimensions+1

   This creates a N-by-2 matrix ‘nodes’ and a E-by-3 matrix ‘elems’ with
values, which define finite element triangles:

       nodes(1:7,:)'
         1.00 1.00 1.00 1.00 1.00 1.05 1.05 ...
         1.00 1.20 1.50 1.80 2.00 1.00 1.20 ...

       elems(1:7,:)'
         1    2    3    4    2    3    4 ...
         2    3    4    5    7    8    9 ...
         6    7    8    9    6    7    8 ...

   Using a first order FEM, we approximate the electrical conductivity
distribution in Omega as constant on each simplex (represented by the
vector ‘conductivity’).  Based on the finite element geometry, we first
calculate a system (or stiffness) matrix for each simplex (represented
as 3-by-3 elements on the diagonal of the element-wise system matrix
‘SE’).  Based on ‘SE’ and a N-by-DE connectivity matrix ‘C’,
representing the connections between simplices and vertices, the global
connectivity matrix ‘S’ is calculated.

       ## Element conductivity
       conductivity = [1*ones(1,16), ...
              2*ones(1,48), 1*ones(1,16)];

       ## Connectivity matrix
       C = sparse ((1:D*E), reshape (elems', ...
              D*E, 1), 1, D*E, N);

       ## Calculate system matrix
       Siidx = floor ([0:D*E-1]'/D) * D * ...
              ones(1,D) + ones(D*E,1)*(1:D) ;
       Sjidx = [1:D*E]'*ones (1,D);
       Sdata = zeros (D*E,D);
       dfact = factorial (D-1);
       for j = 1:E
          a = inv ([ones(D,1), ...
              nodes(elems(j,:), :)]);
          const = conductivity(j) * 2 / ...
              dfact / abs (det (a));
          Sdata(D*(j-1)+(1:D),:) = const * ...
              a(2:D,:)' * a(2:D,:);
       endfor
       ## Element-wise system matrix
       SE = sparse(Siidx,Sjidx,Sdata);
       ## Global system matrix
       S = C'* SE *C;

   The system matrix acts like the conductivity ‘S’ in Ohm's law ‘S * V
= I’.  Based on the Dirichlet and Neumann boundary conditions, we are
able to solve for the voltages at each vertex ‘V’.

       ## Dirichlet boundary conditions
       D_nodes = [1:5, 51:55];
       D_value = [10*ones(1,5), 20*ones(1,5)];

       V = zeros (N,1);
       V(D_nodes) = D_value;
       idx = 1:N; # vertices without Dirichlet
                  # boundary condns
       idx(D_nodes) = [];

       ## Neumann boundary conditions.  Note that
       ## N_value must be normalized by the
       ## boundary length and element conductivity
       N_nodes = [];
       N_value = [];

       Q = zeros (N,1);
       Q(N_nodes) = N_value;

       V(idx) = S(idx,idx) \ ( Q(idx) - ...
                 S(idx,D_nodes) * V(D_nodes));

   Finally, in order to display the solution, we show each solved
voltage value in the z-axis for each simplex vertex.

       elemx = elems(:,[1,2,3,1])';
       xelems = reshape (nodes(elemx, 1), 4, E);
       yelems = reshape (nodes(elemx, 2), 4, E);
       velems = reshape (V(elemx), 4, E);
       plot3 (xelems,yelems,velems,"k");
       print "grid.eps";


File: octave.info,  Node: Numerical Integration,  Next: Differential Equations,  Prev: Sparse Matrices,  Up: Top

23 Numerical Integration
************************

Octave comes with several built-in functions for computing the integral
of a function numerically (termed quadrature).  These functions all
solve 1-dimensional integration problems.

* Menu:

* Functions of One Variable::
* Orthogonal Collocation::
* Functions of Multiple Variables::


File: octave.info,  Node: Functions of One Variable,  Next: Orthogonal Collocation,  Up: Numerical Integration

23.1 Functions of One Variable
==============================

Octave supports five different adaptive quadrature algorithms for
computing the integral of a function f over the interval from a to b.
These are

‘quad’
     Numerical integration based on Gaussian quadrature.

‘quadv’
     Numerical integration using an adaptive vectorized Simpson's rule.

‘quadl’
     Numerical integration using an adaptive Lobatto rule.

‘quadgk’
     Numerical integration using an adaptive Gauss-Konrod rule.

‘quadcc’
     Numerical integration using adaptive Clenshaw-Curtis rules.

     In addition, the following functions are also provided:

‘integral’
     A compatibility wrapper function that will choose between ‘quadv’
     and ‘quadgk’ depending on the integrand and options chosen.

‘trapz, cumtrapz’
     Numerical integration of data using the trapezoidal method.

The best quadrature algorithm to use depends on the integrand.  If you
have empirical data, rather than a function, the choice is ‘trapz’ or
‘cumtrapz’.  If you are uncertain about the characteristics of the
integrand, ‘quadcc’ will be the most robust as it can handle
discontinuities, singularities, oscillatory functions, and infinite
intervals.  When the integrand is smooth ‘quadgk’ may be the fastest of
the algorithms.

     Function    Characteristics
----------------------------------------------------------------------------
     quad        Low accuracy with nonsmooth integrands
     quadv       Medium accuracy with smooth integrands
     quadl       Medium accuracy with smooth integrands.  Slower than
                 quadgk.
     quadgk      Medium accuracy (1e-6 - 1e-9) with smooth integrands.
                 Handles oscillatory functions and infinite bounds
     quadcc      Low to High accuracy with nonsmooth/smooth integrands
                 Handles oscillatory functions, singularities, and
                 infinite bounds

   Here is an example of using ‘quad’ to integrate the function

       F(X) = X * sin (1/X) * sqrt (abs (1 - X))

from X = 0 to X = 3.

   This is a fairly difficult integration (plot the function over the
range of integration to see why).

   The first step is to define the function:

     function y = f (x)
       y = x .* sin (1./x) .* sqrt (abs (1 - x));
     endfunction

   Note the use of the 'dot' forms of the operators.  This is not
necessary for the ‘quad’ integrator, but is required by the other
integrators.  In any case, it makes it much easier to generate a set of
points for plotting because it is possible to call the function with a
vector argument to produce a vector result.

   The second step is to call quad with the limits of integration:

     [q, ier, nfun, err] = quad ("f", 0, 3)
          ⇒ 1.9819
          ⇒ 1
          ⇒ 5061
          ⇒ 1.1522e-07

   Although ‘quad’ returns a nonzero value for IER, the result is
reasonably accurate (to see why, examine what happens to the result if
you move the lower bound to 0.1, then 0.01, then 0.001, etc.).

   The function "f" can be the string name of a function or a function
handle.  These options make it quite easy to do integration without
having to fully define a function in an m-file.  For example:

     # Verify gamma function = (n-1)! for n = 4
     f = @(x) x.^3 .* exp (-x);
     quadcc (f, 0, Inf)
          ⇒ 6.0000

 -- : Q = quad (F, A, B)
 -- : Q = quad (F, A, B, TOL)
 -- : Q = quad (F, A, B, TOL, SING)
 -- : [Q, IER, NFEV, ERR] = quad (...)
     Numerically evaluate the integral of F from A to B using Fortran
     routines from QUADPACK.

     F is a function handle, inline function, or a string containing the
     name of the function to evaluate.  The function must have the form
     ‘y = f (x)’ where Y and X are scalars.

     A and B are the lower and upper limits of integration.  Either or
     both may be infinite.

     The optional argument TOL is a vector that specifies the desired
     accuracy of the result.  The first element of the vector is the
     desired absolute tolerance, and the second element is the desired
     relative tolerance.  To choose a relative test only, set the
     absolute tolerance to zero.  To choose an absolute test only, set
     the relative tolerance to zero.  Both tolerances default to ‘sqrt
     (eps)’ or approximately 1.5e-8.

     The optional argument SING is a vector of values at which the
     integrand is known to be singular.

     The result of the integration is returned in Q.

     IER contains an integer error code (0 indicates a successful
     integration).

     NFEV indicates the number of function evaluations that were made.

     ERR contains an estimate of the error in the solution.

     The function ‘quad_options’ can set other optional parameters for
     ‘quad’.

     Note: because ‘quad’ is written in Fortran it cannot be called
     recursively.  This prevents its use in integrating over more than
     one variable by routines ‘dblquad’ and ‘triplequad’.

     See also: *note quad_options: XREFquad_options, *note quadv:
     XREFquadv, *note quadl: XREFquadl, *note quadgk: XREFquadgk, *note
     quadcc: XREFquadcc, *note trapz: XREFtrapz, *note dblquad:
     XREFdblquad, *note triplequad: XREFtriplequad.

 -- : quad_options ()
 -- : val = quad_options (OPT)
 -- : quad_options (OPT, VAL)
     Query or set options for the function ‘quad’.

     When called with no arguments, the names of all available options
     and their current values are displayed.

     Given one argument, return the value of the option OPT.

     When called with two arguments, ‘quad_options’ sets the option OPT
     to value VAL.

     Options include

     "absolute tolerance"
          Absolute tolerance; may be zero for pure relative error test.

     "relative tolerance"
          Non-negative relative tolerance.  If the absolute tolerance is
          zero, the relative tolerance must be greater than or equal to
          ‘max (50*eps, 0.5e-28)’.

     "single precision absolute tolerance"
          Absolute tolerance for single precision; may be zero for pure
          relative error test.

     "single precision relative tolerance"
          Non-negative relative tolerance for single precision.  If the
          absolute tolerance is zero, the relative tolerance must be
          greater than or equal to ‘max (50*eps, 0.5e-28)’.

 -- : Q = quadv (F, A, B)
 -- : Q = quadv (F, A, B, TOL)
 -- : Q = quadv (F, A, B, TOL, TRACE)
 -- : Q = quadv (F, A, B, TOL, TRACE, P1, P2, ...)
 -- : [Q, NFEV] = quadv (...)

     Numerically evaluate the integral of F from A to B using an
     adaptive Simpson's rule.

     F is a function handle, inline function, or string containing the
     name of the function to evaluate.  ‘quadv’ is a vectorized version
     of ‘quad’ and the function defined by F must accept a scalar or
     vector as input and return a scalar, vector, or array as output.

     A and B are the lower and upper limits of integration.  Both limits
     must be finite.

     The optional argument TOL defines the absolute tolerance used to
     stop the adaptation procedure.  The default value is 1e-6.

     The algorithm used by ‘quadv’ involves recursively subdividing the
     integration interval and applying Simpson's rule on each
     subinterval.  If TRACE is true then after computing each of these
     partial integrals display: (1) the total number of function
     evaluations, (2) the left end of the subinterval, (3) the length of
     the subinterval, (4) the approximation of the integral over the
     subinterval.

     Additional arguments P1, etc., are passed directly to the function
     F.  To use default values for TOL and TRACE, one may pass empty
     matrices ([]).

     The result of the integration is returned in Q.

     The optional output NFEV indicates the total number of function
     evaluations performed.

     Note: ‘quadv’ is written in Octave's scripting language and can be
     used recursively in ‘dblquad’ and ‘triplequad’, unlike the ‘quad’
     function.

     See also: *note quad: XREFquad, *note quadl: XREFquadl, *note
     quadgk: XREFquadgk, *note quadcc: XREFquadcc, *note trapz:
     XREFtrapz, *note dblquad: XREFdblquad, *note triplequad:
     XREFtriplequad, *note integral: XREFintegral, *note integral2:
     XREFintegral2, *note integral3: XREFintegral3.

 -- : Q = quadl (F, A, B)
 -- : Q = quadl (F, A, B, TOL)
 -- : Q = quadl (F, A, B, TOL, TRACE)
 -- : Q = quadl (F, A, B, TOL, TRACE, P1, P2, ...)
 -- : [Q, NFEV] = quadl (...)

     Numerically evaluate the integral of F from A to B using an
     adaptive Lobatto rule.

     F is a function handle, inline function, or string containing the
     name of the function to evaluate.  The function F must be
     vectorized and return a vector of output values when given a vector
     of input values.

     A and B are the lower and upper limits of integration.  Both limits
     must be finite.

     The optional argument TOL defines the absolute tolerance with which
     to perform the integration.  The default value is 1e-6.

     The algorithm used by ‘quadl’ involves recursively subdividing the
     integration interval.  If TRACE is defined then for each
     subinterval display: (1) the total number of function evaluations,
     (2) the left end of the subinterval, (3) the length of the
     subinterval, (4) the approximation of the integral over the
     subinterval.

     Additional arguments P1, etc., are passed directly to the function
     F.  To use default values for TOL and TRACE, one may pass empty
     matrices ([]).

     The result of the integration is returned in Q.

     The optional output NFEV indicates the total number of function
     evaluations performed.

     Reference: W. Gander and W. Gautschi, ‘Adaptive Quadrature -
     Revisited’, BIT Vol. 40, No. 1, March 2000, pp. 84-101.
     <https://www.inf.ethz.ch/personal/gander/>

     See also: *note quad: XREFquad, *note quadv: XREFquadv, *note
     quadgk: XREFquadgk, *note quadcc: XREFquadcc, *note trapz:
     XREFtrapz, *note dblquad: XREFdblquad, *note triplequad:
     XREFtriplequad, *note integral: XREFintegral, *note integral2:
     XREFintegral2, *note integral3: XREFintegral3.

 -- : Q = quadgk (F, A, B)
 -- : Q = quadgk (F, A, B, ABSTOL)
 -- : Q = quadgk (F, A, B, ABSTOL, TRACE)
 -- : Q = quadgk (F, A, B, "PROP", VAL, ...)
 -- : [Q, ERR] = quadgk (...)

     Numerically evaluate the integral of F from A to B using adaptive
     Gauss-Kronrod quadrature.

     F is a function handle, inline function, or string containing the
     name of the function to evaluate.  The function F must be
     vectorized and return a vector of output values when given a vector
     of input values (See property "ArrayValued" for an exception to
     this rule).

     A and B are the lower and upper limits of integration.  Either or
     both limits may be infinite or contain weak end singularities.
     Variable transformation will be used to treat any infinite
     intervals and weaken the singularities.  For example:

          quadgk (@(x) 1 ./ (sqrt (x) .* (x + 1)), 0, Inf)

     Note that the formulation of the integrand uses the
     element-by-element operator ‘./’ and all user functions to ‘quadgk’
     should do the same.

     The optional argument ABSTOL defines the absolute tolerance used to
     stop the integration procedure.  The default value is 1e-10 (1e-5
     for single).

     The algorithm used by ‘quadgk’ involves subdividing the integration
     interval and evaluating each subinterval.  If TRACE is true then
     after computing each of these partial integrals display: (1) the
     number of subintervals at this step, (2) the current estimate of
     the error ERR, (3) the current estimate for the integral Q.

     The behavior of the algorithm can be configured by passing
     arguments to ‘quadgk’ as pairs "PROP", VAL.  Valid properties are

     ‘AbsTol’
          Define the absolute error tolerance for the quadrature.  The
          default absolute tolerance is 1e-10 (1e-5 for single).

     ‘RelTol’
          Define the relative error tolerance for the quadrature.  The
          default relative tolerance is 1e-6 (1e-4 for single).

     ‘ArrayValued’
          When set to true, the function F produces an array output for
          a scalar input.  The default is false which requires that F
          produce an output that is the same size as the input.  For
          example,

               quadgk (@(x) x .^ (1:5), 0, 2, "ArrayValued", 1)

          will integrate ‘[x.^1, x.^2, x.^3, x.^4, x.^5]’ in one
          function call rather than having to repeatedly define a single
          anonymous function and use a normal invocation of ‘quadgk’.

     ‘WayPoints’
          Specify points which will become endpoints for subintervals in
          the algorithm which can result in significantly improved
          estimation of the error in the integral, faster computation,
          or both.  It can be useful to specify more subintervals around
          a region where the integrand is rapidly changing or to flag
          locations where there is a discontinuity in the first
          derivative of the function.  For example, the signum function
          has a discontinuity at ‘x == 0’ and by specifying a waypoint

               quadgk (@(x) sign (x), -0.5, 1, "Waypoints", [0])

          the error bound is reduced from 4e-7 to 1e-13.

          If the function has *singularities* within the region of
          integration those should not be addressed with waypoints.
          Instead, the overall integral should be decomposed into a sum
          of several smaller integrals such that the singularity occurs
          as one of the bounds of integration in the call to ‘quadgk’.

          If any of the waypoints are complex then contour integration
          is performed as documented below.

     ‘MaxIntervalCount’
          ‘quadgk’ initially subdivides the interval on which to perform
          the quadrature into 10 intervals or, if WayPoints are given,
          at each waypoint.  Subintervals that have an unacceptable
          error are subdivided and re-evaluated.  If the number of
          subintervals exceeds 650 subintervals at any point then a poor
          convergence is signaled and the current estimate of the
          integral is returned.  The property "MaxIntervalCount" can be
          used to alter the number of subintervals that can exist before
          exiting.

     ‘Trace’
          If logically true ‘quadgk’ prints information on the
          convergence of the quadrature at each iteration.

     If any of A, B, or WAYPOINTS is complex then the quadrature is
     treated as a contour integral along a piecewise linear path defined
     by ‘[A, WAYPOINTS(1), WAYPOINTS(2), ..., B]’.  In this case the
     integral is assumed to have no edge singularities.  For example,

          quadgk (@(z) log (z), 1+1i, 1+1i, "WayPoints",
                  [-1+1i, -1-1i, +1-1i])

     integrates ‘log (z)’ along the square defined by ‘[1+1i, -1+1i,
     -1-1i, +1-1i]’.

     The result of the integration is returned in Q.

     ERR is an approximate bound on the error in the integral
     ‘abs (Q - I)’, where I is the exact value of the integral.  If the
     adaptive integration did not converge, the value of ERR will be
     larger than the requested tolerance.  If only a single output is
     requested then a warning will be emitted when the requested
     tolerance is not met.  If the second output ERR is requested then
     no warning is issued and it is the responsibility of the programmer
     to inspect and determine whether the results are satisfactory.

     Reference: L.F. Shampine, "Vectorized adaptive quadrature in
     MATLAB", ‘Journal of Computational and Applied Mathematics’,
     Vol. 211, Issue 2, pp. 131-140, Feb 2008.

     See also: *note quad: XREFquad, *note quadv: XREFquadv, *note
     quadl: XREFquadl, *note quadcc: XREFquadcc, *note trapz: XREFtrapz,
     *note dblquad: XREFdblquad, *note triplequad: XREFtriplequad, *note
     integral: XREFintegral, *note integral2: XREFintegral2, *note
     integral3: XREFintegral3.

 -- : Q = quadcc (F, A, B)
 -- : Q = quadcc (F, A, B, TOL)
 -- : Q = quadcc (F, A, B, TOL, SING)
 -- : [Q, ERR, NR_POINTS] = quadcc (...)
     Numerically evaluate the integral of F from A to B using
     doubly-adaptive Clenshaw-Curtis quadrature.

     F is a function handle, inline function, or string containing the
     name of the function to evaluate.  The function F must be
     vectorized and must return a vector of output values if given a
     vector of input values.  For example,

          f = @(x) x .* sin (1./x) .* sqrt (abs (1 - x));

     which uses the element-by-element "dot" form for all operators.

     A and B are the lower and upper limits of integration.  Either or
     both limits may be infinite.  ‘quadcc’ handles an infinite limit by
     substituting the variable of integration with ‘x = tan (pi/2*u)’.

     The optional argument TOL is a 1- or 2-element vector that
     specifies the desired accuracy of the result.  The first element of
     the vector is the desired absolute tolerance, and the second
     element is the desired relative tolerance.  To choose a relative
     test only, set the absolute tolerance to zero.  To choose an
     absolute test only, set the relative tolerance to zero.  The
     default absolute tolerance is 1e-10 (1e-5 for single), and the
     default relative tolerance is 1e-6 (1e-4 for single).

     The optional argument SING contains a list of points where the
     integrand has known singularities, or discontinuities in any of its
     derivatives, inside the integration interval.  For the example
     above, which has a discontinuity at x=1, the call to ‘quadcc’ would
     be as follows

          int = quadcc (f, a, b, [], [ 1 ]);

     The result of the integration is returned in Q.

     ERR is an estimate of the absolute integration error.

     NR_POINTS is the number of points at which the integrand was
     evaluated.

     If the adaptive integration did not converge, the value of ERR will
     be larger than the requested tolerance.  If only a single output is
     requested then a warning will be emitted when the requested
     tolerance is not met.  If the second output ERR is requested then
     no warning is issued and it is the responsibility of the programmer
     to inspect and determine whether the results are satisfactory.

     ‘quadcc’ is capable of dealing with non-numeric values of the
     integrand such as ‘NaN’ or ‘Inf’.  If the integral diverges, and
     ‘quadcc’ detects this, then a warning is issued and ‘Inf’ or ‘-Inf’
     is returned.

     Note: ‘quadcc’ is a general purpose quadrature algorithm and, as
     such, may be less efficient for a smooth or otherwise well-behaved
     integrand than other methods such as ‘quadgk’.

     The algorithm uses Clenshaw-Curtis quadrature rules of increasing
     degree in each interval and bisects the interval if either the
     function does not appear to be smooth or a rule of maximum degree
     has been reached.  The error estimate is computed from the L2-norm
     of the difference between two successive interpolations of the
     integrand over the nodes of the respective quadrature rules.

     Reference: P. Gonnet, "Increasing the Reliability of Adaptive
     Quadrature Using Explicit Interpolants", ‘ACM Transactions on
     Mathematical Software’, Vol. 37, Issue 3, Article No. 3, 2010.

     See also: *note quad: XREFquad, *note quadv: XREFquadv, *note
     quadl: XREFquadl, *note quadgk: XREFquadgk, *note trapz: XREFtrapz,
     *note dblquad: XREFdblquad, *note triplequad: XREFtriplequad.

 -- : Q = integral (F, A, B)
 -- : Q = integral (F, A, B, PROP, VAL, ...)
 -- : [Q, ERR] = integral (...)

     Numerically evaluate the integral of F from A to B using adaptive
     quadrature.

     ‘integral’ is a wrapper for ‘quadcc’ (general real-valued, scalar
     integrands and limits), and ‘quadgk’ (integrals with specified
     integration paths and array-valued integrands) that is intended to
     provide MATLAB compatibility.  More control of the numerical
     integration may be achievable by calling the various quadrature
     functions directly.

     F is a function handle, inline function, or string containing the
     name of the function to evaluate.  The function F must be
     vectorized and return a vector of output values when given a vector
     of input values.

     A and B are the lower and upper limits of integration.  Either or
     both limits may be infinite or contain weak end singularities.  If
     either or both limits are complex, ‘integral’ will perform a
     straight line path integral.  Alternatively, a complex domain path
     can be specified using the "Waypoints" option (see below).

     Additional optional parameters can be specified using "PROPERTY",
     VALUE pairs.  Valid properties are:

     ‘Waypoints’
          Specifies points to be used in defining subintervals of the
          quadrature algorithm, or if A, B, or WAYPOINTS are complex
          then the quadrature is calculated as a contour integral along
          a piecewise continuous path.  For more detail, *note ‘quadgk’:
          XREFquadgk.

     ‘ArrayValued’
          ‘integral’ expects F to return a scalar value unless
          ARRAYVALUED is specified as true.  This option will cause
          ‘integral’ to perform the integration over the entire array
          and return Q with the same dimensions as returned by F.  For
          more detail *note ‘quadgk’: XREFquadgk.

     ‘AbsTol’
          Define the absolute error tolerance for the quadrature.  The
          default absolute tolerance is 1e-10 (1e-5 for single).

     ‘RelTol’
          Define the relative error tolerance for the quadrature.  The
          default relative tolerance is 1e-6 (1e-4 for single).

     The optional output ERR contains the absolute error estimate used
     by the called integrator.

     Adaptive quadrature is used to minimize the estimate of error until
     the following is satisfied:

            ERROR <= max (ABSTOL, RELTOL*|Q|).

     Known MATLAB incompatibilities:

       1. If tolerances are left unspecified, and any integration limits
          or waypoints are of type ‘single’, then Octave's integral
          functions automatically reduce the default absolute and
          relative error tolerances as specified above.  If tighter
          tolerances are desired they must be specified.  MATLAB leaves
          the tighter tolerances appropriate for ‘double’ inputs in
          place regardless of the class of the integration limits.

     See also: *note integral2: XREFintegral2, *note integral3:
     XREFintegral3, *note quad: XREFquad, *note quadgk: XREFquadgk,
     *note quadv: XREFquadv, *note quadl: XREFquadl, *note quadcc:
     XREFquadcc, *note trapz: XREFtrapz, *note dblquad: XREFdblquad,
     *note triplequad: XREFtriplequad.

   Sometimes one does not have the function, but only the raw (x, y)
points from which to perform an integration.  This can occur when
collecting data in an experiment.  The ‘trapz’ function can integrate
these values as shown in the following example where "data" has been
collected on the cosine function over the range [0, pi/2).

     x = 0:0.1:pi/2;  # Uniformly spaced points
     y = cos (x);
     trapz (x, y)
          ⇒ 0.99666

   The answer is reasonably close to the exact value of 1.  Ordinary
quadrature is sensitive to the characteristics of the integrand.
Empirical integration depends not just on the integrand, but also on the
particular points chosen to represent the function.  Repeating the
example above with the sine function over the range [0, pi/2) produces
far inferior results.

     x = 0:0.1:pi/2;  # Uniformly spaced points
     y = sin (x);
     trapz (x, y)
          ⇒ 0.92849

   However, a slightly different choice of data points can change the
result significantly.  The same integration, with the same number of
points, but spaced differently produces a more accurate answer.

     x = linspace (0, pi/2, 16);  # Uniformly spaced, but including endpoint
     y = sin (x);
     trapz (x, y)
          ⇒ 0.99909

   In general there may be no way of knowing the best distribution of
points ahead of time.  Or the points may come from an experiment where
there is no freedom to select the best distribution.  In any case, one
must remain aware of this issue when using ‘trapz’.

 -- : Q = trapz (Y)
 -- : Q = trapz (X, Y)
 -- : Q = trapz (..., DIM)

     Numerically evaluate the integral of points Y using the trapezoidal
     method.

     ‘trapz (Y)’ computes the integral of Y along the first
     non-singleton dimension.  When the argument X is omitted an equally
     spaced X vector with unit spacing (1) is assumed.  ‘trapz (X, Y)’
     evaluates the integral with respect to the spacing in X and the
     values in Y.  This is useful if the points in Y have been sampled
     unevenly.

     If the optional DIM argument is given, operate along this
     dimension.

     Application Note: If X is not specified then unit spacing will be
     used.  To scale the integral to the correct value you must multiply
     by the actual spacing value (deltaX). As an example, the integral
     of x^3 over the range [0, 1] is x^4/4 or 0.25.  The following code
     uses ‘trapz’ to calculate the integral in three different ways.

          x = 0:0.1:1;
          y = x.^3;
          ## No scaling
          q = trapz (y)
            ⇒ q = 2.5250
          ## Approximation to integral by scaling
          q * 0.1
            ⇒ 0.25250
          ## Same result by specifying X
          trapz (x, y)
            ⇒ 0.25250

     See also: *note cumtrapz: XREFcumtrapz.

 -- : Q = cumtrapz (Y)
 -- : Q = cumtrapz (X, Y)
 -- : Q = cumtrapz (..., DIM)
     Cumulative numerical integration of points Y using the trapezoidal
     method.

     ‘cumtrapz (Y)’ computes the cumulative integral of Y along the
     first non-singleton dimension.  Where ‘trapz’ reports only the
     overall integral sum, ‘cumtrapz’ reports the current partial sum
     value at each point of Y.

     When the argument X is omitted an equally spaced X vector with unit
     spacing (1) is assumed.  ‘cumtrapz (X, Y)’ evaluates the integral
     with respect to the spacing in X and the values in Y.  This is
     useful if the points in Y have been sampled unevenly.

     If the optional DIM argument is given, operate along this
     dimension.

     Application Note: If X is not specified then unit spacing will be
     used.  To scale the integral to the correct value you must multiply
     by the actual spacing value (deltaX).

     See also: *note trapz: XREFtrapz, *note cumsum: XREFcumsum.


File: octave.info,  Node: Orthogonal Collocation,  Next: Functions of Multiple Variables,  Prev: Functions of One Variable,  Up: Numerical Integration

23.2 Orthogonal Collocation
===========================

 -- : [R, AMAT, BMAT, Q] = colloc (N, "left", "right")
     Compute derivative and integral weight matrices for orthogonal
     collocation.

     Reference: J. Villadsen, M. L. Michelsen, ‘Solution of Differential
     Equation Models by Polynomial Approximation’.

   Here is an example of using ‘colloc’ to generate weight matrices for
solving the second order differential equation U' - ALPHA * U" = 0 with
the boundary conditions U(0) = 0 and U(1) = 1.

   First, we can generate the weight matrices for N points (including
the endpoints of the interval), and incorporate the boundary conditions
in the right hand side (for a specific value of ALPHA).

     n = 7;
     alpha = 0.1;
     [r, a, b] = colloc (n-2, "left", "right");
     at = a(2:n-1,2:n-1);
     bt = b(2:n-1,2:n-1);
     rhs = alpha * b(2:n-1,n) - a(2:n-1,n);

   Then the solution at the roots R is

     u = [ 0; (at - alpha * bt) \ rhs; 1]
          ⇒ [ 0.00; 0.004; 0.01 0.00; 0.12; 0.62; 1.00 ]


File: octave.info,  Node: Functions of Multiple Variables,  Prev: Orthogonal Collocation,  Up: Numerical Integration

23.3 Functions of Multiple Variables
====================================

Octave includes several functions for computing the integral of
functions of multiple variables.  This procedure can generally be
performed by creating a function that integrates f with respect to x,
and then integrates that function with respect to y.  This procedure can
be performed manually using the following example which integrates the
function:

     f(x, y) = sin(pi*x*y) * sqrt(x*y)

   for x and y between 0 and 1.

   Using ‘quadgk’ in the example below, a double integration can be
performed.  (Note that any of the 1-D quadrature functions can be used
in this fashion except for ‘quad’ since it is written in Fortran and
cannot be called recursively.)

     function q = g(y)
       q = ones (size (y));
       for i = 1:length (y)
         f = @(x) sin (pi*x.*y(i)) .* sqrt (x.*y(i));
         q(i) = quadgk (f, 0, 1);
       endfor
     endfunction

     I = quadgk ("g", 0, 1)
           ⇒ 0.30022

   The algorithm above is implemented in the function ‘dblquad’ for
integrals over two variables.  The 3-D equivalent of this process is
implemented in ‘triplequad’ for integrals over three variables.  As an
example, the result above can be replicated with a call to ‘dblquad’ as
shown below.

     I = dblquad (@(x, y) sin (pi*x.*y) .* sqrt (x.*y), 0, 1, 0, 1)
           ⇒ 0.30022

 -- : Q = dblquad (F, XA, XB, YA, YB)
 -- : Q = dblquad (F, XA, XB, YA, YB, TOL)
 -- : Q = dblquad (F, XA, XB, YA, YB, TOL, QUADF)
 -- : Q = dblquad (F, XA, XB, YA, YB, TOL, QUADF, ...)
     Numerically evaluate the double integral of F.

     F is a function handle, inline function, or string containing the
     name of the function to evaluate.  The function F must have the
     form z = f(x,y) where X is a vector and Y is a scalar.  It should
     return a vector of the same length and orientation as X.

     XA, YA and XB, YB are the lower and upper limits of integration for
     x and y respectively.  The underlying integrator determines whether
     infinite bounds are accepted.

     The optional argument TOL defines the absolute tolerance used to
     integrate each sub-integral.  The default value is 1e-6.

     The optional argument QUADF specifies which underlying integrator
     function to use.  Any choice but ‘quad’ is available and the
     default is ‘quadcc’.

     Additional arguments, are passed directly to F.  To use the default
     value for TOL or QUADF one may pass ':' or an empty matrix ([]).

     See also: *note integral2: XREFintegral2, *note integral3:
     XREFintegral3, *note triplequad: XREFtriplequad, *note quad:
     XREFquad, *note quadv: XREFquadv, *note quadl: XREFquadl, *note
     quadgk: XREFquadgk, *note quadcc: XREFquadcc, *note trapz:
     XREFtrapz.

 -- : Q = triplequad (F, XA, XB, YA, YB, ZA, ZB)
 -- : Q = triplequad (F, XA, XB, YA, YB, ZA, ZB, TOL)
 -- : Q = triplequad (F, XA, XB, YA, YB, ZA, ZB, TOL, QUADF)
 -- : Q = triplequad (F, XA, XB, YA, YB, ZA, ZB, TOL, QUADF, ...)
     Numerically evaluate the triple integral of F.

     F is a function handle, inline function, or string containing the
     name of the function to evaluate.  The function F must have the
     form w = f(x,y,z) where either X or Y is a vector and the remaining
     inputs are scalars.  It should return a vector of the same length
     and orientation as X or Y.

     XA, YA, ZA and XB, YB, ZB are the lower and upper limits of
     integration for x, y, and z respectively.  The underlying
     integrator determines whether infinite bounds are accepted.

     The optional argument TOL defines the absolute tolerance used to
     integrate each sub-integral.  The default value is 1e-6.

     The optional argument QUADF specifies which underlying integrator
     function to use.  Any choice but ‘quad’ is available and the
     default is ‘quadcc’.

     Additional arguments, are passed directly to F.  To use the default
     value for TOL or QUADF one may pass ':' or an empty matrix ([]).

     See also: *note integral3: XREFintegral3, *note integral2:
     XREFintegral2, *note dblquad: XREFdblquad, *note quad: XREFquad,
     *note quadv: XREFquadv, *note quadl: XREFquadl, *note quadgk:
     XREFquadgk, *note quadcc: XREFquadcc, *note trapz: XREFtrapz.

   The recursive algorithm for quadrature presented above is referred to
as "iterated".  A separate 2-D integration method is implemented in the
function ‘quad2d’.  This function performs a "tiled" integration by
subdividing the integration domain into rectangular regions and
performing separate integrations over those domains.  The domains are
further subdivided in areas requiring refinement to reach the desired
numerical accuracy.  For certain functions this method can be faster
than the 2-D iteration used in the other functions above.

 -- : Q = quad2d (F, XA, XB, YA, YB)
 -- : Q = quad2d (F, XA, XB, YA, YB, PROP, VAL, ...)
 -- : [Q, ERR, ITER] = quad2d (...)

     Numerically evaluate the two-dimensional integral of F using
     adaptive quadrature over the two-dimensional domain defined by XA,
     XB, YA, YB using tiled integration.  Additionally, YA and YB may be
     scalar functions of X, allowing for the integration over
     non-rectangular domains.

     F is a function handle, inline function, or string containing the
     name of the function to evaluate.  The function F must be of the
     form z = f(x,y), and all operations must be vectorized such that X
     and Y accept array inputs and return array outputs of the same
     size.  (It can be assumed that X and Y will either be same-size
     arrays or one will be a scalar.)  The underlying integrators will
     input arrays of integration points into F and/or use internal
     vector expansions to speed computation that can produce
     unpredictable results if F is not restricted to elementwise
     operations.  For integrands where this is unavoidable, the
     ("Vectorized") option described below may produce more reliable
     results.

     Additional optional parameters can be specified using "PROPERTY",
     VALUE pairs.  Valid properties are:

     ‘AbsTol’
          Define the absolute error tolerance for the quadrature.  The
          default value is 1e-10 (1e-5 for single).

     ‘RelTol’
          Define the relative error tolerance for the quadrature.  The
          default value is 1e-6 (1e-4 for single).

     ‘MaxFunEvals’
          The maximum number of function calls to the vectorized
          function F.  The default value is 5000.

     ‘Singular’
          Enable/disable transforms to weaken singularities on the edge
          of the integration domain.  The default value is TRUE.

     ‘Vectorized’
          Enable or disable vectorized integration.  A value of ‘false’
          forces Octave to use only scalar inputs when calling the
          integrand, which enables integrands f(x,y) that have not been
          vectorized or only accept scalar values of X or Y.  The
          default value is ‘true’.  Note that this is achieved by
          wrapping f(x,y) with the function ‘arrayfun’, which may
          significantly decrease computation speed.

     ‘FailurePlot’
          If ‘quad2d’ fails to converge to the desired error tolerance
          before MaxFunEvals is reached, a plot of the areas that still
          need refinement is created.  The default value is FALSE.

     Adaptive quadrature is used to minimize the estimate of error until
     the following is satisfied:

                  ERROR <= max (ABSTOL, RELTOL*|Q|)

     The optional output ERR is an approximate bound on the error in the
     integral ‘abs (Q - I)’, where I is the exact value of the integral.
     The optional output ITER is the number of vectorized function calls
     to the function F that were used.

     Example 1 : integrate a rectangular region in x-y plane

          F = @(X,Y) 2*ones (size (X));
          Q = quad2d (F, 0, 1, 0, 1)
            ⇒ Q =  2

     The result is a volume, which for this constant-value integrand, is
     just ‘LENGTH * WIDTH * HEIGHT’.

     Example 2 : integrate a triangular region in x-y plane

          F = @(X,Y) 2*ones (size (X));
          YMAX = @(X) 1 - X;
          Q = quad2d (F, 0, 1, 0, YMAX)
            ⇒ Q =  1

     The result is a volume, which for this constant-value integrand
     F = 2, is the Triangle Area x Height or
     ‘1/2 * BASE * WIDTH * HEIGHT’.

     Example 3 : integrate a non-vectorized function over a square
     region

          F = @(X,Y) sinc (X) * sinc (Y));
          Q = quad2d (F, -1, 1, -1, 1)
            ⇒ Q =  12.328  (incorrect)
          Q = quad2d (F, -1, 1, -1, 1, "Vectorized", false)
            ⇒ Q =  1.390 (correct)
          F = @(X,Y) sinc (X) .* sinc (Y);
          Q = quad2d (F, -1, 1, -1, 1)
            ⇒ Q =  1.390  (correct)

     The first result is incorrect as the non-elementwise operator
     between the sinc functions in F create unintended matrix
     multiplications between the internal integration arrays used by
     ‘quad2d’.  In the second result, setting "Vectorized" to false
     forces ‘quad2d’ to perform scalar internal operations to compute
     the integral, resulting in the correct numerical result at the cost
     of about a 20x increase in computation time.  In the third result,
     vectorizing the integrand F using the elementwise multiplication
     operator gets the correct result without increasing computation
     time.

     Programming Notes: If there are singularities within the
     integration region it is best to split the integral and place the
     singularities on the boundary.

     Known MATLAB incompatibility: If tolerances are left unspecified,
     and any integration limits are of type ‘single’, then Octave's
     integral functions automatically reduce the default absolute and
     relative error tolerances as specified above.  If tighter
     tolerances are desired they must be specified.  MATLAB leaves the
     tighter tolerances appropriate for ‘double’ inputs in place
     regardless of the class of the integration limits.

     Reference: L.F. Shampine, "MATLAB program for quadrature in 2D",
     ‘Applied Mathematics and Computation’, Vol. 1, pp. 266-274, 2008.

     See also: *note integral2: XREFintegral2, *note dblquad:
     XREFdblquad, *note integral: XREFintegral, *note quad: XREFquad,
     *note quadgk: XREFquadgk, *note quadv: XREFquadv, *note quadl:
     XREFquadl, *note quadcc: XREFquadcc, *note trapz: XREFtrapz, *note
     integral3: XREFintegral3, *note triplequad: XREFtriplequad.

   Finally, the functions ‘integral2’ and ‘integral3’ are provided as
general 2-D and 3-D integration functions.  They will auto-select
between iterated and tiled integration methods and, unlike ‘dblquad’ and
‘triplequad’, will work with non-rectangular integration domains.

 -- : Q = integral2 (F, XA, XB, YA, YB)
 -- : Q = integral2 (F, XA, XB, YA, YB, PROP, VAL, ...)
 -- : [Q, ERR] = integral2 (...)

     Numerically evaluate the two-dimensional integral of F using
     adaptive quadrature over the two-dimensional domain defined by XA,
     XB, YA, YB (scalars may be finite or infinite).  Additionally, YA
     and YB may be scalar functions of X, allowing for integration over
     non-rectangular domains.

     F is a function handle, inline function, or string containing the
     name of the function to evaluate.  The function F must be of the
     form z = f(x,y), and all operations must be vectorized such that X
     and Y accept array inputs and return array outputs of the same
     size.  (It can be assumed that X and Y will either be same-size
     arrays or one will be a scalar.)  The underlying integrators will
     input arrays of integration points into F and/or use internal
     vector expansions to speed computation that can produce
     unpredictable results if F is not restricted to elementwise
     operations.  For integrands where this is unavoidable, the
     ("Vectorized") option described below may produce more reliable
     results.

     Additional optional parameters can be specified using "PROPERTY",
     VALUE pairs.  Valid properties are:

     ‘AbsTol’
          Define the absolute error tolerance for the quadrature.  The
          default value is 1e-10 (1e-5 for single).

     ‘RelTol’
          Define the relative error tolerance for the quadrature.  The
          default value is 1e-6 (1e-4 for single).

     ‘Method’
          Specify the two-dimensional integration method to be used,
          with valid options being "auto" (default), "tiled", or
          "iterated".  When using "auto", Octave will choose the "tiled"
          method unless any of the integration limits are infinite.

     ‘Vectorized’
          Enable or disable vectorized integration.  A value of ‘false’
          forces Octave to use only scalar inputs when calling the
          integrand, which enables integrands f(x,y) that have not been
          vectorized or only accept scalar values of X or Y.  The
          default value is ‘true’.  Note that this is achieved by
          wrapping f(x,y) with the function ‘arrayfun’, which may
          significantly decrease computation speed.

     Adaptive quadrature is used to minimize the estimate of error until
     the following is satisfied:

                  ERROR <= max (ABSTOL, RELTOL*|Q|)

     ERR is an approximate bound on the error in the integral ‘abs (Q -
     I)’, where I is the exact value of the integral.

     Example 1 : integrate a rectangular region in x-y plane

          F = @(X,Y) 2*ones (size (X));
          Q = integral2 (F, 0, 1, 0, 1)
            ⇒ Q =  2

     The result is a volume, which for this constant-value integrand, is
     just ‘LENGTH * WIDTH * HEIGHT’.

     Example 2 : integrate a triangular region in x-y plane

          F = @(X,Y) 2*ones (size (X));
          YMAX = @(X) 1 - X;
          Q = integral2 (F, 0, 1, 0, YMAX)
            ⇒ Q =  1

     The result is a volume, which for this constant-value integrand
     F = 2, is the Triangle Area x Height or
     ‘1/2 * BASE * WIDTH * HEIGHT’.

     Example 3 : integrate a non-vectorized function over a square
     region

          F = @(X,Y) sinc (X) * sinc (Y));
          Q = integral2 (F, -1, 1, -1, 1)
            ⇒ Q =  12.328  (incorrect)
          Q = integral2 (F, -1, 1, -1, 1, "Vectorized", false)
            ⇒ Q =  1.390 (correct)
          F = @(X,Y) sinc (X) .* sinc (Y);
          Q = integral2 (F, -1, 1, -1, 1)
            ⇒ Q =  1.390  (correct)

     The first result is incorrect as the non-elementwise operator
     between the sinc functions in F create unintended matrix
     multiplications between the internal integration arrays used by
     ‘integral2’.  In the second result, setting "Vectorized" to false
     forces ‘integral2’ to perform scalar internal operations to compute
     the integral, resulting in the correct numerical result at the cost
     of about a 20x increase in computation time.  In the third result,
     vectorizing the integrand F using the elementwise multiplication
     operator gets the correct result without increasing computation
     time.

     Programming Notes: If there are singularities within the
     integration region it is best to split the integral and place the
     singularities on the boundary.

     Known MATLAB incompatibility: If tolerances are left unspecified,
     and any integration limits are of type ‘single’, then Octave's
     integral functions automatically reduce the default absolute and
     relative error tolerances as specified above.  If tighter
     tolerances are desired they must be specified.  MATLAB leaves the
     tighter tolerances appropriate for ‘double’ inputs in place
     regardless of the class of the integration limits.

     Reference: L.F. Shampine, "MATLAB program for quadrature in 2D",
     ‘Applied Mathematics and Computation’, Vol. 1, pp. 266-274, 2008.

     See also: *note quad2d: XREFquad2d, *note dblquad: XREFdblquad,
     *note integral: XREFintegral, *note quad: XREFquad, *note quadgk:
     XREFquadgk, *note quadv: XREFquadv, *note quadl: XREFquadl, *note
     quadcc: XREFquadcc, *note trapz: XREFtrapz, *note integral3:
     XREFintegral3, *note triplequad: XREFtriplequad.

 -- : Q = integral3 (F, XA, XB, YA, YB, ZA, ZB)
 -- : Q = integral3 (F, XA, XB, YA, YB, ZA, ZB, PROP, VAL, ...)

     Numerically evaluate the three-dimensional integral of F using
     adaptive quadrature over the three-dimensional domain defined by
     XA, XB, YA, YB, ZA, ZB (scalars may be finite or infinite).
     Additionally, YA and YB may be scalar functions of X and ZA, and ZB
     maybe be scalar functions of X and Y, allowing for integration over
     non-rectangular domains.

     F is a function handle, inline function, or string containing the
     name of the function to evaluate.  The function F must be of the
     form z = f(x,y,z), and all operations must be vectorized such that
     X, Y, and Z accept array inputs and return array outputs of the
     same size.  (It can be assumed that X, Y, and Z will either be
     same-size arrays or scalars.)  The underlying integrators will
     input arrays of integration points into F and/or use internal
     vector expansions to speed computation that can produce
     unpredictable results if F is not restricted to elementwise
     operations.  For integrands where this is unavoidable, the
     ("Vectorized") option described below may produce more reliable
     results.

     Additional optional parameters can be specified using "PROPERTY",
     VALUE pairs.  Valid properties are:

     ‘AbsTol’
          Define the absolute error tolerance for the quadrature.  The
          default value is 1e-10 (1e-5 for single).

     ‘RelTol’
          Define the relative error tolerance for the quadrature.  The
          default value is 1e-6 (1e-4 for single).

     ‘Method’
          Specify the two-dimensional integration method to be used,
          with valid options being "auto" (default), "tiled", or
          "iterated".  When using "auto", Octave will choose the "tiled"
          method unless any of the integration limits are infinite.

     ‘Vectorized’
          Enable or disable vectorized integration.  A value of ‘false’
          forces Octave to use only scalar inputs when calling the
          integrand, which enables integrands f(x,y,z) that have not
          been vectorized or only accept scalar values of X, Y, or Z.
          The default value is ‘true’.  Note that this is achieved by
          wrapping f(x,y,z) with the function ‘arrayfun’, which may
          significantly decrease computation speed.

     Adaptive quadrature is used to minimize the estimate of error until
     the following is satisfied:

                  ERROR <= max (ABSTOL, RELTOL*|Q|)

     ERR is an approximate bound on the error in the integral ‘abs (Q -
     I)’, where I is the exact value of the integral.

     Example 1 : integrate over a rectangular volume

          F = @(X,Y,Z) ones (size (X));
          Q = integral3 (F, 0, 1, 0, 1, 0, 1)
            ⇒ Q =  1.00000

     For this constant-value integrand, the result is a volume which is
     just ‘LENGTH * WIDTH * HEIGHT’.

     Example 2 : integrate over a spherical volume

          F = @(X,Y) ones (size (X));
          YMAX = @(X) sqrt (1 - X.^2);
          ZMAX = @(X,Y) sqrt (1 - X.^2 - Y.^2);
          Q = integral3 (F, 0, 1, 0, YMAX, 0, ZMAX)
            ⇒ Q =  0.52360

     For this constant-value integrand, the result is a volume which is
     1/8th of a unit sphere or ‘1/8 * 4/3 * pi’.

     Example 3 : integrate a non-vectorized function over a cubic volume

          F = @(X,Y) sinc (X) * sinc (Y), * sinc (Z);
          Q = integral3 (F, -1, 1, -1, 1, -1, 1)
            ⇒ Q =  14.535  (incorrect)
          Q = integral3 (F, -1, 1, -1, 1, -1, 1, "Vectorized", false)
            ⇒ Q =  1.6388 (correct)
          F = @(X,Y,Z) sinc (X) .* sinc (Y), .* sinc (Z);
          Q = integral3 (F, -1, 1, -1, 1, -1, 1)
            ⇒ Q =  1.6388  (correct)

     The first result is incorrect as the non-elementwise operator
     between the sinc functions in F create unintended matrix
     multiplications between the internal integration arrays used by
     ‘integral3’.  In the second result, setting "Vectorized" to false
     forces ‘integral3’ to perform scalar internal operations to compute
     the integral, resulting in the correct numerical result at the cost
     of about a 30x increase in computation time.  In the third result,
     vectorizing the integrand F using the elementwise multiplication
     operator gets the correct result without increasing computation
     time.

     Programming Notes: If there are singularities within the
     integration region it is best to split the integral and place the
     singularities on the boundary.

     Known MATLAB incompatibility: If tolerances are left unspecified,
     and any integration limits are of type ‘single’, then Octave's
     integral functions automatically reduce the default absolute and
     relative error tolerances as specified above.  If tighter
     tolerances are desired they must be specified.  MATLAB leaves the
     tighter tolerances appropriate for ‘double’ inputs in place
     regardless of the class of the integration limits.

     Reference: L.F. Shampine, "MATLAB program for quadrature in 2D",
     ‘Applied Mathematics and Computation’, Vol. 1, pp. 266-274, 2008.

     See also: *note triplequad: XREFtriplequad, *note integral:
     XREFintegral, *note quad: XREFquad, *note quadgk: XREFquadgk, *note
     quadv: XREFquadv, *note quadl: XREFquadl, *note quadcc: XREFquadcc,
     *note trapz: XREFtrapz, *note integral2: XREFintegral2, *note
     quad2d: XREFquad2d, *note dblquad: XREFdblquad.

   The above integrations can be fairly slow, and that problem increases
exponentially with the dimensionality of the integral.  Another possible
solution for 2-D integration is to use Orthogonal Collocation as
described in the previous section (*note Orthogonal Collocation::).  The
integral of a function f(x,y) for x and y between 0 and 1 can be
approximated using n points by the sum over ‘i=1:n’ and ‘j=1:n’ of
‘q(i)*q(j)*f(r(i),r(j))’, where q and r is as returned by ‘colloc (n)’.
The generalization to more than two variables is straight forward.  The
following code computes the studied integral using n=8 points.

     f = @(x,y) sin (pi*x*y') .* sqrt (x*y');
     n = 8;
     [t, ~, ~, q] = colloc (n);
     I = q'*f(t,t)*q;
           ⇒ 0.30022

It should be noted that the number of points determines the quality of
the approximation.  If the integration needs to be performed between a
and b, instead of 0 and 1, then a change of variables is needed.


File: octave.info,  Node: Differential Equations,  Next: Optimization,  Prev: Numerical Integration,  Up: Top

24 Differential Equations
*************************

Octave has built-in functions for solving ordinary differential
equations (ODEs), and differential-algebraic equations (DAEs).

* Menu:

* Ordinary Differential Equations::
* Differential-Algebraic Equations::
* Matlab-compatible solvers::


File: octave.info,  Node: Ordinary Differential Equations,  Next: Differential-Algebraic Equations,  Up: Differential Equations

24.1 Ordinary Differential Equations
====================================

The function ‘lsode’ can be used to solve ODEs of the form

     dx
     -- = f (x, t)
     dt

using Hindmarsh's ODE solver LSODE.

 -- : [X, ISTATE, MSG] = lsode (FCN, X_0, T)
 -- : [X, ISTATE, MSG] = lsode (FCN, X_0, T, T_CRIT)
     Ordinary Differential Equation (ODE) solver.

     The set of differential equations to solve is

          dx
          -- = f (x, t)
          dt

     with

          x(t_0) = x_0

     The solution is returned in the matrix X, with each row
     corresponding to an element of the vector T.  The first element of
     T should be t_0 and should correspond to the initial state of the
     system X_0, so that the first row of the output is X_0.

     The first argument, FCN, is a string, inline, or function handle
     that names the function f to call to compute the vector of right
     hand sides for the set of equations.  The function must have the
     form

          XDOT = f (X, T)

     in which XDOT and X are vectors and T is a scalar.

     If FCN is a two-element string array or a two-element cell array of
     strings, inline functions, or function handles, the first element
     names the function f described above, and the second element names
     a function to compute the Jacobian of f.  The Jacobian function
     must have the form

          JAC = j (X, T)

     in which JAC is the matrix of partial derivatives

                       | df_1  df_1       df_1 |
                       | ----  ----  ...  ---- |
                       | dx_1  dx_2       dx_N |
                       |                       |
                       | df_2  df_2       df_2 |
                       | ----  ----  ...  ---- |
                df_i   | dx_1  dx_2       dx_N |
          jac = ---- = |                       |
                dx_j   |  .    .     .    .    |
                       |  .    .      .   .    |
                       |  .    .       .  .    |
                       |                       |
                       | df_M  df_M       df_M |
                       | ----  ----  ...  ---- |
                       | dx_1  dx_2       dx_N |

     The second argument specifies the initial state of the system x_0.
     The third argument is a vector, T, specifying the time values for
     which a solution is sought.

     The fourth argument is optional, and may be used to specify a set
     of times that the ODE solver should not integrate past.  It is
     useful for avoiding difficulties with singularities and points
     where there is a discontinuity in the derivative.

     After a successful computation, the value of ISTATE will be 2
     (consistent with the Fortran version of LSODE).

     If the computation is not successful, ISTATE will be something
     other than 2 and MSG will contain additional information.

     You can use the function ‘lsode_options’ to set optional parameters
     for ‘lsode’.

     See Alan C. Hindmarsh, "ODEPACK, A Systematized Collection of ODE
     Solvers", ‘Scientific Computing’, R. S. Stepleman, editor, 1983.
     or <https://computing.llnl.gov/projects/odepack> for more
     information about the inner workings of ‘lsode’.

     Example: Solve the Van der Pol equation

          fvdp = @(Y,T) [Y(2); (1 - Y(1)^2) * Y(2) - Y(1)];
          T = linspace (0, 20, 100);
          Y = lsode (fvdp, [2; 0], T);

     See also: *note daspk: XREFdaspk, *note dassl: XREFdassl, *note
     dasrt: XREFdasrt.

 -- : lsode_options ()
 -- : val = lsode_options (OPT)
 -- : lsode_options (OPT, VAL)
     Query or set options for the function ‘lsode’.

     When called with no arguments, the names of all available options
     and their current values are displayed.

     Given one argument, return the value of the option OPT.

     When called with two arguments, ‘lsode_options’ sets the option OPT
     to value VAL.

     Options include

     "absolute tolerance"
          Absolute tolerance.  May be either vector or scalar.  If a
          vector, it must match the dimension of the state vector.

     "relative tolerance"
          Relative tolerance parameter.  Unlike the absolute tolerance,
          this parameter may only be a scalar.

          The local error test applied at each integration step is

                 abs (local error in x(i)) <= ...
                     rtol * abs (y(i)) + atol(i)

     "integration method"
          A string specifying the method of integration to use to solve
          the ODE system.  Valid values are

          "adams"
          "non-stiff"
               No Jacobian used (even if it is available).

          "bdf"
          "stiff"
               Use stiff backward differentiation formula (BDF) method.
               If a function to compute the Jacobian is not supplied,
               ‘lsode’ will compute a finite difference approximation of
               the Jacobian matrix.

     "initial step size"
          The step size to be attempted on the first step (default is
          determined automatically).

     "maximum order"
          Restrict the maximum order of the solution method.  If using
          the Adams method, this option must be between 1 and 12.
          Otherwise, it must be between 1 and 5, inclusive.

     "maximum step size"
          Setting the maximum stepsize will avoid passing over very
          large regions (default is not specified).

     "minimum step size"
          The minimum absolute step size allowed (default is 0).

     "step limit"
          Maximum number of steps allowed (default is 100000).

     "jacobian type"
          A string specifying the type of Jacobian used with the stiff
          backward differentiation formula (BDF) integration method.
          Valid values are

          "full"
               The default.  All partial derivatives are approximated or
               used from the user-supplied Jacobian function.

          "banded"
               Only the diagonal and the number of lower and upper
               subdiagonals specified by the options "lower jacobian
               subdiagonals" and "upper jacobian subdiagonals",
               respectively, are approximated or used from the
               user-supplied Jacobian function.  A user-supplied
               Jacobian function may set all other partial derivatives
               to arbitrary values.

          "diagonal"
               If a Jacobian function is supplied by the user, this
               setting has no effect.  A Jacobian approximated by
               ‘lsode’ is restricted to the diagonal, where each partial
               derivative is computed by applying a finite change to all
               elements of the state together; if the real Jacobian is
               indeed always diagonal, this has the same effect as
               applying the finite change only to the respective element
               of the state, but is more efficient.

     "lower jacobian subdiagonals"
          Number of lower subdiagonals used if option "jacobian type" is
          set to "banded".  The default is zero.

     "upper jacobian subdiagonals"
          Number of upper subdiagonals used if option "jacobian type" is
          set to "banded".  The default is zero.

   Here is an example of solving a set of three differential equations
using ‘lsode’.  Given the function

     ## oregonator differential equation
     function xdot = f (x, t)

       xdot = zeros (3,1);

       xdot(1) = 77.27 * (x(2) - x(1)*x(2) + x(1) ...
                 - 8.375e-06*x(1)^2);
       xdot(2) = (x(3) - x(1)*x(2) - x(2)) / 77.27;
       xdot(3) = 0.161*(x(1) - x(3));

     endfunction

and the initial condition ‘x0 = [ 4; 1.1; 4 ]’, the set of equations can
be integrated using the command

     t = linspace (0, 500, 1000);

     y = lsode ("f", x0, t);

   If you try this, you will see that the value of the result changes
dramatically between T = 0 and 5, and again around T = 305.  A more
efficient set of output points might be

     t = [0, logspace(-1, log10(303), 150), ...
             logspace(log10(304), log10(500), 150)];

   An m-file for the differential equation used above is included with
the Octave distribution in the examples directory under the name
‘oregonator.m’.


File: octave.info,  Node: Differential-Algebraic Equations,  Next: Matlab-compatible solvers,  Prev: Ordinary Differential Equations,  Up: Differential Equations

24.2 Differential-Algebraic Equations
=====================================

The function ‘daspk’ can be used to solve DAEs of the form

     0 = f (x-dot, x, t),    x(t=0) = x_0, x-dot(t=0) = x-dot_0

where x-dot is the derivative of x.  The equation is solved using
Petzold's DAE solver DASPK.

 -- : [X, XDOT, ISTATE, MSG] = daspk (FCN, X_0, XDOT_0, T, T_CRIT)
     Solve a set of differential-algebraic equations.

     ‘daspk’ solves the set of equations

          0 = f (x, xdot, t)

     with

          x(t_0) = x_0, xdot(t_0) = xdot_0

     The solution is returned in the matrices X and XDOT, with each row
     in the result matrices corresponding to one of the elements in the
     vector T.  The first element of T should be t_0 and correspond to
     the initial state of the system X_0 and its derivative XDOT_0, so
     that the first row of the output X is X_0 and the first row of the
     output XDOT is XDOT_0.

     The first argument, FCN, is a string, inline, or function handle
     that names the function f to call to compute the vector of
     residuals for the set of equations.  It must have the form

          RES = f (X, XDOT, T)

     in which X, XDOT, and RES are vectors, and T is a scalar.

     If FCN is a two-element string array or a two-element cell array of
     strings, inline functions, or function handles, the first element
     names the function f described above, and the second element names
     a function to compute the modified Jacobian

                df       df
          jac = -- + c ------
                dx     d xdot

     The modified Jacobian function must have the form


          JAC = j (X, XDOT, T, C)

     The second and third arguments to ‘daspk’ specify the initial
     condition of the states and their derivatives, and the fourth
     argument specifies a vector of output times at which the solution
     is desired, including the time corresponding to the initial
     condition.

     The set of initial states and derivatives are not strictly required
     to be consistent.  If they are not consistent, you must use the
     ‘daspk_options’ function to provide additional information so that
     ‘daspk’ can compute a consistent starting point.

     The fifth argument is optional, and may be used to specify a set of
     times that the DAE solver should not integrate past.  It is useful
     for avoiding difficulties with singularities and points where there
     is a discontinuity in the derivative.

     After a successful computation, the value of ISTATE will be greater
     than zero (consistent with the Fortran version of DASPK).

     If the computation is not successful, the value of ISTATE will be
     less than zero and MSG will contain additional information.

     You can use the function ‘daspk_options’ to set optional parameters
     for ‘daspk’.

     See also: *note dassl: XREFdassl.

 -- : daspk_options ()
 -- : val = daspk_options (OPT)
 -- : daspk_options (OPT, VAL)
     Query or set options for the function ‘daspk’.

     When called with no arguments, the names of all available options
     and their current values are displayed.

     Given one argument, return the value of the option OPT.

     When called with two arguments, ‘daspk_options’ sets the option OPT
     to value VAL.

     Options include

     "absolute tolerance"
          Absolute tolerance.  May be either vector or scalar.  If a
          vector, it must match the dimension of the state vector, and
          the relative tolerance must also be a vector of the same
          length.

     "relative tolerance"
          Relative tolerance.  May be either vector or scalar.  If a
          vector, it must match the dimension of the state vector, and
          the absolute tolerance must also be a vector of the same
          length.

          The local error test applied at each integration step is

                 abs (local error in x(i))
                      <= rtol(i) * abs (Y(i)) + atol(i)

     "compute consistent initial condition"
          Denoting the differential variables in the state vector by
          ‘Y_d’ and the algebraic variables by ‘Y_a’, ‘ddaspk’ can solve
          one of two initialization problems:

            1. Given Y_d, calculate Y_a and Y'_d

            2. Given Y', calculate Y.

          In either case, initial values for the given components are
          input, and initial guesses for the unknown components must
          also be provided as input.  Set this option to 1 to solve the
          first problem, or 2 to solve the second (the default is 0, so
          you must provide a set of initial conditions that are
          consistent).

          If this option is set to a nonzero value, you must also set
          the "algebraic variables" option to declare which variables in
          the problem are algebraic.

     "use initial condition heuristics"
          Set to a nonzero value to use the initial condition heuristics
          options described below.

     "initial condition heuristics"
          A vector of the following parameters that can be used to
          control the initial condition calculation.

          ‘MXNIT’
               Maximum number of Newton iterations (default is 5).

          ‘MXNJ’
               Maximum number of Jacobian evaluations (default is 6).

          ‘MXNH’
               Maximum number of values of the artificial stepsize
               parameter to be tried if the "compute consistent initial
               condition" option has been set to 1 (default is 5).

               Note that the maximum total number of Newton iterations
               allowed is ‘MXNIT*MXNJ*MXNH’ if the "compute consistent
               initial condition" option has been set to 1 and
               ‘MXNIT*MXNJ’ if it is set to 2.

          ‘LSOFF’
               Set to a nonzero value to disable the linesearch
               algorithm (default is 0).

          ‘STPTOL’
               Minimum scaled step in linesearch algorithm (default is
               eps^(2/3)).

          ‘EPINIT’
               Swing factor in the Newton iteration convergence test.
               The test is applied to the residual vector, premultiplied
               by the approximate Jacobian.  For convergence, the
               weighted RMS norm of this vector (scaled by the error
               weights) must be less than ‘EPINIT*EPCON’, where ‘EPCON’
               = 0.33 is the analogous test constant used in the time
               steps.  The default is ‘EPINIT’ = 0.01.

     "print initial condition info"
          Set this option to a nonzero value to display detailed
          information about the initial condition calculation (default
          is 0).

     "exclude algebraic variables from error test"
          Set to a nonzero value to exclude algebraic variables from the
          error test.  You must also set the "algebraic variables"
          option to declare which variables in the problem are algebraic
          (default is 0).

     "algebraic variables"
          A vector of the same length as the state vector.  A nonzero
          element indicates that the corresponding element of the state
          vector is an algebraic variable (i.e., its derivative does not
          appear explicitly in the equation set).

          This option is required by the "compute consistent initial
          condition" and "exclude algebraic variables from error test"
          options.

     "enforce inequality constraints"
          Set to one of the following values to enforce the inequality
          constraints specified by the "inequality constraint types"
          option (default is 0).

            1. To have constraint checking only in the initial condition
               calculation.

            2. To enforce constraint checking during the integration.

            3. To enforce both options 1 and 2.

     "inequality constraint types"
          A vector of the same length as the state specifying the type
          of inequality constraint.  Each element of the vector
          corresponds to an element of the state and should be assigned
          one of the following codes

          -2
               Less than zero.

          -1
               Less than or equal to zero.

          0
               Not constrained.

          1
               Greater than or equal to zero.

          2
               Greater than zero.

          This option only has an effect if the "enforce inequality
          constraints" option is nonzero.

     "initial step size"
          Differential-algebraic problems may occasionally suffer from
          severe scaling difficulties on the first step.  If you know a
          great deal about the scaling of your problem, you can help to
          alleviate this problem by specifying an initial stepsize
          (default is computed automatically).

     "maximum order"
          Restrict the maximum order of the solution method.  This
          option must be between 1 and 5, inclusive (default is 5).

     "maximum step size"
          Setting the maximum stepsize will avoid passing over very
          large regions (default is not specified).

   Octave also includes DASSL, an earlier version of DASPK, and DASRT,
which can be used to solve DAEs with constraints (stopping conditions).

 -- : [X, XDOT, ISTATE, MSG] = dassl (FCN, X_0, XDOT_0, T, T_CRIT)
     Solve a set of differential-algebraic equations.

     ‘dassl’ solves the set of equations

          0 = f (x, xdot, t)

     with

          x(t_0) = x_0, xdot(t_0) = xdot_0

     The solution is returned in the matrices X and XDOT, with each row
     in the result matrices corresponding to one of the elements in the
     vector T.  The first element of T should be t_0 and correspond to
     the initial state of the system X_0 and its derivative XDOT_0, so
     that the first row of the output X is X_0 and the first row of the
     output XDOT is XDOT_0.

     The first argument, FCN, is a string, inline, or function handle
     that names the function f to call to compute the vector of
     residuals for the set of equations.  It must have the form

          RES = f (X, XDOT, T)

     in which X, XDOT, and RES are vectors, and T is a scalar.

     If FCN is a two-element string array or a two-element cell array of
     strings, inline functions, or function handles, the first element
     names the function f described above, and the second element names
     a function to compute the modified Jacobian

                df       df
          jac = -- + c ------
                dx     d xdot

     The modified Jacobian function must have the form


          JAC = j (X, XDOT, T, C)

     The second and third arguments to ‘dassl’ specify the initial
     condition of the states and their derivatives, and the fourth
     argument specifies a vector of output times at which the solution
     is desired, including the time corresponding to the initial
     condition.

     The set of initial states and derivatives are not strictly required
     to be consistent.  In practice, however, DASSL is not very good at
     determining a consistent set for you, so it is best if you ensure
     that the initial values result in the function evaluating to zero.

     The fifth argument is optional, and may be used to specify a set of
     times that the DAE solver should not integrate past.  It is useful
     for avoiding difficulties with singularities and points where there
     is a discontinuity in the derivative.

     After a successful computation, the value of ISTATE will be greater
     than zero (consistent with the Fortran version of DASSL).

     If the computation is not successful, the value of ISTATE will be
     less than zero and MSG will contain additional information.

     You can use the function ‘dassl_options’ to set optional parameters
     for ‘dassl’.

     See also: *note daspk: XREFdaspk, *note dasrt: XREFdasrt, *note
     lsode: XREFlsode.

 -- : dassl_options ()
 -- : val = dassl_options (OPT)
 -- : dassl_options (OPT, VAL)
     Query or set options for the function ‘dassl’.

     When called with no arguments, the names of all available options
     and their current values are displayed.

     Given one argument, return the value of the option OPT.

     When called with two arguments, ‘dassl_options’ sets the option OPT
     to value VAL.

     Options include

     "absolute tolerance"
          Absolute tolerance.  May be either vector or scalar.  If a
          vector, it must match the dimension of the state vector, and
          the relative tolerance must also be a vector of the same
          length.

     "relative tolerance"
          Relative tolerance.  May be either vector or scalar.  If a
          vector, it must match the dimension of the state vector, and
          the absolute tolerance must also be a vector of the same
          length.

          The local error test applied at each integration step is

                 abs (local error in x(i))
                      <= rtol(i) * abs (Y(i)) + atol(i)

     "compute consistent initial condition"
          If nonzero, ‘dassl’ will attempt to compute a consistent set
          of initial conditions.  This is generally not reliable, so it
          is best to provide a consistent set and leave this option set
          to zero.

     "enforce nonnegativity constraints"
          If you know that the solutions to your equations will always
          be non-negative, it may help to set this parameter to a
          nonzero value.  However, it is probably best to try leaving
          this option set to zero first, and only setting it to a
          nonzero value if that doesn't work very well.

     "initial step size"
          Differential-algebraic problems may occasionally suffer from
          severe scaling difficulties on the first step.  If you know a
          great deal about the scaling of your problem, you can help to
          alleviate this problem by specifying an initial stepsize.

     "maximum order"
          Restrict the maximum order of the solution method.  This
          option must be between 1 and 5, inclusive.

     "maximum step size"
          Setting the maximum stepsize will avoid passing over very
          large regions (default is not specified).

     "step limit"
          Maximum number of integration steps to attempt on a single
          call to the underlying Fortran code.

 -- : [X, XDOT, T_OUT, ISTAT, MSG] = dasrt (FCN, G, X_0, XDOT_0, T)
 -- : ... = dasrt (FCN, G, X_0, XDOT_0, T, T_CRIT)
 -- : ... = dasrt (FCN, X_0, XDOT_0, T)
 -- : ... = dasrt (FCN, X_0, XDOT_0, T, T_CRIT)
     Solve a set of differential-algebraic equations.

     ‘dasrt’ solves the set of equations

          0 = f (x, xdot, t)

     with

          x(t_0) = x_0, xdot(t_0) = xdot_0

     with functional stopping criteria (root solving).

     The solution is returned in the matrices X and XDOT, with each row
     in the result matrices corresponding to one of the elements in the
     vector T_OUT.  The first element of T should be t_0 and correspond
     to the initial state of the system X_0 and its derivative XDOT_0,
     so that the first row of the output X is X_0 and the first row of
     the output XDOT is XDOT_0.

     The vector T provides an upper limit on the length of the
     integration.  If the stopping condition is met, the vector T_OUT
     will be shorter than T, and the final element of T_OUT will be the
     point at which the stopping condition was met, and may not
     correspond to any element of the vector T.

     The first argument, FCN, is a string, inline, or function handle
     that names the function f to call to compute the vector of
     residuals for the set of equations.  It must have the form

          RES = f (X, XDOT, T)

     in which X, XDOT, and RES are vectors, and T is a scalar.

     If FCN is a two-element string array or a two-element cell array of
     strings, inline functions, or function handles, the first element
     names the function f described above, and the second element names
     a function to compute the modified Jacobian

                df       df
          jac = -- + c ------
                dx     d xdot

     The modified Jacobian function must have the form


          JAC = j (X, XDOT, T, C)

     The optional second argument names a function that defines the
     constraint functions whose roots are desired during the
     integration.  This function must have the form

          G_OUT = g (X, T)

     and return a vector of the constraint function values.  If the
     value of any of the constraint functions changes sign, DASRT will
     attempt to stop the integration at the point of the sign change.

     If the name of the constraint function is omitted, ‘dasrt’ solves
     the same problem as ‘daspk’ or ‘dassl’.

     Note that because of numerical errors in the constraint functions
     due to round-off and integration error, DASRT may return false
     roots, or return the same root at two or more nearly equal values
     of T.  If such false roots are suspected, the user should consider
     smaller error tolerances or higher precision in the evaluation of
     the constraint functions.

     If a root of some constraint function defines the end of the
     problem, the input to DASRT should nevertheless allow integration
     to a point slightly past that root, so that DASRT can locate the
     root by interpolation.

     The third and fourth arguments to ‘dasrt’ specify the initial
     condition of the states and their derivatives, and the fourth
     argument specifies a vector of output times at which the solution
     is desired, including the time corresponding to the initial
     condition.

     The set of initial states and derivatives are not strictly required
     to be consistent.  In practice, however, DASSL is not very good at
     determining a consistent set for you, so it is best if you ensure
     that the initial values result in the function evaluating to zero.

     The sixth argument is optional, and may be used to specify a set of
     times that the DAE solver should not integrate past.  It is useful
     for avoiding difficulties with singularities and points where there
     is a discontinuity in the derivative.

     After a successful computation, the value of ISTATE will be greater
     than zero (consistent with the Fortran version of DASSL).

     If the computation is not successful, the value of ISTATE will be
     less than zero and MSG will contain additional information.

     You can use the function ‘dasrt_options’ to set optional parameters
     for ‘dasrt’.

     See also: *note dasrt_options: XREFdasrt_options, *note daspk:
     XREFdaspk, *note dasrt: XREFdasrt, *note lsode: XREFlsode.

 -- : dasrt_options ()
 -- : val = dasrt_options (OPT)
 -- : dasrt_options (OPT, VAL)
     Query or set options for the function ‘dasrt’.

     When called with no arguments, the names of all available options
     and their current values are displayed.

     Given one argument, return the value of the option OPT.

     When called with two arguments, ‘dasrt_options’ sets the option OPT
     to value VAL.

     Options include

     "absolute tolerance"
          Absolute tolerance.  May be either vector or scalar.  If a
          vector, it must match the dimension of the state vector, and
          the relative tolerance must also be a vector of the same
          length.

     "relative tolerance"
          Relative tolerance.  May be either vector or scalar.  If a
          vector, it must match the dimension of the state vector, and
          the absolute tolerance must also be a vector of the same
          length.

          The local error test applied at each integration step is

                 abs (local error in x(i)) <= ...
                     rtol(i) * abs (Y(i)) + atol(i)

     "initial step size"
          Differential-algebraic problems may occasionally suffer from
          severe scaling difficulties on the first step.  If you know a
          great deal about the scaling of your problem, you can help to
          alleviate this problem by specifying an initial stepsize.

     "maximum order"
          Restrict the maximum order of the solution method.  This
          option must be between 1 and 5, inclusive.

     "maximum step size"
          Setting the maximum stepsize will avoid passing over very
          large regions.

     "step limit"
          Maximum number of integration steps to attempt on a single
          call to the underlying Fortran code.

   See K. E. Brenan, et al., ‘Numerical Solution of Initial-Value
Problems in Differential-Algebraic Equations’, North-Holland, 1989, DOI:
<https://doi.org/10.1137/1.9781611971224>, for more information about
the implementation of DASSL.


File: octave.info,  Node: Matlab-compatible solvers,  Prev: Differential-Algebraic Equations,  Up: Differential Equations

24.3 Matlab-compatible solvers
==============================

Octave also provides a set of solvers for initial value problems for
ordinary differential equations (ODEs) that have a MATLAB-compatible
interface.  The options for this class of methods are set using the
functions.

   • *note odeset: XREFodeset.

   • *note odeget: XREFodeget.

   Currently implemented solvers are:

   • Runge-Kutta methods

        • *note ode45: XREFode45. integrates a system of non-stiff ODEs
          or index-1 differential-algebraic equations (DAEs) using the
          high-order, variable-step Dormand-Prince method.  It requires
          six function evaluations per integration step, but may take
          larger steps on smooth problems than ‘ode23’: potentially
          offering improved efficiency at smaller tolerances.

        • *note ode23: XREFode23. integrates a system of non-stiff ODEs
          or (or index-1 DAEs).  It uses the third-order
          Bogacki-Shampine method and adapts the local step size in
          order to satisfy a user-specified tolerance.  The solver
          requires three function evaluations per integration step.

        • *note ode23s: XREFode23s. integrates a system of stiff ODEs
          (or index-1 DAEs) using a modified second-order Rosenbrock
          method.

   • Linear multistep methods

        • *note ode15s: XREFode15s. integrates a system of stiff ODEs
          (or index-1 DAEs) using a variable step, variable order method
          based on Backward Difference Formulas (BDF).

        • *note ode15i: XREFode15i. integrates a system of
          fully-implicit ODEs (or index-1 DAEs) using the same variable
          step, variable order method as ‘ode15s’.  *note decic:
          XREFdecic. can be used to compute consistent initial
          conditions for ‘ode15i’.

   Detailed information on the solvers are given in L. F. Shampine and
M.  W. Reichelt, "The MATLAB ODE Suite", ‘SIAM Journal on Scientific
Computing’, Vol. 18, pp. 1-22, 1997, DOI:
<https://doi.org/10.1137/S1064827594276424>.

 -- : [T, Y] = ode45 (FCN, TRANGE, INIT)
 -- : [T, Y] = ode45 (FCN, TRANGE, INIT, ODE_OPT)
 -- : [T, Y, TE, YE, IE] = ode45 (...)
 -- : SOLUTION = ode45 (...)
 -- : ode45 (...)

     Solve a set of non-stiff Ordinary Differential Equations (non-stiff
     ODEs) with the well known explicit Dormand-Prince method of order
     4.

     FCN is a function handle, inline function, or string containing the
     name of the function that defines the ODE: ‘y' = f(t,y)’.  The
     function must accept two inputs where the first is time T and the
     second is a column vector of unknowns Y.

     TRANGE specifies the time interval over which the ODE will be
     evaluated.  Typically, it is a two-element vector specifying the
     initial and final times (‘[tinit, tfinal]’).  If there are more
     than two elements then the solution will also be evaluated at these
     intermediate time instances.

     By default, ‘ode45’ uses an adaptive timestep with the
     ‘integrate_adaptive’ algorithm.  The tolerance for the timestep
     computation may be changed by using the options "RelTol" and
     "AbsTol".

     INIT contains the initial value for the unknowns.  If it is a row
     vector then the solution Y will be a matrix in which each column is
     the solution for the corresponding initial value in INIT.

     The optional fourth argument ODE_OPT specifies non-default options
     to the ODE solver.  It is a structure generated by ‘odeset’.

     The function typically returns two outputs.  Variable T is a column
     vector and contains the times where the solution was found.  The
     output Y is a matrix in which each column refers to a different
     unknown of the problem and each row corresponds to a time in T.

     The output can also be returned as a structure SOLUTION which has a
     field X containing a row vector of times where the solution was
     evaluated and a field Y containing the solution matrix such that
     each column corresponds to a time in X.  Use
     ‘fieldnames (SOLUTION)’ to see the other fields and additional
     information returned.

     If no output arguments are requested, and no "OutputFcn" is
     specified in ODE_OPT, then the "OutputFcn" is set to ‘odeplot’ and
     the results of the solver are plotted immediately.

     If using the "Events" option then three additional outputs may be
     returned.  TE holds the time when an Event function returned a
     zero.  YE holds the value of the solution at time TE.  IE contains
     an index indicating which Event function was triggered in the case
     of multiple Event functions.

     Example: Solve the Van der Pol equation

          fvdp = @(T,Y) [Y(2); (1 - Y(1)^2) * Y(2) - Y(1)];
          [T,Y] = ode45 (fvdp, [0, 20], [2, 0]);

     See also: *note odeset: XREFodeset, *note odeget: XREFodeget, *note
     ode23: XREFode23, *note ode15s: XREFode15s.

 -- : [T, Y] = ode23 (FCN, TRANGE, INIT)
 -- : [T, Y] = ode23 (FCN, TRANGE, INIT, ODE_OPT)
 -- : [T, Y, TE, YE, IE] = ode23 (...)
 -- : SOLUTION = ode23 (...)
 -- : ode23 (...)

     Solve a set of non-stiff Ordinary Differential Equations (non-stiff
     ODEs) with the well known explicit Bogacki-Shampine method of order
     3.

     FCN is a function handle, inline function, or string containing the
     name of the function that defines the ODE: ‘y' = f(t,y)’.  The
     function must accept two inputs where the first is time T and the
     second is a column vector of unknowns Y.

     TRANGE specifies the time interval over which the ODE will be
     evaluated.  Typically, it is a two-element vector specifying the
     initial and final times (‘[tinit, tfinal]’).  If there are more
     than two elements then the solution will also be evaluated at these
     intermediate time instances.

     By default, ‘ode23’ uses an adaptive timestep with the
     ‘integrate_adaptive’ algorithm.  The tolerance for the timestep
     computation may be changed by using the options "RelTol" and
     "AbsTol".

     INIT contains the initial value for the unknowns.  If it is a row
     vector then the solution Y will be a matrix in which each column is
     the solution for the corresponding initial value in INIT.

     The optional fourth argument ODE_OPT specifies non-default options
     to the ODE solver.  It is a structure generated by ‘odeset’.

     The function typically returns two outputs.  Variable T is a column
     vector and contains the times where the solution was found.  The
     output Y is a matrix in which each column refers to a different
     unknown of the problem and each row corresponds to a time in T.

     The output can also be returned as a structure SOLUTION which has a
     field X containing a row vector of times where the solution was
     evaluated and a field Y containing the solution matrix such that
     each column corresponds to a time in X.  Use
     ‘fieldnames (SOLUTION)’ to see the other fields and additional
     information returned.

     If no output arguments are requested, and no "OutputFcn" is
     specified in ODE_OPT, then the "OutputFcn" is set to ‘odeplot’ and
     the results of the solver are plotted immediately.

     If using the "Events" option then three additional outputs may be
     returned.  TE holds the time when an Event function returned a
     zero.  YE holds the value of the solution at time TE.  IE contains
     an index indicating which Event function was triggered in the case
     of multiple Event functions.

     Example: Solve the Van der Pol equation

          fvdp = @(T,Y) [Y(2); (1 - Y(1)^2) * Y(2) - Y(1)];
          [T,Y] = ode23 (fvdp, [0, 20], [2, 0]);

     Reference: For the definition of this method see
     <https://en.wikipedia.org/wiki/List_of_Runge%E2%80%93Kutta_methods>.

     See also: *note odeset: XREFodeset, *note odeget: XREFodeget, *note
     ode45: XREFode45, *note ode15s: XREFode15s.

 -- : [T, Y] = ode23s (FCN, TRANGE, INIT)
 -- : [T, Y] = ode23s (FCN, TRANGE, INIT, ODE_OPT)
 -- : [T, Y] = ode23s (..., PAR1, PAR2, ...)
 -- : [T, Y, TE, YE, IE] = ode23s (...)
 -- : SOLUTION = ode23s (...)

     Solve a set of stiff Ordinary Differential Equations (stiff ODEs)
     with a Rosenbrock method of order (2,3).

     FCN is a function handle, inline function, or string containing the
     name of the function that defines the ODE: ‘M y' = f(t,y)’.  The
     function must accept two inputs where the first is time T and the
     second is a column vector of unknowns Y.  M is a constant mass
     matrix, non-singular and possibly sparse.  Set the field "Mass" in
     ODEOPTS using ODESET to specify a mass matrix.

     TRANGE specifies the time interval over which the ODE will be
     evaluated.  Typically, it is a two-element vector specifying the
     initial and final times (‘[tinit, tfinal]’).  If there are more
     than two elements then the solution will also be evaluated at these
     intermediate time instances using an interpolation procedure of the
     same order as the one of the solver.

     By default, ‘ode23s’ uses an adaptive timestep with the
     ‘integrate_adaptive’ algorithm.  The tolerance for the timestep
     computation may be changed by using the options "RelTol" and
     "AbsTol".

     INIT contains the initial value for the unknowns.  If it is a row
     vector then the solution Y will be a matrix in which each column is
     the solution for the corresponding initial value in INIT.

     The optional fourth argument ODE_OPT specifies non-default options
     to the ODE solver.  It is a structure generated by ‘odeset’.
     ‘ode23s’ will ignore the following options: "BDF", "InitialSlope",
     "MassSingular", "MStateDependence", "MvPattern", "MaxOrder",
     "Non-negative".

     The function typically returns two outputs.  Variable T is a column
     vector and contains the times where the solution was found.  The
     output Y is a matrix in which each column refers to a different
     unknown of the problem and each row corresponds to a time in T.  If
     TRANGE specifies intermediate time steps, only those will be
     returned.

     The output can also be returned as a structure SOLUTION which has a
     field X containing a row vector of times where the solution was
     evaluated and a field Y containing the solution matrix such that
     each column corresponds to a time in X.  Use
     ‘fieldnames (SOLUTION)’ to see the other fields and additional
     information returned.

     If using the "Events" option then three additional outputs may be
     returned.  TE holds the time when an Event function returned a
     zero.  YE holds the value of the solution at time TE.  IE contains
     an index indicating which Event function was triggered in the case
     of multiple Event functions.

     Example: Solve the stiff Van der Pol equation

          f = @(T,Y) [Y(2); 1000*(1 - Y(1)^2) * Y(2) - Y(1)];
          opt = odeset ('Mass', [1 0; 0 1], 'MaxStep', 1e-1);
          [vt, vy] = ode23s (f, [0 2000], [2 0], opt);

     See also: *note odeset: XREFodeset, *note daspk: XREFdaspk, *note
     dassl: XREFdassl.

 -- : [T, Y] = ode15s (FCN, TRANGE, Y0)
 -- : [T, Y] = ode15s (FCN, TRANGE, Y0, ODE_OPT)
 -- : [T, Y, TE, YE, IE] = ode15s (...)
 -- : SOLUTION = ode15s (...)
 -- : ode15s (...)
     Solve a set of stiff Ordinary Differential Equations (ODEs) or
     stiff semi-explicit index 1 Differential Algebraic Equations
     (DAEs).

     ‘ode15s’ uses a variable step, variable order BDF (Backward
     Differentiation Formula) method that ranges from order 1 to 5.

     FCN is a function handle, inline function, or string containing the
     name of the function that defines the ODE: ‘y' = f(t,y)’.  The
     function must accept two inputs where the first is time T and the
     second is a column vector of unknowns Y.

     TRANGE specifies the time interval over which the ODE will be
     evaluated.  Typically, it is a two-element vector specifying the
     initial and final times (‘[tinit, tfinal]’).  If there are more
     than two elements then the solution will also be evaluated at these
     intermediate time instances.

     INIT contains the initial value for the unknowns.  If it is a row
     vector then the solution Y will be a matrix in which each column is
     the solution for the corresponding initial value in INIT.

     The optional fourth argument ODE_OPT specifies non-default options
     to the ODE solver.  It is a structure generated by ‘odeset’.

     The function typically returns two outputs.  Variable T is a column
     vector and contains the times where the solution was found.  The
     output Y is a matrix in which each column refers to a different
     unknown of the problem and each row corresponds to a time in T.

     The output can also be returned as a structure SOLUTION which has a
     field X containing a row vector of times where the solution was
     evaluated and a field Y containing the solution matrix such that
     each column corresponds to a time in X.  Use
     ‘fieldnames (SOLUTION)’ to see the other fields and additional
     information returned.

     If no output arguments are requested, and no "OutputFcn" is
     specified in ODE_OPT, then the "OutputFcn" is set to ‘odeplot’ and
     the results of the solver are plotted immediately.

     If using the "Events" option then three additional outputs may be
     returned.  TE holds the time when an Event function returned a
     zero.  YE holds the value of the solution at time TE.  IE contains
     an index indicating which Event function was triggered in the case
     of multiple Event functions.

     Example: Solve Robertson's equations:

          function r = robertson_dae (T, Y)
            r = [ -0.04*Y(1) + 1e4*Y(2)*Y(3)
                   0.04*Y(1) - 1e4*Y(2)*Y(3) - 3e7*Y(2)^2
          Y(1) + Y(2) + Y(3) - 1 ];
          endfunction
          opt = odeset ("Mass", [1 0 0; 0 1 0; 0 0 0], "MStateDependence", "none");
          [T,Y] = ode15s (@robertson_dae, [0, 1e3], [1; 0; 0], opt);

     See also: *note decic: XREFdecic, *note odeset: XREFodeset, *note
     odeget: XREFodeget, *note ode23: XREFode23, *note ode45: XREFode45.

 -- : [T, Y] = ode15i (FCN, TRANGE, Y0, YP0)
 -- : [T, Y] = ode15i (FCN, TRANGE, Y0, YP0, ODE_OPT)
 -- : [T, Y, TE, YE, IE] = ode15i (...)
 -- : SOLUTION = ode15i (...)
 -- : ode15i (...)
     Solve a set of fully-implicit Ordinary Differential Equations
     (ODEs) or index 1 Differential Algebraic Equations (DAEs).

     ‘ode15i’ uses a variable step, variable order BDF (Backward
     Differentiation Formula) method that ranges from order 1 to 5.

     FCN is a function handle, inline function, or string containing the
     name of the function that defines the ODE: ‘0 = f(t,y,yp)’.  The
     function must accept three inputs where the first is time T, the
     second is the function value Y (a column vector), and the third is
     the derivative value YP (a column vector).

     TRANGE specifies the time interval over which the ODE will be
     evaluated.  Typically, it is a two-element vector specifying the
     initial and final times (‘[tinit, tfinal]’).  If there are more
     than two elements then the solution will also be evaluated at these
     intermediate time instances.

     Y0 and YP0 contain the initial values for the unknowns Y and YP.
     If they are row vectors then the solution Y will be a matrix in
     which each column is the solution for the corresponding initial
     value in Y0 and YP0.

     Y0 and YP0 must be consistent initial conditions, meaning that
     ‘f(t,y0,yp0) = 0’ is satisfied.  The function ‘decic’ may be used
     to compute consistent initial conditions given initial guesses.

     The optional fifth argument ODE_OPT specifies non-default options
     to the ODE solver.  It is a structure generated by ‘odeset’.

     The function typically returns two outputs.  Variable T is a column
     vector and contains the times where the solution was found.  The
     output Y is a matrix in which each column refers to a different
     unknown of the problem and each row corresponds to a time in T.

     The output can also be returned as a structure SOLUTION which has a
     field X containing a row vector of times where the solution was
     evaluated and a field Y containing the solution matrix such that
     each column corresponds to a time in X.  Use
     ‘fieldnames (SOLUTION)’ to see the other fields and additional
     information returned.

     If no output arguments are requested, and no "OutputFcn" is
     specified in ODE_OPT, then the "OutputFcn" is set to ‘odeplot’ and
     the results of the solver are plotted immediately.

     If using the "Events" option then three additional outputs may be
     returned.  TE holds the time when an Event function returned a
     zero.  YE holds the value of the solution at time TE.  IE contains
     an index indicating which Event function was triggered in the case
     of multiple Event functions.

     Example: Solve Robertson's equations:

          function r = robertson_dae (T, Y, YP)
            r = [ -(YP(1) + 0.04*Y(1) - 1e4*Y(2)*Y(3))
                  -(YP(2) - 0.04*Y(1) + 1e4*Y(2)*Y(3) + 3e7*Y(2)^2)
          Y(1) + Y(2) + Y(3) - 1 ];
          endfunction
          [T,Y] = ode15i (@robertson_dae, [0, 1e3], [1; 0; 0], [-1e-4; 1e-4; 0]);

     See also: *note decic: XREFdecic, *note odeset: XREFodeset, *note
     odeget: XREFodeget.

 -- : [Y0_NEW, YP0_NEW] = decic (FCN, T0, Y0, FIXED_Y0, YP0, FIXED_YP0)
 -- : [Y0_NEW, YP0_NEW] = decic (FCN, T0, Y0, FIXED_Y0, YP0, FIXED_YP0,
          OPTIONS)
 -- : [Y0_NEW, YP0_NEW, RESNORM] = decic (...)

     Compute consistent implicit ODE initial conditions Y0_NEW and
     YP0_NEW given initial guesses Y0 and YP0.

     A maximum of ‘length (Y0)’ components between FIXED_Y0 and
     FIXED_YP0 may be chosen as fixed values.

     FCN is a function handle.  The function must accept three inputs
     where the first is time T, the second is a column vector of
     unknowns Y, and the third is a column vector of unknowns YP.

     T0 is the initial time such that ‘FCN(T0, Y0_NEW, YP0_NEW) = 0’,
     specified as a scalar.

     Y0 is a vector used as the initial guess for Y.

     FIXED_Y0 is a vector which specifies the components of Y0 to hold
     fixed.  Choose a maximum of ‘length (Y0)’ components between
     FIXED_Y0 and FIXED_YP0 as fixed values.  Set FIXED_Y0(i) component
     to 1 if you want to fix the value of Y0(i).  Set FIXED_Y0(i)
     component to 0 if you want to allow the value of Y0(i) to change.

     YP0 is a vector used as the initial guess for YP.

     FIXED_YP0 is a vector which specifies the components of YP0 to hold
     fixed.  Choose a maximum of ‘length (YP0)’ components between
     FIXED_Y0 and FIXED_YP0 as fixed values.  Set FIXED_YP0(i) component
     to 1 if you want to fix the value of YP0(i).  Set FIXED_YP0(i)
     component to 0 if you want to allow the value of YP0(i) to change.

     The optional seventh argument OPTIONS is a structure array.  Use
     ‘odeset’ to generate this structure.  The relevant options are
     ‘RelTol’ and ‘AbsTol’ which specify the error thresholds used to
     compute the initial conditions.

     The function typically returns two outputs.  Variable Y0_NEW is a
     column vector and contains the consistent initial value of Y.  The
     output YP0_NEW is a column vector and contains the consistent
     initial value of YP.

     The optional third output RESNORM is the norm of the vector of
     residuals.  If RESNORM is small, ‘decic’ has successfully computed
     the initial conditions.  If the value of RESNORM is large, use
     ‘RelTol’ and ‘AbsTol’ to adjust it.

     Example: Compute initial conditions for Robertson's equations:

          function r = robertson_dae (T, Y, YP)
            r = [ -(YP(1) + 0.04*Y(1) - 1e4*Y(2)*Y(3))
                  -(YP(2) - 0.04*Y(1) + 1e4*Y(2)*Y(3) + 3e7*Y(2)^2)
          Y(1) + Y(2) + Y(3) - 1 ];
          endfunction
          [Y0_NEW,YP0_NEW] = decic (@robertson_dae, 0, [1; 0; 0], [1; 1; 0],
          [-1e-4; 1; 0], [0; 0; 0]);

     See also: *note ode15i: XREFode15i, *note odeset: XREFodeset.

 -- : ODESTRUCT = odeset ()
 -- : ODESTRUCT = odeset ("FIELD1", VALUE1, "FIELD2", VALUE2, ...)
 -- : ODESTRUCT = odeset (OLDSTRUCT, "FIELD1", VALUE1, "FIELD2", VALUE2,
          ...)
 -- : ODESTRUCT = odeset (OLDSTRUCT, NEWSTRUCT)
 -- : odeset ()

     Create or modify an ODE options structure.

     When called with no input argument and one output argument, return
     a new ODE options structure that contains all possible fields
     initialized to their default values.  If no output argument is
     requested, display a list of the common ODE solver options along
     with their default value.

     If called with name-value input argument pairs "FIELD1", "VALUE1",
     "FIELD2", "VALUE2", ... return a new ODE options structure with all
     the most common option fields initialized, *and* set the values of
     the fields "FIELD1", "FIELD2", ... to the values VALUE1, VALUE2,
     ...

     If called with an input structure OLDSTRUCT then overwrite the
     values of the options "FIELD1", "FIELD2", ... with new values
     VALUE1, VALUE2, ... and return the modified structure.

     When called with two input ODE options structures OLDSTRUCT and
     NEWSTRUCT overwrite all values from the structure OLDSTRUCT with
     new values from the structure NEWSTRUCT.  Empty values in NEWSTRUCT
     will not overwrite values in OLDSTRUCT.

     The most commonly used ODE options, which are always assigned a
     value by ‘odeset’, are the following:

     ‘AbsTol’: positive scalar | vector, def. ‘1e-6’
          Absolute error tolerance.

     ‘BDF’: {"off"} | "on"
          Use BDF formulas in implicit multistep methods.  _Note_: This
          option is not yet implemented.

     ‘Events’: function_handle
          Event function.  An event function must have the form ‘[value,
          isterminal, direction] = my_events_f (t, y)’

     ‘InitialSlope’: vector
          Consistent initial slope vector for DAE solvers.

     ‘InitialStep’: positive scalar
          Initial time step size.

     ‘Jacobian’: matrix | function_handle
          Jacobian matrix, specified as a constant matrix or a function
          of time and state.

     ‘JConstant’: {"off"} | "on"
          Specify whether the Jacobian is a constant matrix or depends
          on the state.

     ‘JPattern’: sparse matrix
          If the Jacobian matrix is sparse and non-constant but
          maintains a constant sparsity pattern, specify the sparsity
          pattern.

     ‘Mass’: matrix | function_handle
          Mass matrix, specified as a constant matrix or a function of
          time and state.

     ‘MassSingular’: {"maybe"} | "yes" | "on"
          Specify whether the mass matrix is singular.

     ‘MaxOrder’: {5} | 4 | 3 | 2 | 1
          Maximum order of formula.

     ‘MaxStep’: positive scalar
          Maximum time step value.

     ‘MStateDependence’: {"weak"} | "none" | "strong"
          Specify whether the mass matrix depends on the state or only
          on time.

     ‘MvPattern’: sparse matrix
          If the mass matrix is sparse and non-constant but maintains a
          constant sparsity pattern, specify the sparsity pattern.
          _Note_: This option is not yet implemented.

     ‘NonNegative’: scalar | vector
          Specify elements of the state vector that are expected to
          remain non-negative during the simulation.

     ‘NormControl’: {"off"} | "on"
          Control error relative to the 2-norm of the solution, rather
          than its absolute value.

     ‘OutputFcn’: function_handle
          Function to monitor the state during the simulation.  For the
          form of the function to use *note ‘odeplot’: XREFodeplot.

     ‘OutputSel’: scalar | vector
          Indices of elements of the state vector to be passed to the
          output monitoring function.

     ‘Refine’: positive scalar
          Specify whether output should be returned only at the end of
          each time step or also at intermediate time instances.  The
          value should be a scalar indicating the number of equally
          spaced time points to use within each timestep at which to
          return output.

     ‘RelTol’: positive scalar
          Relative error tolerance.

     ‘Stats’: {"off"} | "on"
          Print solver statistics after simulation.

     ‘Vectorized’: {"off"} | "on"
          Specify whether ‘odefcn’ can be passed multiple values of the
          state at once.

     Field names that are not in the above list are also accepted and
     added to the result structure.

     See also: *note odeget: XREFodeget.

 -- : VAL = odeget (ODE_OPT, FIELD)
 -- : VAL = odeget (ODE_OPT, FIELD, DEFAULT)

     Query the value of the property FIELD in the ODE options structure
     ODE_OPT.

     If called with two input arguments and the first input argument
     ODE_OPT is an ODE option structure and the second input argument
     FIELD is a string specifying an option name, then return the option
     value VAL corresponding to FIELD from ODE_OPT.

     If called with an optional third input argument, and FIELD is not
     set in the structure ODE_OPT, then return the default value DEFAULT
     instead.

     See also: *note odeset: XREFodeset.

 -- : STOP_SOLVE = odeplot (T, Y, FLAG)

     Open a new figure window and plot the solution of an ode problem at
     each time step during the integration.

     The types and values of the input parameters T and Y depend on the
     input FLAG that is of type string.  Valid values of FLAG are:

     ‘"init"’
          The input T must be a column vector of length 2 with the first
          and last time step (‘[TFIRST TLAST]’.  The input Y contains
          the initial conditions for the ode problem (Y0).

     ‘""’
          The input T must be a scalar double or vector specifying the
          time(s) for which the solution in input Y was calculated.

     ‘"done"’
          The inputs should be empty, but are ignored if they are
          present.

     ‘odeplot’ always returns false, i.e., don't stop the ode solver.

     Example: solve an anonymous implementation of the "Van der Pol"
     equation and display the results while solving.

          fvdp = @(t,y) [y(2); (1 - y(1)^2) * y(2) - y(1)];

          opt = odeset ("OutputFcn", @odeplot, "RelTol", 1e-6);
          sol = ode45 (fvdp, [0 20], [2 0], opt);

     Background Information: This function is called by an ode solver
     function if it was specified in the "OutputFcn" property of an
     options structure created with ‘odeset’.  The ode solver will
     initially call the function with the syntax ‘odeplot ([TFIRST,
     TLAST], Y0, "init")’.  The function initializes internal variables,
     creates a new figure window, and sets the x limits of the plot.
     Subsequently, at each time step during the integration the ode
     solver calls ‘odeplot (T, Y, [])’.  At the end of the solution the
     ode solver calls ‘odeplot ([], [], "done")’ so that odeplot can
     perform any clean-up actions required.

     See also: *note odeset: XREFodeset, *note odeget: XREFodeget, *note
     ode23: XREFode23, *note ode45: XREFode45.


File: octave.info,  Node: Optimization,  Next: Statistics,  Prev: Differential Equations,  Up: Top

25 Optimization
***************

Octave comes with support for solving various kinds of optimization
problems.  Specifically Octave can solve problems in Linear Programming,
Quadratic Programming, Nonlinear Programming, and Linear Least Squares
Minimization.

* Menu:

* Linear Programming::
* Quadratic Programming::
* Nonlinear Programming::
* Linear Least Squares::


File: octave.info,  Node: Linear Programming,  Next: Quadratic Programming,  Up: Optimization

25.1 Linear Programming
=======================

Octave can solve Linear Programming problems using the ‘glpk’ function.
That is, Octave can solve

     min C'*x

   subject to the linear constraints A*x = b where x ≥ 0.

The ‘glpk’ function also supports variations of this problem.

 -- : [XOPT, FMIN, ERRNUM, EXTRA] = glpk (C, A, B, LB, UB, CTYPE,
          VARTYPE, SENSE, PARAM)
     Solve a linear program using the GNU GLPK library.

     Given three arguments, ‘glpk’ solves the following standard LP:

          min C'*x

     subject to

          A*x  = b
            x >= 0

     but may also solve problems of the form

          [ min | max ] C'*x

     subject to

          A*x [ "=" | "<=" | ">=" ] b
            x >= LB
            x <= UB

     Input arguments:

     C
          A column array containing the objective function coefficients.

     A
          A matrix containing the constraints coefficients.

     B
          A column array containing the right-hand side value for each
          constraint in the constraint matrix.

     LB
          An array containing the lower bound on each of the variables.
          If LB is not supplied, the default lower bound for the
          variables is zero.

     UB
          An array containing the upper bound on each of the variables.
          If UB is not supplied, the default upper bound is assumed to
          be infinite.

     CTYPE
          An array of characters containing the sense of each constraint
          in the constraint matrix.  Each element of the array may be
          one of the following values

          "F"
               A free (unbounded) constraint (the constraint is
               ignored).

          "U"
               An inequality constraint with an upper bound (‘A(i,:)*x
               <= b(i)’).

          "S"
               An equality constraint (‘A(i,:)*x = b(i)’).

          "L"
               An inequality with a lower bound (‘A(i,:)*x >= b(i)’).

          "D"
               An inequality constraint with both upper and lower bounds
               (‘A(i,:)*x >= -b(i)’) _and_ (‘A(i,:)*x <= b(i)’).

     VARTYPE
          A column array containing the types of the variables.

          "C"
               A continuous variable.

          "I"
               An integer variable.

     SENSE
          If SENSE is 1, the problem is a minimization.  If SENSE is -1,
          the problem is a maximization.  The default value is 1.

     PARAM
          A structure containing the following parameters used to define
          the behavior of solver.  Missing elements in the structure
          take on default values, so you only need to set the elements
          that you wish to change from the default.

          Integer parameters:

          ‘msglev (default: 1)’
               Level of messages output by solver routines:

               0 (‘GLP_MSG_OFF’)
                    No output.

               1 (‘GLP_MSG_ERR’)
                    Error and warning messages only.

               2 (‘GLP_MSG_ON’)
                    Normal output.

               3 (‘GLP_MSG_ALL’)
                    Full output (includes informational messages).

          ‘scale (default: 16)’
               Scaling option.  The values can be combined with the
               bitwise OR operator and may be the following:

               1 (‘GLP_SF_GM’)
                    Geometric mean scaling.

               16 (‘GLP_SF_EQ’)
                    Equilibration scaling.

               32 (‘GLP_SF_2N’)
                    Round scale factors to power of two.

               64 (‘GLP_SF_SKIP’)
                    Skip if problem is well scaled.

               Alternatively, a value of 128 (‘GLP_SF_AUTO’) may be also
               specified, in which case the routine chooses the scaling
               options automatically.

          ‘dual (default: 1)’
               Simplex method option:

               1 (‘GLP_PRIMAL’)
                    Use two-phase primal simplex.

               2 (‘GLP_DUALP’)
                    Use two-phase dual simplex, and if it fails, switch
                    to the primal simplex.

               3 (‘GLP_DUAL’)
                    Use two-phase dual simplex.

          ‘price (default: 34)’
               Pricing option (for both primal and dual simplex):

               17 (‘GLP_PT_STD’)
                    Textbook pricing.

               34 (‘GLP_PT_PSE’)
                    Steepest edge pricing.

          ‘itlim (default: intmax)’
               Simplex iterations limit.  It is decreased by one each
               time when one simplex iteration has been performed, and
               reaching zero value signals the solver to stop the
               search.

          ‘outfrq (default: 200)’
               Output frequency, in iterations.  This parameter
               specifies how frequently the solver sends information
               about the solution to the standard output.

          ‘branch (default: 4)’
               Branching technique option (for MIP only):

               1 (‘GLP_BR_FFV’)
                    First fractional variable.

               2 (‘GLP_BR_LFV’)
                    Last fractional variable.

               3 (‘GLP_BR_MFV’)
                    Most fractional variable.

               4 (‘GLP_BR_DTH’)
                    Heuristic by Driebeck and Tomlin.

               5 (‘GLP_BR_PCH’)
                    Hybrid pseudocost heuristic.

          ‘btrack (default: 4)’
               Backtracking technique option (for MIP only):

               1 (‘GLP_BT_DFS’)
                    Depth first search.

               2 (‘GLP_BT_BFS’)
                    Breadth first search.

               3 (‘GLP_BT_BLB’)
                    Best local bound.

               4 (‘GLP_BT_BPH’)
                    Best projection heuristic.

          ‘presol (default: 1)’
               If this flag is set, the simplex solver uses the built-in
               LP presolver.  Otherwise the LP presolver is not used.

          ‘lpsolver (default: 1)’
               Select which solver to use.  If the problem is a MIP
               problem this flag will be ignored.

               1
                    Revised simplex method.

               2
                    Interior point method.

          ‘rtest (default: 34)’
               Ratio test technique:

               17 (‘GLP_RT_STD’)
                    Standard ("textbook").

               34 (‘GLP_RT_HAR’)
                    Harris' two-pass ratio test.

          ‘tmlim (default: intmax)’
               Searching time limit, in milliseconds.

          ‘outdly (default: 0)’
               Output delay, in seconds.  This parameter specifies how
               long the solver should delay sending information about
               the solution to the standard output.

          ‘save (default: 0)’
               If this parameter is nonzero, save a copy of the problem
               in CPLEX LP format to the file ‘"outpb.lp"’.  There is
               currently no way to change the name of the output file.

          Real parameters:

          ‘tolbnd (default: 1e-7)’
               Relative tolerance used to check if the current basic
               solution is primal feasible.  It is not recommended that
               you change this parameter unless you have a detailed
               understanding of its purpose.

          ‘toldj (default: 1e-7)’
               Absolute tolerance used to check if the current basic
               solution is dual feasible.  It is not recommended that
               you change this parameter unless you have a detailed
               understanding of its purpose.

          ‘tolpiv (default: 1e-9)’
               Relative tolerance used to choose eligible pivotal
               elements of the simplex table.  It is not recommended
               that you change this parameter unless you have a detailed
               understanding of its purpose.

          ‘objll (default: -DBL_MAX)’
               Lower limit of the objective function.  If the objective
               function reaches this limit and continues decreasing, the
               solver stops the search.  This parameter is used in the
               dual simplex method only.

          ‘objul (default: +DBL_MAX)’
               Upper limit of the objective function.  If the objective
               function reaches this limit and continues increasing, the
               solver stops the search.  This parameter is used in the
               dual simplex only.

          ‘tolint (default: 1e-5)’
               Relative tolerance used to check if the current basic
               solution is integer feasible.  It is not recommended that
               you change this parameter unless you have a detailed
               understanding of its purpose.

          ‘tolobj (default: 1e-7)’
               Relative tolerance used to check if the value of the
               objective function is not better than in the best known
               integer feasible solution.  It is not recommended that
               you change this parameter unless you have a detailed
               understanding of its purpose.

     Output values:

     XOPT
          The optimizer (the value of the decision variables at the
          optimum).

     FOPT
          The optimum value of the objective function.

     ERRNUM
          Error code.

          0
               No error.

          1 (‘GLP_EBADB’)
               Invalid basis.

          2 (‘GLP_ESING’)
               Singular matrix.

          3 (‘GLP_ECOND’)
               Ill-conditioned matrix.

          4 (‘GLP_EBOUND’)
               Invalid bounds.

          5 (‘GLP_EFAIL’)
               Solver failed.

          6 (‘GLP_EOBJLL’)
               Objective function lower limit reached.

          7 (‘GLP_EOBJUL’)
               Objective function upper limit reached.

          8 (‘GLP_EITLIM’)
               Iterations limit exhausted.

          9 (‘GLP_ETMLIM’)
               Time limit exhausted.

          10 (‘GLP_ENOPFS’)
               No primal feasible solution.

          11 (‘GLP_ENODFS’)
               No dual feasible solution.

          12 (‘GLP_EROOT’)
               Root LP optimum not provided.

          13 (‘GLP_ESTOP’)
               Search terminated by application.

          14 (‘GLP_EMIPGAP’)
               Relative MIP gap tolerance reached.

          15 (‘GLP_ENOFEAS’)
               No primal/dual feasible solution.

          16 (‘GLP_ENOCVG’)
               No convergence.

          17 (‘GLP_EINSTAB’)
               Numerical instability.

          18 (‘GLP_EDATA’)
               Invalid data.

          19 (‘GLP_ERANGE’)
               Result out of range.

     EXTRA
          A data structure containing the following fields:

          ‘lambda’
               Dual variables.

          ‘redcosts’
               Reduced Costs.

          ‘time’
               Time (in seconds) used for solving LP/MIP problem.

          ‘status’
               Status of the optimization.

               1 (‘GLP_UNDEF’)
                    Solution status is undefined.

               2 (‘GLP_FEAS’)
                    Solution is feasible.

               3 (‘GLP_INFEAS’)
                    Solution is infeasible.

               4 (‘GLP_NOFEAS’)
                    Problem has no feasible solution.

               5 (‘GLP_OPT’)
                    Solution is optimal.

               6 (‘GLP_UNBND’)
                    Problem has no unbounded solution.

     Example:

          c = [10, 6, 4]';
          A = [ 1, 1, 1;
               10, 4, 5;
                2, 2, 6];
          b = [100, 600, 300]';
          lb = [0, 0, 0]';
          ub = [];
          ctype = "UUU";
          vartype = "CCC";
          s = -1;

          param.msglev = 1;
          param.itlim = 100;

          [xmin, fmin, status, extra] = ...
             glpk (c, A, b, lb, ub, ctype, vartype, s, param);


File: octave.info,  Node: Quadratic Programming,  Next: Nonlinear Programming,  Prev: Linear Programming,  Up: Optimization

25.2 Quadratic Programming
==========================

Octave can also solve Quadratic Programming problems, this is

     min 0.5 x'*H*x + x'*q

   subject to

          A*x = b
          lb <= x <= ub
          A_lb <= A_in*x <= A_ub

 -- : [X, OBJ, INFO, LAMBDA] = qp (X0, H)
 -- : [X, OBJ, INFO, LAMBDA] = qp (X0, H, Q)
 -- : [X, OBJ, INFO, LAMBDA] = qp (X0, H, Q, A, B)
 -- : [X, OBJ, INFO, LAMBDA] = qp (X0, H, Q, A, B, LB, UB)
 -- : [X, OBJ, INFO, LAMBDA] = qp (X0, H, Q, A, B, LB, UB, A_LB, A_IN,
          A_UB)
 -- : [X, OBJ, INFO, LAMBDA] = qp (..., OPTIONS)
     Solve a quadratic program (QP).

     Solve the quadratic program defined by

          min 0.5 x'*H*x + x'*q
           x

     subject to

          A*x = b
          lb <= x <= ub
          A_lb <= A_in*x <= A_ub

     using a null-space active-set method.

     Any bound (A, B, LB, UB, A_IN, A_LB, A_UB) may be set to the empty
     matrix (‘[]’) if not present.  The constraints A and A_IN are
     matrices with each row representing a single constraint.  The other
     bounds are scalars or vectors depending on the number of
     constraints.  The algorithm is faster if the initial guess is
     feasible.

     OPTIONS is a structure specifying additional parameters which
     control the algorithm.  Currently, ‘qp’ recognizes these options:
     "MaxIter", "TolX", "AllowSemidefinite".

     "MaxIter" sets the maximum number of algorithm iterations before
     optimization is halted.  The default value is 200.  The value must
     be a positive integer.

     "TolX" specifies the termination tolerance for the unknown
     variables X.  The default is ‘sqrt (eps)’ or approximately 1e-8.

     "AllowSemidefinite" is a boolean flag: ‘true’ allows positive
     semi-definite problems (one or more eigenvalues of the Hessian are
     zero).  ‘false’ requires positive definiteness (all eigenvalues
     positive).  The default is ‘false’.

     On return, X is the location of the minimum and FVAL contains the
     value of the objective function at X.

     INFO
          Structure containing run-time information about the algorithm.
          The following fields are defined:

          ‘solveiter’
               The number of iterations required to find the solution.

          ‘info’
               An integer indicating the status of the solution.

               0
                    The problem is feasible and convex.  Global solution
                    found.

               1
                    The problem is not convex.  Local solution found.

               2
                    The problem is not convex and unbounded.

               3
                    Maximum number of iterations reached.

               6
                    The problem is infeasible.

     See also: *note sqp: XREFsqp.

 -- : X = pqpnonneg (C, D)
 -- : X = pqpnonneg (C, D, X0)
 -- : X = pqpnonneg (C, D, X0, OPTIONS)
 -- : [X, MINVAL] = pqpnonneg (...)
 -- : [X, MINVAL, EXITFLAG] = pqpnonneg (...)
 -- : [X, MINVAL, EXITFLAG, OUTPUT] = pqpnonneg (...)
 -- : [X, MINVAL, EXITFLAG, OUTPUT, LAMBDA] = pqpnonneg (...)

     Minimize ‘ (1/2 * X' * C * X + D' * X) ’ subject to ‘X >= 0’.

     C and D must be real matrices, and C must be symmetric and positive
     definite.

     X0 is an optional initial guess for the solution X.

     OPTIONS is an options structure to change the behavior of the
     algorithm (*note ‘optimset’: XREFoptimset.).  ‘pqpnonneg’
     recognizes one option: "MaxIter".

     Outputs:

     X
          The solution matrix

     MINVAL
          The minimum attained model value, ‘1/2*XMIN'*C*XMIN + D'*XMIN’

     EXITFLAG
          An indicator of convergence.  0 indicates that the iteration
          count was exceeded, and therefore convergence was not reached;
          >0 indicates that the algorithm converged.  (The algorithm is
          stable and will converge given enough iterations.)

     OUTPUT
          A structure with two fields:

             • "algorithm": The algorithm used ("nnls")

             • "iterations": The number of iterations taken.

     LAMBDA
          Lagrange multipliers.  If these are nonzero, the corresponding
          X values should be zero, indicating the solution is pressed up
          against a coordinate plane.  The magnitude indicates how much
          the residual would improve if the ‘X >= 0’ constraints were
          relaxed in that direction.

     See also: *note lsqnonneg: XREFlsqnonneg, *note qp: XREFqp, *note
     optimset: XREFoptimset.


File: octave.info,  Node: Nonlinear Programming,  Next: Linear Least Squares,  Prev: Quadratic Programming,  Up: Optimization

25.3 Nonlinear Programming
==========================

Octave can also perform general nonlinear minimization using a
successive quadratic programming solver.

 -- : [X, OBJ, INFO, ITER, NF, LAMBDA] = sqp (X0, PHI)
 -- : [...] = sqp (X0, PHI, G)
 -- : [...] = sqp (X0, PHI, G, H)
 -- : [...] = sqp (X0, PHI, G, H, LB, UB)
 -- : [...] = sqp (X0, PHI, G, H, LB, UB, MAXITER)
 -- : [...] = sqp (X0, PHI, G, H, LB, UB, MAXITER, TOLERANCE)
     Minimize an objective function using sequential quadratic
     programming (SQP).

     Solve the nonlinear program

          min phi (x)
           x

     subject to

          g(x)  = 0
          h(x) >= 0
          lb <= x <= ub

     using a sequential quadratic programming method.

     The first argument is the initial guess for the vector X0.

     The second argument is a function handle pointing to the objective
     function PHI.  The objective function must accept one vector
     argument and return a scalar.

     The second argument may also be a 2- or 3-element cell array of
     function handles.  The first element should point to the objective
     function, the second should point to a function that computes the
     gradient of the objective function, and the third should point to a
     function that computes the Hessian of the objective function.  If
     the gradient function is not supplied, the gradient is computed by
     finite differences.  If the Hessian function is not supplied, a
     BFGS update formula is used to approximate the Hessian.

     When supplied, the gradient function ‘PHI{2}’ must accept one
     vector argument and return a vector.  When supplied, the Hessian
     function ‘PHI{3}’ must accept one vector argument and return a
     matrix.

     The third and fourth arguments G and H are function handles
     pointing to functions that compute the equality constraints and the
     inequality constraints, respectively.  If the problem does not have
     equality (or inequality) constraints, then use an empty matrix ([])
     for G (or H).  When supplied, these equality and inequality
     constraint functions must accept one vector argument and return a
     vector.

     The third and fourth arguments may also be 2-element cell arrays of
     function handles.  The first element should point to the constraint
     function and the second should point to a function that computes
     the gradient of the constraint function:

                      [ d f(x)   d f(x)        d f(x) ]
          transpose ( [ ------   -----   ...   ------ ] )
                      [  dx_1     dx_2          dx_N  ]

     The fifth and sixth arguments, LB and UB, contain lower and upper
     bounds on X and when provided must be vectors of the same size as
     the vector X0.  The bounds must be consistent with the equality and
     inequality constraints G and H.

     The seventh argument MAXITER specifies the maximum number of
     iterations.  The default value is 100.

     The eighth argument TOLERANCE specifies the tolerance for the
     stopping criteria.  The default value is ‘sqrt (eps)’.

     The value returned in INFO may be one of the following:

     101
          The algorithm terminated normally.  All constraints meet the
          specified tolerance.

     102
          The BFGS update failed.

     103
          The maximum number of iterations was reached.

     104
          The stepsize has become too small, i.e., delta X, is less than
          ‘TOL * norm (x)’.

     An example of calling ‘sqp’:

          function r = g (x)
            r = [ sumsq(x)-10;
                  x(2)*x(3)-5*x(4)*x(5);
                  x(1)^3+x(2)^3+1 ];
          endfunction

          function obj = phi (x)
            obj = exp (prod (x)) - 0.5*(x(1)^3+x(2)^3+1)^2;
          endfunction

          x0 = [-1.8; 1.7; 1.9; -0.8; -0.8];

          [x, obj, info, iter, nf, lambda] = sqp (x0, @phi, @g, [])

          x =

            -1.71714
             1.59571
             1.82725
            -0.76364
            -0.76364

          obj = 0.053950
          info = 101
          iter = 8
          nf = 10
          lambda =

            -0.0401627
             0.0379578
            -0.0052227

     See also: *note qp: XREFqp.


File: octave.info,  Node: Linear Least Squares,  Prev: Nonlinear Programming,  Up: Optimization

25.4 Linear Least Squares
=========================

Octave also supports linear least squares minimization.  That is, Octave
can find the parameter b such that the model y = x*b fits data (x,y) as
well as possible, assuming zero-mean Gaussian noise.  If the noise is
assumed to be isotropic the problem can be solved using the ‘\’ or ‘/’
operators, or the ‘ols’ function.  In the general case where the noise
is assumed to be anisotropic the ‘gls’ is needed.

 -- : [BETA, SIGMA, R] = ols (Y, X)
     Ordinary least squares (OLS) estimation.

     OLS applies to the multivariate model Y = X*B + E where Y is a
     t-by-p matrix, X is a t-by-k matrix, B is a k-by-p matrix, and E is
     a t-by-p matrix.

     Each row of Y is a p-variate observation in which each column
     represents a variable.  Likewise, the rows of X represent k-variate
     observations or possibly designed values.  Furthermore, the
     collection of observations X must be of adequate rank, k, otherwise
     B cannot be uniquely estimated.

     The observation errors, E, are assumed to originate from an
     underlying p-variate distribution with zero mean and p-by-p
     covariance matrix S, both constant conditioned on X.  Furthermore,
     the matrix S is constant with respect to each observation such that
     ‘mean (E) = 0’ and ‘cov (vec (E)) = kron (S, I)’.  (For cases that
     don't meet this criteria, such as autocorrelated errors, see
     generalized least squares, gls, for more efficient estimations.)

     The return values BETA, SIGMA, and R are defined as follows.

     BETA
          The OLS estimator for matrix B.  BETA is calculated directly
          via ‘inv (X'*X) * X' * Y’ if the matrix ‘X'*X’ is of full
          rank.  Otherwise, ‘BETA = pinv (X) * Y’ where ‘pinv (X)’
          denotes the pseudoinverse of X.

     SIGMA
          The OLS estimator for the matrix S,

               SIGMA = (Y-X*BETA)' * (Y-X*BETA) / (t-rank(X))

     R
          The matrix of OLS residuals, ‘R = Y - X*BETA’.

     See also: *note gls: XREFgls, *note pinv: XREFpinv.

 -- : [BETA, V, R] = gls (Y, X, O)
     Generalized least squares (GLS) model.

     Perform a generalized least squares estimation for the multivariate
     model Y = X*B + E where Y is a t-by-p matrix, X is a t-by-k matrix,
     B is a k-by-p matrix and E is a t-by-p matrix.

     Each row of Y is a p-variate observation in which each column
     represents a variable.  Likewise, the rows of X represent k-variate
     observations or possibly designed values.  Furthermore, the
     collection of observations X must be of adequate rank, k, otherwise
     B cannot be uniquely estimated.

     The observation errors, E, are assumed to originate from an
     underlying p-variate distribution with zero mean but possibly
     heteroscedastic observations.  That is, in general, ‘mean (E) = 0’
     and ‘cov (vec (E)) = (s^2)*O’ in which s is a scalar and O is a
     t*p-by-t*p matrix.

     The return values BETA, V, and R are defined as follows.

     BETA
          The GLS estimator for matrix B.

     V
          The GLS estimator for scalar s^2.

     R
          The matrix of GLS residuals, R = Y - X*BETA.

     See also: *note ols: XREFols.

 -- : X = lsqnonneg (C, D)
 -- : X = lsqnonneg (C, D, X0)
 -- : X = lsqnonneg (C, D, X0, OPTIONS)
 -- : [X, RESNORM] = lsqnonneg (...)
 -- : [X, RESNORM, RESIDUAL] = lsqnonneg (...)
 -- : [X, RESNORM, RESIDUAL, EXITFLAG] = lsqnonneg (...)
 -- : [X, RESNORM, RESIDUAL, EXITFLAG, OUTPUT] = lsqnonneg (...)
 -- : [X, RESNORM, RESIDUAL, EXITFLAG, OUTPUT, LAMBDA] = lsqnonneg (...)

     Minimize ‘norm (C*X - D)’ subject to ‘X >= 0’.

     C and D must be real matrices.

     X0 is an optional initial guess for the solution X.

     OPTIONS is an options structure to change the behavior of the
     algorithm (*note ‘optimset’: XREFoptimset.).  ‘lsqnonneg’
     recognizes these options: "MaxIter", "TolX".

     Outputs:

     RESNORM
          The squared 2-norm of the residual: ‘norm (C*X-D)^2’

     RESIDUAL
          The residual: ‘D-C*X’

     EXITFLAG
          An indicator of convergence.  0 indicates that the iteration
          count was exceeded, and therefore convergence was not reached;
          >0 indicates that the algorithm converged.  (The algorithm is
          stable and will converge given enough iterations.)

     OUTPUT
          A structure with two fields:

             • "algorithm": The algorithm used ("nnls")

             • "iterations": The number of iterations taken.

     LAMBDA
          Lagrange multipliers.  If these are nonzero, the corresponding
          X values should be zero, indicating the solution is pressed up
          against a coordinate plane.  The magnitude indicates how much
          the residual would improve if the ‘X >= 0’ constraints were
          relaxed in that direction.

     See also: *note pqpnonneg: XREFpqpnonneg, *note lscov: XREFlscov,
     *note optimset: XREFoptimset.

 -- : X = lscov (A, B)
 -- : X = lscov (A, B, V)
 -- : X = lscov (A, B, V, ALG)
 -- : [X, STDX, MSE, S] = lscov (...)

     Compute a generalized linear least squares fit.

     Estimate X under the model B = AX + W, where the noise W is assumed
     to follow a normal distribution with covariance matrix {\sigma^2}
     V.

     If the size of the coefficient matrix A is n-by-p, the size of the
     vector/array of constant terms B must be n-by-k.

     The optional input argument V may be an n-element vector of
     positive weights (inverse variances), or an n-by-n symmetric
     positive semi-definite matrix representing the covariance of B.  If
     V is not supplied, the ordinary least squares solution is returned.

     The ALG input argument, a guidance on solution method to use, is
     currently ignored.

     Besides the least-squares estimate matrix X (p-by-k), the function
     also returns STDX (p-by-k), the error standard deviation of
     estimated X; MSE (k-by-1), the estimated data error covariance
     scale factors (\sigma^2); and S (p-by-p, or p-by-p-by-k if k > 1),
     the error covariance of X.

     Reference: Golub and Van Loan, ‘Matrix Computations (3rd Ed.)’,
     Johns Hopkins, Section 5.6.3, 1996.

     See also: *note ols: XREFols, *note gls: XREFgls, *note lsqnonneg:
     XREFlsqnonneg.

 -- : optimset ()
 -- : OPTIONS = optimset ()
 -- : OPTIONS = optimset (PAR, VAL, ...)
 -- : OPTIONS = optimset (OLD, PAR, VAL, ...)
 -- : OPTIONS = optimset (OLD, NEW)
     Create options structure for optimization functions.

     When called without any input or output arguments, ‘optimset’
     prints a list of all valid optimization parameters.

     When called with one output and no inputs, return an options
     structure with all valid option parameters initialized to ‘[]’.

     When called with a list of parameter/value pairs, return an options
     structure with only the named parameters initialized.

     When the first input is an existing options structure OLD, the
     values are updated from either the PAR/VAL list or from the options
     structure NEW.

     If PAR does not exactly match the name of a standard parameter,
     ‘optimset’ will attempt to match PAR to a standard parameter and
     will set the value of that parameter if a match is found.  Matching
     is case insensitive and is based on character matching at the start
     of the parameter name.  ‘optimset’ produces an error if it finds
     multiple ambiguous matches.  If no standard parameter matches are
     found a warning is issued and the non-standard parameter is
     created.

     Standard list of valid parameters:

     AutoScaling

     ComplexEqn

     Display
          Request verbose display of results from optimizations.  Values
          are:

          "off" [default]
               No display.

          "iter"
               Display intermediate results for every loop iteration.

          "final"
               Display the result of the final loop iteration.

          "notify"
               Display the result of the final loop iteration if the
               function has failed to converge.

     FinDiffType

     FunValCheck
          When enabled, display an error if the objective function
          returns an invalid value (a complex number, NaN, or Inf).
          Must be set to "on" or "off" [default].  Note: the functions
          ‘fzero’ and ‘fminbnd’ correctly handle Inf values and only
          complex values or NaN will cause an error in this case.

     GradObj
          When set to "on", the function to be minimized must return a
          second argument which is the gradient, or first derivative, of
          the function at the point X.  If set to "off" [default], the
          gradient is computed via finite differences.

     Jacobian
          When set to "on", the function to be minimized must return a
          second argument which is the Jacobian, or first derivative, of
          the function at the point X.  If set to "off" [default], the
          Jacobian is computed via finite differences.

     MaxFunEvals
          Maximum number of function evaluations before optimization
          stops.  Must be a positive integer.

     MaxIter
          Maximum number of algorithm iterations before optimization
          stops.  Must be a positive integer.

     OutputFcn
          A user-defined function executed once per algorithm iteration.

     TolFun
          Termination criterion for the function output.  If the
          difference in the calculated objective function between one
          algorithm iteration and the next is less than ‘TolFun’ the
          optimization stops.  Must be a positive scalar.

     TolX
          Termination criterion for the function input.  If the
          difference in X, the current search point, between one
          algorithm iteration and the next is less than ‘TolX’ the
          optimization stops.  Must be a positive scalar.

     TypicalX

     Updating

     This list can be extended by the user or other loaded Octave
     packages.  An updated valid parameters list can be queried using
     the no-argument form of ‘optimset’.

     Note 1: Only parameter names from the standard list are considered
     when matching short parameter names, and PAR will always be
     expanded to match a standard parameter even if an exact
     non-standard match exists.  The value of a non-standard parameter
     that is ambiguous with one or more standard parameters cannot be
     set by ‘optimset’ and can only be set using ‘setfield’ or dot
     notation for structs.

     Note 2: The optimization options structure is primarily intended
     for manipulation of known parameters by ‘optimset’ and ‘optimget’.
     Unpredictable behavior on future calls to ‘optimset’ or ‘optimget’
     can result from creating non-standard or ambiguous parameters or
     from loading/unloading packages that change the known parameter
     list after creation of an optimization options structure.

     See also: *note optimget: XREFoptimget.

 -- : VAL = optimget (OPTIONS, PAR)
 -- : VAL = optimget (OPTIONS, PAR, DEFAULT)
     Return the value of the specific parameter PAR from the
     optimization options structure OPTIONS created by ‘optimset’.

     If PAR is not defined then return the DEFAULT value if supplied,
     otherwise return an empty matrix.

     If PAR does not exactly match the name of a standard parameter,
     ‘optimget’ will attempt to match PAR to a standard parameter and
     will return that parameter's value if a match is found.  Matching
     is case insensitive and is based on character matching at the start
     of the parameter name.  ‘optimget’ produces an error if it finds
     multiple ambiguous matches.  If no standard parameter matches are
     found a warning is issued.  See ‘optimset’ for information about
     the standard options list.

     Note: Only parameter names from the standard list are considered
     when matching short parameter names, and PAR will always be
     expanded to match a standard parameter even if an exact
     non-standard match exists.  The value of a non-standard parameter
     that is ambiguous with one or more standard parameters cannot be
     returned by ‘optimget’ and can only be accessed using ‘getfield’ or
     dot notation for structs.

     See also: *note optimset: XREFoptimset.


File: octave.info,  Node: Statistics,  Next: Sets,  Prev: Optimization,  Up: Top

26 Statistics
*************

Octave has support for various statistical methods, emphasizing basic
descriptive statistics.  More advanced functions for probability
distributions, statistical tests, random number generation, etc. are
provided by the add-on Statistics package from Octave Packages.

   The functions that analyze data all assume that multi-dimensional
data is arranged in a matrix where each row is an observation, and each
column is a variable.  Thus, the matrix defined by

     a = [ 0.9, 0.7;
           0.1, 0.1;
           0.5, 0.4 ];

contains three observations from a two-dimensional distribution.  While
this is the default data arrangement, most functions support different
arrangements.

   It should be noted that the statistics functions don't test for data
containing NaN, NA, or Inf.  These values need to be detected and dealt
with explicitly.  See *note isnan: XREFisnan, *note isna: XREFisna,
*note isinf: XREFisinf, and *note isfinite: XREFisfinite.

* Menu:

* Descriptive Statistics::
* Statistics on Sliding Windows of Data::
* Basic Statistical Functions::
* Forecasting Metrics::
* Correlation and Regression Analysis::
* Distributions::
* Random Number Generation::


File: octave.info,  Node: Descriptive Statistics,  Next: Statistics on Sliding Windows of Data,  Up: Statistics

26.1 Descriptive Statistics
===========================

One principal goal of descriptive statistics is to represent the essence
of a large data set concisely.  Octave provides the mean, median, and
mode functions which all summarize a data set with just a single number
corresponding to the central tendency of the data.

 -- : M = mean (X)
 -- : M = mean (X, DIM)
 -- : M = mean (X, VECDIM)
 -- : M = mean (X, "all")
 -- : M = mean (..., NANFLAG)
 -- : M = mean (..., OUTTYPE)
 -- : M = mean (..., 'Weights', W)
     Compute the mean of the elements of X.

     The mean is defined as

          mean (X) = SUM_i X(i) / N

     where N is the number of elements in X.

     The weighted mean is defined as

          weighted_mean (X) = SUM_i (W(i) * X(i)) / SUM_i (W(i))

     where N is the number of elements in X.

     If X is a vector, then ‘mean (X)’ returns the mean of the elements
     in X.

     If X is a matrix, then ‘mean (X)’ returns a row vector with each
     element containing the mean of the corresponding column in X.

     If X is an array, then ‘mean (X)’ computes the mean along the first
     non-singleton dimension of X.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension of
     X, including any dimension exceeding ‘ndims (X)’, will return X.

     Specifying multiple dimensions with input VECDIM, a vector of
     non-repeating dimensions, will operate along the array slice
     defined by VECDIM.  If VECDIM indexes all dimensions of X, then it
     is equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (X)’ is ignored.

     Specifying the dimension as "all" will cause ‘mean’ to operate on
     all elements of X, and is equivalent to ‘mean (X(:))’.

     The optional input OUTTYPE specifies the data type that is
     returned.  OUTTYPE can take the following values:

     'default'
          Output is of type double, unless the input is single in which
          case the output is of type single.

     'double'
          Output is of type double.

     'native'
          Output is of the same type as the input as reported by ‘class
          (X)’, unless the input is logical in which case the output is
          of type double.

     The optional variable NANFLAG specifies whether to include or
     exclude NaN values from the calculation using any of the previously
     specified input argument combinations.  The default value for
     NANFLAG is "includenan" which keeps NaN values in the calculation.
     To exclude NaN values set the value of NANFLAG to "omitnan".  The
     output will still contain NaN values if X consists of all NaN
     values in the operating dimension.

     The optional argument pair ‘"Weights", W’ specifies a weighting
     scheme W, which is applied on input X, so that ‘mean’ computes the
     weighted mean.  When operating along a single dimension, W must be
     a vector of the same length as the operating dimension or it must
     have the same size as X.  When operating over an array slice
     defined by VECDIM, W must have the same size as the operating array
     slice, i.e., ‘size (w) == size (X)(VECDIM)’, or the same size as X.

     See also: *note median: XREFmedian, *note mode: XREFmode, *note
     movmean: XREFmovmean.

 -- : M = median (X)
 -- : M = median (X, DIM)
 -- : M = median (X, VECDIM)
 -- : M = median (X, "all")
 -- : M = median (..., NANFLAG)
 -- : M = median (..., OUTTYPE)
     Compute the median value of the elements of X.

     The median is defined on the sorted data S (‘S = sort (X)’) as

                       |  S(ceil (N/2))          N odd
          median (X) = |
                       | (S(N/2) + S(N/2+1))/2   N even

     If X is a vector, then ‘median (X)’ returns the median of the
     elements in X.

     If X is a matrix, then ‘median (X)’ returns a row vector with each
     element containing the median of the corresponding column in X.

     If X is an array, then ‘median (X)’ computes the median along the
     first non-singleton dimension of X.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension of
     X, including any dimension exceeding ‘ndims (X)’, will return X.

     Specifying multiple dimensions with input VECDIM, a vector of
     non-repeating dimensions, will operate along the array slice
     defined by VECDIM.  If VECDIM indexes all dimensions of X, then it
     is equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (X)’ is ignored.

     Specifying the dimension as "all" will cause ‘median’ to operate on
     all elements of X, and is equivalent to ‘median (X(:))’.

     ‘median (..., OUTTYPE)’ returns the median with a specified data
     type, using any of the input arguments in the previous syntaxes.
     OUTTYPE can take the following values:

     "default"
          Output is of type double, unless the input is single in which
          case the output is of type single.

     "double"
          Output is of type double.

     "native".
          Output is of the same type as the input (‘class (X)’), unless
          the input is logical in which case the output is of type
          double.

     The optional variable NANFLAG specifies whether to include or
     exclude NaN values from the calculation using any of the previously
     specified input argument combinations.  The default value for
     NANFLAG is "includenan" which keeps NaN values in the calculation.
     To exclude NaN values set the value of NANFLAG to "omitnan".  The
     output will still contain NaN values if X consists of all NaN
     values in the operating dimension.

     See also: *note mean: XREFmean, *note mode: XREFmode, *note
     movmedian: XREFmovmedian.

 -- : M = mode (X)
 -- : M = mode (X, DIM)
 -- : M = mode (X, VECDIM)
 -- : M = mode (X, "all")
 -- : [M, F, C] = mode (...)
     Compute the most frequently occurring value in the input data X.

     ‘mode’ determines the frequency of values along the first
     non-singleton dimension and returns the value with the highest
     frequency.  If two, or more, values have the same frequency ‘mode’
     returns the smallest.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension of
     X, including any dimension exceeding ‘ndims (X)’, will return X.

     Specifying multiple dimensions with input VECDIM, a vector of
     non-repeating dimensions, will operate along the array slice
     defined by VECDIM.  If VECDIM indexes all dimensions of X, then it
     is equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (X)’ is ignored.  If all dimensions in VECDIM are
     greater than ‘ndims (X)’, then ‘mode’ will return X.

     Specifying the dimension as "all" will cause ‘mode’ to operate on
     all elements of X, and is equivalent to ‘mode (X(:))’.

     The return variable F is the number of occurrences of the mode in
     the dataset.

     The cell array C contains all of the elements with the maximum
     frequency.

     See also: *note mean: XREFmean, *note median: XREFmedian.

   Using just one number, such as the mean, to represent an entire data
set may not give an accurate picture of the data.  One way to
characterize the fit is to measure the dispersion of the data.  Octave
provides several functions for measuring dispersion.

 -- : [S, L] = bounds (X)
 -- : [S, L] = bounds (X, DIM)
 -- : [S, L] = bounds (X, VECDIM)
 -- : [S, L] = bounds (X, "all")
 -- : [S, L] = bounds (..., NANFLAG)
     Return the smallest and largest values of the input data X.

     If X is a vector, then ‘bounds (X)’ returns the smallest and
     largest values of the elements in X in S and L, respectively.

     If X is a matrix, then ‘bounds (X)’ returns the smallest and
     largest values for each column of X as row vectors S and L,
     respectively.

     If X is an array, then ‘bounds (X)’ computes the smallest and
     largest values along the first non-singleton dimension of X.

     The data in X must be numeric.  By default, any NaN values are
     ignored.  The size of S and L is equal to the size of X except for
     the operating dimension, which becomes 1.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension of
     X, including any dimension exceeding ‘ndims (X)’, will return ‘X’.

     Specifying multiple dimensions with input VECDIM, a vector of
     non-repeating dimensions, will operate along the array slice
     defined by VECDIM.  If VECDIM indexes all dimensions of X, then it
     is equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (X)’ is ignored.

     Specifying the dimension as "all" will cause ‘bounds’ to operate on
     all elements of X, and is equivalent to ‘bounds (X(:))’.

     The optional variable NANFLAG specifies whether to include or
     exclude NaN values from the calculation using any of the previously
     specified input argument combinations.  The default value for
     NANFLAG is "omitnan" which does not include NaN values in the
     result.  If the argument "includenan" is given, and there is a NaN
     present, then the result for both smallest (S) and largest (L)
     elements will be NaN.

     Usage Note: The bounds are a quickly computed measure of the
     dispersion of a data set, but are less accurate than ‘iqr’ if there
     are outlying data points.

     See also: *note range: XREFrange, *note iqr: XREFiqr, *note mad:
     XREFmad, *note std: XREFstd.

 -- : Y = range (X)
 -- : Y = range (X, DIM)
 -- : Y = range (X, VECDIM)
 -- : Y = range (X, "all")
 -- : Y = range (..., NANFLAG)
     Return the difference between the maximum and the minimum values of
     the input data X.

     If X is a vector, then ‘range (X)’ returns the difference between
     the maximum and minimum values of the elements in X.

     If X is a matrix, then ‘range (X)’ returns a row vector Y with the
     difference between the maximum and minimum values for each column
     of X.

     If X is an array, then ‘range (X)’ computes the difference between
     the maximum and minimum values along the first non-singleton
     dimension of X.

     The data in X must be numeric.  By default, any NaN values are
     ignored.  The size of R is equal to the size of X except for the
     operating dimension, which becomes 1.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension of
     X, including any dimension exceeding ‘ndims (X)’, will return ‘X’.

     Specifying multiple dimensions with input VECDIM, a vector of
     non-repeating dimensions, will operate along the array slice
     defined by VECDIM.  If VECDIM indexes all dimensions of X, then it
     is equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (X)’ is ignored.

     Specifying the dimension as "all" will cause ‘range’ to operate on
     all elements of X, and is equivalent to ‘range (X(:))’.

     The optional variable NANFLAG specifies whether to include or
     exclude NaN values from the calculation using any of the previously
     specified input argument combinations.  The default value for
     NANFLAG is "omitnan" which does not include NaN values in the
     result.  If the argument "includenan" is given, and there is a NaN
     present, then the corresponding result will be NaN.

     Usage Note: The range is a quickly computed measure of the
     dispersion of a data set, but is less accurate than ‘iqr’ if there
     are outlying data points.

     See also: *note bounds: XREFbounds, *note iqr: XREFiqr, *note mad:
     XREFmad, *note std: XREFstd.

 -- : R = iqr (X)
 -- : R = iqr (X, DIM)
 -- : R = iqr (X, VECDIM)
 -- : R = iqr (X, "all")
 -- : [R, Q] = iqr (...)
     Compute the interquartile range of the input data X.

     The interquartile range is defined as the difference between the
     75th and 25th percentile values of X calculated using

          quantile (x, [0.25 , 0.75])

     If X is a vector, then ‘iqr (X)’ computes the interquartile range
     of the elements in X.

     If X is a matrix, then ‘iqr (X)’ returns a row vector with each
     element containing the interquartile range of the corresponding
     column in X.

     If X is an array, then ‘iqr (X)’ computes the interquartile range
     along the first non-singleton dimension of X.

     The data in X must be numeric and any NaN values are ignored.  The
     size of R is equal to the size of X except for the operating
     dimension, which becomes 1.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension of
     X, including any dimension exceeding ‘ndims (X)’, will return
     ‘zeros (size (X))’.

     Specifying multiple dimensions with input VECDIM, a vector of
     non-repeating dimensions, will operate along the array slice
     defined by VECDIM.  If VECDIM indexes all dimensions of X, then it
     is equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (X)’ is ignored.

     Specifying the dimension as "all" will cause ‘iqr’ to operate on
     all elements of X, and is equivalent to ‘iqr (X(:))’.

     The optional output Q contains the quantiles for the 25th and 75th
     percentile of the data.

     Usage Note: As a measure of dispersion, the interquartile range is
     less affected by outliers than either ‘range’ or ‘std’.  The
     interquartile range of a scalar is necessarily ‘0’.

     See also: *note bounds: XREFbounds, *note mad: XREFmad, *note
     range: XREFrange, *note std: XREFstd, *note prctile: XREFprctile,
     *note quantile: XREFquantile.

 -- : M = mad (X)
 -- : M = mad (X, OPT)
 -- : M = mad (X, OPT, DIM)
 -- : M = mad (X, OPT, VECDIM)
 -- : M = mad (X, OPT, "all")
     Compute the mean or median absolute deviation (MAD) of the elements
     of X.

     The mean absolute deviation is defined as

          MAD = mean (abs (X - mean (X)))

     The median absolute deviation is defined as

          MAD = median (abs (X - median (X)))

     ‘mad’ excludes NaN values from calculation similar to using the
     ‘omitnan’ option in ‘mean’ and ‘median’.

     If X is a vector, then ‘mad (X)’ returns the mean absolute
     deviation of the elements in X.

     If X is a matrix, then ‘mad (X)’ returns a row vector with each
     element containing the mean absolute deviation of the corresponding
     column in X.

     If X is an array, then ‘mad (X)’ computes the mean absolute
     deviation along the first non-singleton dimension of X.

     The optional argument OPT specifies whether mean or median absolute
     deviation is calculated.  The default is ‘0’ which corresponds to
     mean absolute deviation; a value of ‘1’ corresponds to median
     absolute deviation.  Passing an empty input ‘[]’ defaults to mean
     absolute deviation.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension of
     X, including any dimension exceeding ‘ndims (X)’, will return
     ‘zeros (size (X))’.

     Specifying the dimension as VECDIM, a vector of non-repeating
     dimensions, will return the ‘mad’ over the array slice defined by
     VECDIM.  If VECDIM indexes all dimensions of X, then it is
     equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (X)’ is ignored.

     Specifying the dimension as "all" will cause ‘mad’ to operate on
     all elements of X, and is equivalent to ‘mad (X(:))’.

     Usage Note: As a measure of dispersion, ‘mad’ is less affected by
     outliers than ‘std’.

     See also: *note bounds: XREFbounds, *note range: XREFrange, *note
     iqr: XREFiqr, *note std: XREFstd, *note mean: XREFmean, *note
     median: XREFmedian.

 -- : Y = meansq (X)
 -- : Y = meansq (X, DIM)
 -- : Y = meansq (X, VECDIM)
 -- : Y = meansq (X, "all")
 -- : Y = meansq (..., NANFLAG)
     Compute the mean square of the input data X.

     The mean square is defined as

          meansq (X) = 1/N SUM_i X(i)^2

     where N is the length of the X vector.

     If X is a vector, then ‘meansq (X)’ returns the mean square of the
     elements in X.

     If X is a matrix, then ‘meansq (X)’ returns a row vector with each
     element containing the mean square of the corresponding column in
     X.

     If X is an array, then ‘meansq (X)’ computes the mean square along
     the first non-singleton dimension of X.

     The data in X must be numeric.  The size of Y is equal to the size
     of X except for the operating dimension, which becomes 1.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension of
     X, including any dimension exceeding ‘ndims (X)’, will return
     ‘X.^2’.

     Specifying multiple dimensions with input VECDIM, a vector of
     non-repeating dimensions, will operate along the array slice
     defined by VECDIM.  If VECDIM indexes all dimensions of X, then it
     is equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (X)’ is ignored.

     Specifying the dimension as "all" will cause ‘meansq’ to operate on
     all elements of X, and is equivalent to ‘meansq (X(:))’.

     The optional variable NANFLAG specifies whether to include or
     exclude NaN values from the calculation using any of the previously
     specified input argument combinations.  The default value for
     NANFLAG is "includenan" which keeps NaN values in the calculation.
     To exclude NaN values set the value of NANFLAG to "omitnan".  The
     output will still contain NaN values if X consists of all NaN
     values in the operating dimension.

     See also: *note rms: XREFrms, *note var: XREFvar, *note std:
     XREFstd, *note moment: XREFmoment.

 -- : Y = rms (X)
 -- : Y = rms (X, DIM)
 -- : Y = rms (X, VECDIM)
 -- : Y = rms (X, "all")
 -- : Y = rms (..., NANFLAG)
     Compute the root mean square of the input data X.

     The root mean square is defined as

          rms (X) = sqrt (1/N SUM_i X(i)^2)

     where N is the length of the X vector.

     If X is a vector, then ‘rms (X)’ returns the root mean square of
     the elements in X.

     If X is a matrix, then ‘rms (X)’ returns a row vector with each
     element containing the root mean square of the corresponding column
     in X.

     If X is an array, then ‘rms (X)’ computes the root mean square
     along the first non-singleton dimension of X.

     The data in X must be numeric.  The size of Y is equal to the size
     of X except for the operating dimension, which becomes 1.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension of
     X, including any dimension exceeding ‘ndims (X)’, will return ‘X’.

     Specifying multiple dimensions with input VECDIM, a vector of
     non-repeating dimensions, will operate along the array slice
     defined by VECDIM.  If VECDIM indexes all dimensions of X, then it
     is equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (X)’ is ignored.

     Specifying the dimension as "all" will cause ‘rms’ to operate on
     all elements of X, and is equivalent to ‘rms (X(:))’.

     The optional variable NANFLAG specifies whether to include or
     exclude NaN values from the calculation using any of the previously
     specified input argument combinations.  The default value for
     NANFLAG is "includenan" which keeps NaN values in the calculation.
     To exclude NaN values set the value of NANFLAG to "omitnan".  The
     output will still contain NaN values if X consists of all NaN
     values in the operating dimension.

     See also: *note meansq: XREFmeansq, *note var: XREFvar, *note std:
     XREFstd, *note moment: XREFmoment.

 -- : S = std (X)
 -- : S = std (X, W)
 -- : S = std (X, W, DIM)
 -- : S = std (X, W, VECDIM)
 -- : S = std (X, W, "all")
 -- : S = std (..., NANFLAG)
 -- : [S, M] = std (...)
     Compute the standard deviation of the elements of X.

     The standard deviation is defined as

          std (X) = sqrt ((1 / (N-1)) * SUM_i ((X(i) - mean(X))^2))

     where N is the number of elements of X.

     If X is a vector, then ‘std (X)’ returns the standard deviation of
     the elements in X.

     If X is a matrix, then ‘std (X)’ returns a row vector with each
     element containing the standard deviation of the corresponding
     column in X.

     If X is an array, then ‘std (X)’ computes the standard deviation
     along the first non-singleton dimension of X.

     The optional argument W determines the weighting scheme to use.
     Valid values are:

     0 [default]:
          Normalize with N-1 (population standard deviation).  This
          provides the square root of the best unbiased estimator of the
          standard deviation.

     1:
          Normalize with N (sample standard deviation).  This provides
          the square root of the second moment around the mean.

     a vector:
          Compute the weighted standard deviation with non-negative
          weights.  The length of W must equal the size of X in the
          operating dimension.  NaN values are permitted in W, will be
          multiplied with the associated values in X, and can be
          excluded by the NANFLAG option.

     an array:
          Similar to vector weights, but W must be the same size as X.
          If the operating dimension is supplied as VECDIM or "all" and
          W is not a scalar, W must be an same-sized array.

     Note: W must always be specified before specifying any of the
     following dimension options.  To use the default value for W you
     may pass an empty input argument ‘[]’.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension of
     X, including any dimension exceeding ‘ndims (X)’, will return
     ‘zeros (size (X))’.

     Specifying multiple dimensions with input VECDIM, a vector of
     non-repeating dimensions, will operate along the array slice
     defined by VECDIM.  If VECDIM indexes all dimensions of X, then it
     is equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (X)’ is ignored.

     Specifying the dimension as "all" will cause ‘std’ to operate on
     all elements of X, and is equivalent to ‘std (X(:))’.

     The optional variable NANFLAG specifies whether to include or
     exclude NaN values from the calculation using any of the previously
     specified input argument combinations.  The default value for
     NANFLAG is "includenan" which keeps NaN values in the calculation.
     To exclude NaN values set the value of NANFLAG to "omitnan".  The
     output will still contain NaN values if X consists of all NaN
     values in the operating dimension.

     The optional second output variable M contains the mean of the
     elements of X used to calculate the standard deviation.  If V is
     the weighted standard deviation, then M is also the weighted mean.

     See also: *note var: XREFvar, *note bounds: XREFbounds, *note mad:
     XREFmad, *note range: XREFrange, *note iqr: XREFiqr, *note mean:
     XREFmean, *note median: XREFmedian.

   In addition to knowing the size of a dispersion it is useful to know
the shape of the data set.  For example, are data points massed to the
left or right of the mean?  Octave provides several common measures to
describe the shape of the data set.  Octave can also calculate moments
allowing arbitrary shape measures to be developed.

 -- : V = var (X)
 -- : V = var (X, W)
 -- : V = var (X, W, DIM)
 -- : V = var (X, W, VECDIM)
 -- : V = var (X, W, "all")
 -- : V = var (..., NANFLAG)
 -- : [V, M] = var (...)
     Compute the variance of the elements of X.

     The variance is defined as

          var (X) = (1 / (N-1)) * SUM_i ((X(i) - mean(X))^2)

     where N is the number of elements of X.

     If X is a vector, then ‘var (X)’ returns the variance of the
     elements in X.

     If X is a matrix, then ‘var (X)’ returns a row vector with each
     element containing the variance of the corresponding column in X.

     If X is an array, then ‘var (X)’ computes the variance along the
     first non-singleton dimension of X.

     The optional argument W determines the weighting scheme to use.
     Valid values are:

     0 [default]:
          Normalize with N-1 (population variance).  This provides the
          square root of the best unbiased estimator of the variance.

     1:
          Normalize with N (sample variance).  This provides the square
          root of the second moment around the mean.

     a vector:
          Compute the weighted variance with non-negative weights.  The
          length of W must equal the size of X in the operating
          dimension.  NaN values are permitted in W, will be multiplied
          with the associated values in X, and can be excluded by the
          NANFLAG option.

     an array:
          Similar to vector weights, but W must be the same size as X.
          If the operating dimension is supplied as VECDIM or "all" and
          W is not a scalar, then W must match the size of the specified
          array slice.

     Note: W must always be specified before specifying any of the
     following dimension options.  To use the default value for W you
     may pass an empty input argument ‘[]’.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension of
     X, including any dimension exceeding ‘ndims (X)’, will return
     ‘zeros (size (X))’.

     Specifying multiple dimensions with input VECDIM, a vector of
     non-repeating dimensions, will operate along the array slice
     defined by VECDIM.  If VECDIM indexes all dimensions of X, then it
     is equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (X)’ is ignored.

     Specifying the dimension as "all" will cause ‘var’ to operate on
     all elements of X, and is equivalent to ‘var (X(:))’.

     The optional variable NANFLAG specifies whether to include or
     exclude NaN values from the calculation using any of the previously
     specified input argument combinations.  The default value for
     NANFLAG is "includenan" which keeps NaN values in the calculation.
     To exclude NaN values set the value of NANFLAG to "omitnan".  The
     output will still contain NaN values if X consists of all NaN
     values in the operating dimension.

     The optional second output variable M contains the mean of the
     elements of X used to calculate the variance.  If V is the weighted
     variance, then M is also the weighted mean.

     See also: *note std: XREFstd, *note mean: XREFmean, *note cov:
     XREFcov, *note skewness: XREFskewness, *note kurtosis:
     XREFkurtosis, *note moment: XREFmoment.

 -- : Y = skewness (X)
 -- : Y = skewness (X, FLAG)
 -- : Y = skewness (X, FLAG, DIM)
 -- : Y = skewness (X, FLAG, VECDIM)
 -- : Y = skewness (X, FLAG, "all")
     Compute the sample skewness of the input data X.

     The sample skewness is defined as

                         mean ((X - mean (X)).^3)
          skewness (X) = ------------------------.
                                std (X).^3

     The optional argument FLAG controls which normalization is used.
     If FLAG is equal to 1 (default value, used when FLAG is omitted or
     empty), return the sample skewness as defined above.  If FLAG is
     equal to 0, return the adjusted skewness coefficient instead:

                            sqrt (N*(N-1))   mean ((X - mean (X)).^3)
          skewness (X, 0) = -------------- * ------------------------.
                                (N - 2)             std (X).^3

     where N is the length of the X vector.

     The adjusted skewness coefficient is obtained by replacing the
     sample second and third central moments by their bias-corrected
     versions.

     If X is a vector, then ‘skewness (X)’ computes the skewness of the
     data in X.

     If X is a matrix, then ‘skewness (X)’ returns a row vector with
     each element containing the skewness of the data of the
     corresponding column in X.

     If X is an array, then ‘skewness (X)’ computes the skewness of the
     data along the first non-singleton dimension of X.

     The data in X must be numeric and any NaN values are ignored.  The
     size of Y is equal to the size of X except for the operating
     dimension, which becomes 1.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension of
     X, including any dimension exceeding ‘ndims (X)’, will return ‘X’.

     Specifying multiple dimensions with input VECDIM, a vector of
     non-repeating dimensions, will operate along the array slice
     defined by VECDIM.  If VECDIM indexes all dimensions of X, then it
     is equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (X)’ is ignored.

     Specifying the dimension as "all" will cause ‘skewness’ to operate
     on all elements of X, and is equivalent to ‘skewness (X(:))’.

     See also: *note var: XREFvar, *note kurtosis: XREFkurtosis, *note
     moment: XREFmoment.

 -- : Y = kurtosis (X)
 -- : Y = kurtosis (X, FLAG)
 -- : Y = kurtosis (X, FLAG, DIM)
 -- : Y = kurtosis (X, FLAG, VECDIM)
 -- : Y = kurtosis (X, FLAG, "all")
     Compute the sample kurtosis of the input data X.

     The sample kurtosis is defined as

               mean ((X - mean (X)).^4)
          k1 = ------------------------
                      std (X).^4

     The optional argument FLAG controls which normalization is used.
     If FLAG is equal to 1 (default value, used when FLAG is omitted or
     empty), return the sample kurtosis as defined above.  If FLAG is
     equal to 0, return the "bias-corrected" kurtosis coefficient
     instead:

                        N - 1
          k0 = 3 + -------------- * ((N + 1) * k1 - 3 * (N - 1))
                   (N - 2)(N - 3)

     where N is the length of the X vector.

     The bias-corrected kurtosis coefficient is obtained by replacing
     the sample second and fourth central moments by their unbiased
     versions.  It is an unbiased estimate of the population kurtosis
     for normal populations.

     If X is a vector, then ‘kurtosis (X)’ computes the kurtosis of the
     data in X.

     If X is a matrix, then ‘kurtosis (X)’ returns a row vector with
     each element containing the kurtosis of the data of the
     corresponding column in X.

     If X is an array, then ‘kurtosis (X)’ computes the kurtosis of the
     data along the first non-singleton dimension of X.

     The data in X must be numeric and any NaN values are ignored.  The
     size of Y is equal to the size of X except for the operating
     dimension, which becomes 1.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension of
     X, including any dimension exceeding ‘ndims (X)’, will return ‘X’.

     Specifying multiple dimensions with input VECDIM, a vector of
     non-repeating dimensions, will operate along the array slice
     defined by VECDIM.  If VECDIM indexes all dimensions of X, then it
     is equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (X)’ is ignored.

     Specifying the dimension as "all" will cause ‘kurtosis’ to operate
     on all elements of X, and is equivalent to ‘kurtosis (X(:))’.

     See also: *note var: XREFvar, *note skewness: XREFskewness, *note
     moment: XREFmoment.

 -- : M = moment (X, P)
 -- : M = moment (X, P, DIM)
 -- : M = moment (X, P, VECDIM)
 -- : M = moment (X, P, "all")
 -- : M = moment (X, P, ..., TYPE)
     Compute the P-th central moment of the input data X.

     The P-th central moment of X is defined as:

          1/N SUM_i (X(i) - mean(X))^P

     where N is the length of the X vector.

     If X is a vector, then ‘moment (X)’ computes the P-th central
     moment of the data in X.

     If X is a matrix, then ‘moment (X)’ returns a vector with element
     containing the P-th central moment of the corresponding column in
     X.

     If X is an array, then ‘moment (X)’ computes the P-th central
     moment along the first non-singleton dimension of X.

     The data in X must be a non-empty numeric array and any NaN values
     along the operating dimension will return NaN for central moment.
     The size of M is equal to the size of X except for the operating
     dimension, which becomes 1.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension of
     X, including any dimension exceeding ‘ndims (X)’, will return X.

     Specifying multiple dimensions with input VECDIM, a vector of
     non-repeating dimensions, will operate along the array slice
     defined by VECDIM.  If VECDIM indexes all dimensions of X, then it
     is equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (X)’ is ignored.  If all dimensions in VECDIM are
     greater than ‘ndims (X)’, then ‘moment’ will return X.

     Specifying the dimension as "all" will cause ‘moment’ to operate on
     all elements of X, and is equivalent to ‘moment (X(:))’.

     The optional fourth input argument, TYPE, is a string specifying
     the type of moment to be computed.  Valid options are:

     "c"
          Central Moment (default).

     "a"
     "ac"
          Absolute Central Moment.  The moment about the mean ignoring
          sign defined as

               1/N SUM_i (abs (X(i) - mean(X)))^P

     "r"
          Raw Moment.  The moment about zero defined as

               moment (X) = 1/N SUM_i X(i)^P

     "ar"
          Absolute Raw Moment.  The moment about zero ignoring sign
          defined as

               1/N SUM_i ( abs (X(i)) )^P

     See also: *note var: XREFvar, *note skewness: XREFskewness, *note
     kurtosis: XREFkurtosis.

 -- : Q = quantile (X)
 -- : Q = quantile (X, P)
 -- : Q = quantile (X, N)
 -- : Q = quantile (X, ..., DIM)
 -- : Q = quantile (X, ..., VECDIM)
 -- : Q = quantile (X, ..., "all")
 -- : Q = quantile (X, P, ..., METHOD)
 -- : Q = quantile (X, N, ..., METHOD)
     Compute the quantiles of the input data X.

     If X is a vector, then ‘quantile (X)’ computes the quantiles
     specified by P of the data in X.

     If X is a matrix, then ‘quantile (X)’ returns a matrix such that
     the i-th row of Q contains the P(i)th quantiles of each column of
     X.

     If X is an array, then ‘quantile (X)’ computes the quantiles
     specified by P along the first non-singleton dimension of X.

     The data in X must be numeric and any NaN values are ignored.  The
     size of Q is equal to the size of X except for the operating
     dimension, which equals to the number of quantiles specified by P
     or N.

     P is a numeric vector specifying the percentiles to be computed,
     which correspond to the cumulative probabilities of the data .  All
     elements of P must be in the range from 0 to 1.  If P is
     unspecified, return the percentiles for ‘[0.00 0.25 0.50 0.75
     1.00]’.  Alternatively, the second input argument may be specified
     as a positive integer value N, in which case ‘quantile’ returns the
     quantiles for N evenly spaced cumulative probabilities computed as
     (1/(N + 1), 2/(N + 1), ..., N/(N + 1)) for ‘N > 1’.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension of
     X, including any dimension exceeding ‘ndims (X)’, will return N
     copies of X along the operating dimension, where N is the number of
     specified quantiles.

     Specifying multiple dimensions with input VECDIM, a vector of
     non-repeating dimensions, will operate along the array slice
     defined by VECDIM.  If VECDIM indexes all dimensions of X, then it
     is equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (X)’ is ignored.  If all dimensions in VECDIM are
     greater than ‘ndims (X)’, then ‘quantile’ will return N copies of X
     along the smallest dimension in VECDIM.

     Specifying the dimension as "all" will cause ‘iqr’ to operate on
     all elements of X, and is equivalent to ‘iqr (X(:))’.

     The fourth input argument, METHODS, determines the method to
     calculate the quantiles specified by P or N.  The methods available
     to calculate sample quantiles are the nine methods used by R
     (<https://www.r-project.org/>) and can be specified by the
     corresponding integer value.  The default value is METHOD = 5.

     Discontinuous sample quantile methods 1, 2, and 3

       1. Method 1: Inverse of empirical distribution function.

       2. Method 2: Similar to method 1 but with averaging at
          discontinuities.

       3. Method 3: SAS definition: nearest even order statistic.

     Continuous sample quantile methods 4 through 9, where P(k) is the
     linear interpolation function respecting each method's
     representative cdf.

       4. Method 4: P(k) = k / N. That is, linear interpolation of the
          empirical cdf, where N is the length of P.

       5. Method 5: P(k) = (k - 0.5) / N. That is, a piecewise linear
          function where the knots are the values midway through the
          steps of the empirical cdf.

       6. Method 6: P(k) = k / (N + 1).

       7. Method 7: P(k) = (k - 1) / (N - 1).

       8. Method 8: P(k) = (k - 1/3) / (N + 1/3).  The resulting
          quantile estimates are approximately median-unbiased
          regardless of the distribution of X.

       9. Method 9: P(k) = (k - 3/8) / (N + 1/4).  The resulting
          quantile estimates are approximately unbiased for the expected
          order statistics if X is normally distributed.

     Hyndman and Fan (1996) recommend method 8.  Maxima, S, and R
     (versions prior to 2.0.0) use 7 as their default.  Minitab and SPSS
     use method 6.  MATLAB uses method 5.

     References:

        • R. A. Becker, J. M. Chambers, and A. R. Wilks, ‘The New S
          Language’, Wadsworth & Brooks/Cole, 1988.

        • R. J. Hyndman, and Y. Fan, "Sample quantiles in statistical
          packages", ‘American Statistician’, 50, pp. 361-365, 1996.

        • ‘R: A Language and Environment for Statistical Computing’,
          <https://cran.r-project.org/doc/manuals/fullrefman.pdf>.

     Examples:

          x = randi (1000, [10, 1]);  # Create empirical data in range 1-1000
          q = quantile (x, [0, 1]);   # Return minimum, maximum of distribution
          q = quantile (x, [0.25 0.5 0.75]); # Return quartiles of distribution

     See also: *note prctile: XREFprctile.

 -- : Q = prctile (X)
 -- : Q = prctile (X, P)
 -- : Q = prctile (X, P, DIM)
 -- : Q = prctile (X, P, VECDIM)
 -- : Q = prctile (X, P, "all")
 -- : Q = prctile (X, P, ..., METHOD)
     Compute the percentiles of the input data X.

     If X is a vector, then ‘prctile (X)’ computes the percentiles
     specified by P of the data in X.

     If X is a matrix, then ‘prctile (X)’ returns a matrix such that the
     i-th row of Q contains the P(i)th percentiles of each column of X.

     If X is an array, then ‘prctile (X)’ computes the percentiles
     specified by P along the first non-singleton dimension of X.

     The data in X must be numeric and any NaN values are ignored.  The
     size of Q is equal to the size of X except for the operating
     dimension, which equals to the number of quantiles specified by P.

     P is a numeric vector specifying the percentiles to be computed.
     All elements of P must be in the range from 0 to 100.  If P is
     unspecified, return the percentiles for ‘[0 25 50 75 100]’.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension of
     X, including any dimension exceeding ‘ndims (X)’, will return N
     copies of X along the operating dimension, where N is the number of
     specified percentiles.

     Specifying multiple dimensions with input VECDIM, a vector of
     non-repeating dimensions, will operate along the array slice
     defined by VECDIM.  If VECDIM indexes all dimensions of X, then it
     is equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (X)’ is ignored.  If all dimensions in VECDIM are
     greater than ‘ndims (X)’, then ‘quantile’ will return N copies of X
     along the smallest dimension in VECDIM.

     Specifying the dimension as "all" will cause ‘iqr’ to operate on
     all elements of X, and is equivalent to ‘iqr (X(:))’.

     The fourth input argument, METHODS, determines the method to
     calculate the percentiles specified by P.  The methods available to
     calculate sample percentiles are the nine methods used by R
     (<https://www.r-project.org/>) and can be specified by the
     corresponding integer value.  The default value is METHOD = 5.

     Discontinuous sample quantile methods 1, 2, and 3

       1. Method 1: Inverse of empirical distribution function.

       2. Method 2: Similar to method 1 but with averaging at
          discontinuities.

       3. Method 3: SAS definition: nearest even order statistic.

     Continuous sample quantile methods 4 through 9, where P(k) is the
     linear interpolation function respecting each method's
     representative cdf.

       4. Method 4: P(k) = k / N. That is, linear interpolation of the
          empirical cdf, where N is the length of P.

       5. Method 5: P(k) = (k - 0.5) / N. That is, a piecewise linear
          function where the knots are the values midway through the
          steps of the empirical cdf.

       6. Method 6: P(k) = k / (N + 1).

       7. Method 7: P(k) = (k - 1) / (N - 1).

       8. Method 8: P(k) = (k - 1/3) / (N + 1/3).  The resulting
          quantile estimates are approximately median-unbiased
          regardless of the distribution of X.

       9. Method 9: P(k) = (k - 3/8) / (N + 1/4).  The resulting
          quantile estimates are approximately unbiased for the expected
          order statistics if X is normally distributed.

     See also: *note quantile: XREFquantile.

   A summary view of a data set can be generated quickly with the
‘statistics’ function.

 -- : STATS = statistics (X)
 -- : STATS = statistics (X, DIM)
 -- : STATS = statistics (X, VECDIM)
 -- : STATS = statistics (X, "all")
 -- : STATS = statistics (..., NANFLAG)
     Return a vector with statistics parameters over the input data X.

     ‘statistics (X’ operates along the first non-singleton dimension of
     X and calculates the following statistical parameters:
       1. minimum
       2. first quartile
       3. median
       4. third quartile
       5. maximum
       6. mean
       7. standard deviation
       8. skewness
       9. kurtosis

     If X is a row vector, then ‘statistics (X)’ returns a row vector
     with the aforementioned statistical parameters.  If X is a column
     vector, then it returns a column vector.

     If X is a matrix, then ‘statistics (X)’ returns a matrix such that
     each column contains the statistical parameters calculated over the
     corresponding column of X.

     If X is an array, then ‘statistics (X)’ computes the statistical
     parameters along the first non-singleton dimension of X.

     The data in X must be numeric and by default any NaN values are
     ignored from the computations of statistical parameters except for
     the mean and the standard deviation.  Set the optional argument
     NANFLAG to "omitnan" to exclude the NaN values from the calculation
     of the mean and standard deviation parameters.  Setting NANFLAG to
     "includenan" is ignored and it is equivalent to calling the
     ‘statistics’ function without the NANFLAG argument.

     The size of STATS is equal to the size of X except for the
     operating dimension, which equals to 9 (i.e., the number of
     statistical parameters returned).

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension of
     X, including any dimension exceeding ‘ndims (X)’, will return ‘X’.

     Specifying multiple dimensions with input VECDIM, a vector of
     non-repeating dimensions, will operate along the array slice
     defined by VECDIM.  If VECDIM indexes all dimensions of X, then it
     is equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (X)’ is ignored.

     Specifying the dimension as "all" will cause ‘statistics’ to
     operate on all elements of X, and is equivalent to ‘statistics
     (X(:))’.

     See also: *note min: XREFmin, *note max: XREFmax, *note median:
     XREFmedian, *note mean: XREFmean, *note std: XREFstd, *note
     skewness: XREFskewness, *note kurtosis: XREFkurtosis.


File: octave.info,  Node: Statistics on Sliding Windows of Data,  Next: Basic Statistical Functions,  Prev: Descriptive Statistics,  Up: Statistics

26.2 Statistics on Sliding Windows of Data
==========================================

It is often useful to calculate descriptive statistics over a subsection
(i.e., window) of a full dataset.  Octave provides the function ‘movfun’
which will call an arbitrary function handle with windows of data and
accumulate the results.  Many of the most commonly desired functions,
such as the moving average over a window of data (‘movmean’), are
already provided.

 -- : Y = movfun (FCN, X, WLEN)
 -- : Y = movfun (FCN, X, [NB, NA])
 -- : Y = movfun (..., "PROPERTY", VALUE)

     Apply function FCN to a moving window of length WLEN on data X.

     The moving window length input WLEN can either be a numeric scalar
     or a 2-element numeric array ‘[NB, NA]’.  The elements included in
     the moving window depend on the size and value of WLEN as well as
     whether the "SamplePoints" option has been specified.  For full
     details of element inclusion, *note ‘movslice’: XREFmovslice.

     During calculations the data input X is reshaped into a
     2-dimensional WLEN-by-N matrix and FCN is called on this new
     matrix.  Therefore, FCN must accept an array input argument and
     apply the computation along dimension 1, i.e., down the columns of
     the array.

     When applied to an array (possibly multi-dimensional) with N
     columns, FCN may return a result in either of two formats:
     Format 1) an array of size 1-by-N-by-DIM3-by-...-by-DIMN.  This is
     the typical output format from Octave core functions.  Type ‘demo
     ("movfun", 5)’ for an example of this use case.  Format 2) a row
     vector of length ‘N * NUMEL_HIGHER_DIMS’ where NUMEL_HIGHER_DIMS is
     ‘prod (size (X)(3:end))’.  The output of FCN for the i-th input
     column must be found in the output at indices
     ‘i:N:(N*NUMEL_HIGHER_DIMS)’.  This format is useful when
     concatenating functions into arrays, or when using ‘nthargout’.
     Type ‘demo ("movfun", 6)’ for an example of this case.

     The calculation can be controlled by specifying PROPERTY/VALUE
     pairs.  Valid properties are

     "dim"
          Operate along the dimension specified, rather than the default
          of the first non-singleton dimension.

     "SamplePoints"

          This property specifies a sorted, numeric vector of unique
          coordinate positions of the data points in X.  The default
          value is the vector ‘[1 : NUMEL (X)]’.  When a non-default
          SamplePoints vector is specified, the moving window length
          WLEN is measured against the SamplePoints positions to
          determine which points are included in each window slice.
          SamplePoints need not be uniformly spaced.  This can result in
          window slices containing different numbers of points.

     "Endpoints"

          This property controls how results are calculated at the
          boundaries (endpoints) of the window.  Possible values are:

          "shrink" (default)
               The window is truncated at the beginning and end of the
               array to exclude elements for which there is no source
               data.  For example, with a window of length 3, ‘Y(1) =
               FCN (X(1:2))’, and ‘Y(end) = FCN (X(end-1:end))’.

          "discard"
               Any Y values that use a window extending beyond the
               original data array are deleted.  For example, with a
               10-element data vector and a window of length 3, the
               output will contain only 8 elements.  The first element
               would require calculating the function over indices
               ‘[0, 1, 2]’ and is therefore discarded.  The last element
               would require calculating the function over indices
               ‘[9, 10, 11]’ and is therefore discarded.

          "fill"
               Any window elements outside the data array are replaced
               by ‘NaN’.  For example, with a window of length 3, ‘Y(1)
               = FCN ([NaN, X(1:2)])’, and ‘Y(end) = FCN ([X(end-1:end),
               NaN])’.  This option usually results in Y having ‘NaN’
               values at the boundaries, although it is influenced by
               how FCN handles ‘NaN’, and also by the property
               "nancond".

          USER_VALUE
               Any window elements outside the data array are replaced
               by the specified value USER_VALUE which must be a numeric
               scalar.  For example, with a window of length 3, ‘Y(1) =
               FCN ([USER_VALUE, X(1:2)])’, and ‘Y(end) = FCN
               ([X(end-1:end), USER_VALUE])’.  A common choice for
               USER_VALUE is 0.

          "same"
               Any window elements outside the data array are replaced
               by the value of X at the boundary.  For example, with a
               window of length 3, ‘Y(1) = FCN ([X(1), X(1:2)])’, and
               ‘Y(end) = FCN ([X(end-1:end), X(end)])’.

          "periodic"
               The window is wrapped so that any missing data elements
               are taken from the other side of the data.  For example,
               with a window of length 3, ‘Y(1) = FCN ([X(end),
               X(1:2)])’, and ‘Y(end) = FCN ([X(end-1:end), X(1)])’.

          Note 1: For non-uniform SamplePoint spacing, the only
          permitted value for "EndPoints" is "shrink".

          Note 2: For some "Endpoints" options, the window size at the
          boundaries may not be the same as for the central part, and
          FCN must work in these cases.

     "nancond"
          Controls haw ‘NaN’ and ‘NA’ values affect the output of
          "movfun".  The value "includenan" (default) causes ‘NaN’ and
          ‘NA’ values to be included in the moving window, and any
          window slice containing ‘NaN’ or ‘NA’ values will return ‘NaN’
          for that element.  The value "omitnan" causes "movfun" to
          ignore any ‘NaN’ or ‘NA’ values resulting in fewer elements
          being used to calculate the result for that window slice.  If
          "omitnan" is specified and a window slice contains all ‘NaN’
          or ‘NA’ values, "movfun" returns the value specified by the
          "nanval" property for that element.  The values
          "includemissing" and "omitmissing" may be used synonymously
          with "includenan" and "omitnan", respectively.

     "nanval"
          Specifies the value to return when "nancond" is set to
          "omitnan" or "omitmissing" and all elements in a window are
          ‘NaN’ or ‘NA’.  "nanval" must be a numeric scalar value or
          ‘NaN’ (default).

     "outdim"
          A row vector that selects which dimensions of the calculation
          will appear in the output Y.  This is only useful when FCN
          returns an N-dimensional array in Format 1.  The default is to
          return all output dimensions.

     Programming Note: The property "outdim" can be used to save memory
     when the output of FCN has many dimensions, or when a wrapper to
     the base function that selects the desired outputs is too costly.
     When memory is not an issue, the easiest way to select output
     dimensions is to first calculate the complete result with ‘movfun’
     and then filter that result with indexing.  If code complexity is
     not an issue then a wrapper can be created using anonymous
     functions.  For example, if ‘basefcn’ is a function returning a
     K-dimensional row output, and only dimension D is desired, then the
     following wrapper could be used.

          FCN = @(x) basefcn (x)(:,columns(x) * (D-1) + (1:columns(x)));
          Y = movfun (@fcn, ...);

     See also: *note movslice: XREFmovslice, *note prepad: XREFprepad,
     *note postpad: XREFpostpad, *note permute: XREFpermute, *note
     reshape: XREFreshape.

 -- : SLCIDX = movslice (N, WLEN)
 -- : SLCIDX = movslice (N, WLEN, SAMPLEPOINTS)
 -- : [SLCIDX, C, CPRE, CPOST, WIN, WLEN, SCALAR_WLEN] = movslice (...)
     Generate indices to slice a vector of length N into windows of
     length WLEN.

     The input N must be a positive integer.

     The moving window length input WLEN can either be a numeric scalar
     or a 2-element numeric array.  The elements included in the moving
     window will depend on the size and value of WLEN as well as whether
     the SAMPLEPOINTS input was specified.

     The optional input SAMPLEPOINTS is a sorted, numeric vector of
     unique positions of the N data points.  The default value is the
     vector ‘[1 : N]’.  When a non-default SAMPLEPOINTS vector is
     specified, the moving window length WLEN is measured against the
     SAMPLEPOINTS positions to determine which points are included in
     each window slice.  It should be noted that SAMPLEPOINTS need not
     be uniformly spaced which can result in window slices containing
     different numbers of points.  Because of this, as specified below
     the shape and content of some ‘movslice’ outputs will be different
     when a non-default SAMPLEPOINTS is used.

     The moving window size and included elements will be defined as
     follows:

        • If SAMPLEPOINTS has the default value of ‘1:N’ (or has not
          been specified):

             • For integer-valued WLEN:

                  • For odd, integer-valued, scalar WLEN the window is
                    symmetric and includes ‘(WLEN - 1) / 2’ elements on
                    either side of the central element.  For example,
                    the window slice at index 5 with a window length of
                    3 will include the elements ‘[4, 5, 6]’.

                  • For even, integer-valued, scalar WLEN the window is
                    asymmetric and has ‘WLEN/2’ elements to the left of
                    the central element and ‘WLEN/2 - 1’ elements to the
                    right of the central element.  For example, the
                    window slice at index 5 with a window length of 4
                    will include the elements ‘[3, 4, 5, 6]’.

                  • For integer-valued vector WLEN of the form
                    ‘[NB, NA]’ where NB and NA are integer valued the
                    window includes NB elements to the left of the
                    central element and NA elements to the right of the
                    central element.  For example, given
                    ‘WLEN = [3, 1]’, the window slice at index 5 will
                    include the elements ‘[2, 3, 4, 5, 6]’.

             • For non-integer-valued scalar WLEN:

                  • Non-integer-valued scalar WLEN will be converted to
                    two-element vector form with
                    ‘NB = NA = fix (WLEN / 2)’, and then processed as
                    stated above for integer-valued vectors.  For
                    example, the window slice at index 5 with
                    ‘WLEN = 2.5’ will include the elements
                    ‘[3, 4, 5, 6, 7]’.

                  • Non-integer-valued vector WLEN will be truncated to
                    integer values with ‘WLEN = fix (WLEN)’ and then
                    processed as stated above for integer-valued
                    vectors.  For example, the window slice at index 5
                    with ‘WLEN = [1.2, 2.3]’ will include the elements
                    ‘[4, 5, 6, 7]’.

        • If SAMPLEPOINTS has been specified with a non-default vector:

             • For vector WLEN specified as ‘[NB, NA]’, the window will
               include all points within a distance less than or equal
               to NB before and NA after the central element's position,
               with point positions defined by the elements of
               SAMPLEPOINTS.  For example, at index 5 with
               ‘WLEN = [2, 3]’ and the 3rd-8th elements of SAMPLEPOINTS
               being ‘[1, 3, 5, 7, 8 ,9]’, the window slice will include
               the elements ‘[4, 5, 6, 7]’ corresponding to SAMPLEPOINTS
               ‘[3, 5, 7, 8]’.

             • Scalar WLEN will be converted to two-element vector form
               with ‘NB = NA = WLEN / 2’.  The window will then include
               all points within a distance of less than or equal to NB
               before and less than, but not equal to, NA after the
               central element's position, ‘[NB, NA)’.  For example, at
               index 5 with ‘WLEN = [2, 3]’ and the 3rd-8th elements of
               SAMPLEPOINTS being ‘[1, 3, 5, 7, 8 ,9]’, the window slice
               will include the elements ‘[4, 5, 6]’ corresponding to
               SAMPLEPOINTS ‘[3, 5, 7]’.

     The output SLCIDX is an array of indices of the slices of the
     vector.

        • If SAMPLEPOINTS is default or unspecified, SLCIDX will contain
          only the indices of the slices that fit fully within the
          vector.  Each column will be the indices of one slice as the
          window moves from left to right.  The slices will have
          ‘fix (WLEN)’ elements for scalar WLEN, or
          ‘NB + NA + 1’ elements for array valued WLEN.

        • If a non-default SAMPLEPOINTS has been specified, SLCIDX will
          be a 2xN array with the first and second rows containing the
          first and last elements of each slice, respectively.

     Optional output C is a row vector of window center positions where
     the window stays fully within the vector.

     Optional outputs CPRE and CPOST contain the vector elements at the
     start and end of the vector, respectively, that result in the
     window extending beyond the ends of the vector.

     Optional output WIN contains information for creating the moving
     window.

        • If SAMPLEPOINTS is default or unspecified, WIN is a column
          vector with the same number of rows as SLCIDX that contains
          the moving window defined as a center relative position
          stencil.

        • If a non-default SAMPLEPOINTS has been specified, WIN will be
          a 2xN array with the first and second rows containing the left
          and right bounds of each window slice, respectively, using the
          same coordinates as SAMPLEPOINTS.  These bounds may lie
          outside of the position vector specified by SAMPLEPOINTS.

     Optional output WLEN returns the window length used by ‘movslice’
     in two-element ‘[NB, NA]’ form.

     Optional logical output SCALAR_WLEN returns the scalar or vector
     state of the input WLEN so that calling functions can determinine
     whether the moving window should be inclusive or exclusive of the
     right window endpoints.  I.e., inclusive ‘[NB, NA]’ for vector WLEN
     or exclusive ‘[NB, NA)’ for scalar WLEN.

     See also: *note movfun: XREFmovfun.

 -- : Y = movmad (X, WLEN)
 -- : Y = movmad (X, [NB, NA])
 -- : Y = movmad (..., DIM)
 -- : Y = movmad (..., NANCOND)
 -- : Y = movmad (..., PROPERTY, VALUE)
     Calculate the moving median or mean absolute deviation over a
     sliding window of length WLEN on data X.

     The moving window length input WLEN can either be a numeric scalar
     or a 2-element numeric array ‘[NB, NA]’.  The elements included in
     the moving window depend on the size and value of WLEN as well as
     whether the "SamplePoints" option has been specified.  For full
     details of element inclusion, *note ‘movslice’: XREFmovslice.

     If the optional argument DIM is given, operate along this
     dimension.

     The optional argument NANCOND is a string that controls how ‘NaN’
     and ‘NA’ values affect the output of "movmad".  The value
     "includenan" causes ‘NaN’ and ‘NA’ values to be included in the
     moving window, and any window slice containing ‘NaN’ or ‘NA’ values
     will return ‘NaN’ for that element.  The value "omitnan" (default)
     causes "movmad" to ignore any ‘NaN’ or ‘NA’ values resulting in
     fewer elements being used to calculate the mad for that window
     slice.  If "omitnan" is specified and a window slice contains all
     ‘NaN’ or ‘NA’ values, "movmad" returns ‘NaN’ for that element.  The
     values "includemissing" and "omitmissing" may be used synonymously
     with "includenan" and "omitnan", respectively.

     The calculation can be controlled by specifying PROPERTY/VALUE
     pairs:

        • The "method" property can take the value "median" (default) or
          "mean" to control whether "movmad" performs median or mean
          absolute deviation calculations on the data.

        • Additional valid properties are "Endpoints" and
          "SamplePoints".  For full descriptions of these properties and
          valid options, *note ‘movfun’: XREFmovfun.

     Programming Note: This function is a wrapper which calls ‘movfun’.
     For full documentation of inputs and options, *note ‘movfun’:
     XREFmovfun.

     Compatibility Note: Prior to Octave 10 this function only
     calculated mean absolute deviation.  For MATLAB compatibility, the
     default has been changed to median absolute deviation.  The
     "method" property is now provided to enable access to both "mad"
     calculation methods.  This property should not be expected to be
     functional outside of Octave code.

     See also: *note mad: XREFmad, *note movfun: XREFmovfun, *note
     movslice: XREFmovslice, *note movmax: XREFmovmax, *note movmean:
     XREFmovmean, *note movmedian: XREFmovmedian, *note movmin:
     XREFmovmin, *note movprod: XREFmovprod, *note movstd: XREFmovstd,
     *note movsum: XREFmovsum, *note movvar: XREFmovvar.

 -- : Y = movmax (X, WLEN)
 -- : Y = movmax (X, [NB, NA])
 -- : Y = movmax (..., DIM)
 -- : Y = movmax (..., NANCOND)
 -- : Y = movmax (..., PROPERTY, VALUE)
     Calculate the moving maximum over a sliding window of length WLEN
     on data X.

     The moving window length input WLEN can either be a numeric scalar
     or a 2-element numeric array ‘[NB, NA]’.  The elements included in
     the moving window depend on the size and value of WLEN as well as
     whether the "SamplePoints" option has been specified.  For full
     details of element inclusion, *note ‘movslice’: XREFmovslice.

     If the optional argument DIM is given, operate along this
     dimension.

     The optional argument NANCOND is a string that controls how ‘NaN’
     and ‘NA’ values affect the output of "movmax".  The value
     "includenan" causes ‘NaN’ and ‘NA’ values to be included in the
     moving window, and any window slice containing ‘NaN’ or ‘NA’ values
     will return ‘NaN’ for that element.  The value "omitnan" (default)
     causes "movmax" to ignore any ‘NaN’ or ‘NA’ values resulting in
     fewer elements being used to calculate the maximum for that window
     slice.  If "omitnan" is specified and a window slice contains all
     ‘NaN’ or ‘NA’ values, "movmax" returns ‘NaN’ for that element.  The
     values "includemissing" and "omitmissing" may be used synonymously
     with "includenan" and "omitnan", respectively.

     The calculation can be controlled by specifying PROPERTY/VALUE
     pairs.  Valid properties are "Endpoints" and "SamplePoints".  For
     full descriptions of these properties and valid options, *note
     ‘movfun’: XREFmovfun.

     Programming Note: This function is a wrapper which calls ‘movfun’.
     For full documentation of inputs and options, *note ‘movfun’:
     XREFmovfun.

     See also: *note max: XREFmax, *note movfun: XREFmovfun, *note
     movslice: XREFmovslice, *note movmad: XREFmovmad, *note movmean:
     XREFmovmean, *note movmedian: XREFmovmedian, *note movmin:
     XREFmovmin, *note movprod: XREFmovprod, *note movstd: XREFmovstd,
     *note movsum: XREFmovsum, *note movvar: XREFmovvar.

 -- : Y = movmean (X, WLEN)
 -- : Y = movmean (X, [NB, NA])
 -- : Y = movmean (..., DIM)
 -- : Y = movmean (..., NANCOND)
 -- : Y = movmean (..., PROPERTY, VALUE)
     Calculate the moving average over a sliding window of length WLEN
     on data X.

     The moving window length input WLEN can either be a numeric scalar
     or a 2-element numeric array ‘[NB, NA]’.  The elements included in
     the moving window depend on the size and value of WLEN as well as
     whether the "SamplePoints" option has been specified.  For full
     details of element inclusion, *note ‘movslice’: XREFmovslice.

     If the optional argument DIM is given, operate along this
     dimension.

     The optional argument NANCOND is a string that controls how ‘NaN’
     and ‘NA’ values affect the output of "movmean".  The value
     "includenan" (default) causes ‘NaN’ and ‘NA’ values to be included
     in the moving window, and any window slice containing ‘NaN’ or ‘NA’
     values will return ‘NaN’ for that element.  The value "omitnan"
     causes "movmean" to ignore any ‘NaN’ or ‘NA’ values resulting in
     fewer elements being used to calculate the mean for that window
     slice.  If "omitnan" is specified and a window slice contains all
     ‘NaN’ or ‘NA’ values, "movmean" returns ‘NaN’ for that element.
     The values "includemissing" and "omitmissing" may be used
     synonymously with "includenan" and "omitnan", respectively.

     The calculation can be controlled by specifying PROPERTY/VALUE
     pairs.  Valid properties are "Endpoints" and "SamplePoints".  For
     full descriptions of these properties and valid options, *note
     ‘movfun’: XREFmovfun.

     Programming Note: This function is a wrapper which calls ‘movfun’.
     For full documentation of inputs and options, *note ‘movfun’:
     XREFmovfun.

     See also: *note mean: XREFmean, *note movfun: XREFmovfun, *note
     movslice: XREFmovslice, *note movmad: XREFmovmad, *note movmax:
     XREFmovmax, *note movmedian: XREFmovmedian, *note movmin:
     XREFmovmin, *note movprod: XREFmovprod, *note movstd: XREFmovstd,
     *note movsum: XREFmovsum, *note movvar: XREFmovvar.

 -- : Y = movmedian (X, WLEN)
 -- : Y = movmedian (X, [NB, NA])
 -- : Y = movmedian (..., DIM)
 -- : Y = movmedian (..., NANCOND)
 -- : Y = movmedian (..., PROPERTY, VALUE)
     Calculate the moving median over a sliding window of length WLEN on
     data X.

     The moving window length input WLEN can either be a numeric scalar
     or a 2-element numeric array ‘[NB, NA]’.  The elements included in
     the moving window depend on the size and value of WLEN as well as
     whether the "SamplePoints" option has been specified.  For full
     details of element inclusion, *note ‘movslice’: XREFmovslice.

     If the optional argument DIM is given, operate along this
     dimension.

     The optional argument NANCOND is a string that controls how ‘NaN’
     and ‘NA’ values affect the output of "movmedian".  The value
     "includenan" (default) causes ‘NaN’ and ‘NA’ values to be included
     in the moving window, and any window slice containing ‘NaN’ or ‘NA’
     values will return ‘NaN’ for that element.  The value "omitnan"
     causes "movmedian" to ignore any ‘NaN’ or ‘NA’ values resulting in
     fewer elements being used to calculate the median for that window
     slice.  If "omitnan" is specified and a window slice contains all
     ‘NaN’ or ‘NA’ values, "movmedian" returns ‘NaN’ for that element.
     The values "includemissing" and "omitmissing" may be used
     synonymously with "includenan" and "omitnan", respectively.

     The calculation can be controlled by specifying PROPERTY/VALUE
     pairs.  Valid properties are "Endpoints" and "SamplePoints".  For
     full descriptions of these properties and valid options, *note
     ‘movfun’: XREFmovfun.

     Programming Note: This function is a wrapper which calls ‘movfun’.
     For full documentation of inputs and options, *note ‘movfun’:
     XREFmovfun.

     See also: *note median: XREFmedian, *note movfun: XREFmovfun, *note
     movslice: XREFmovslice, *note movmad: XREFmovmad, *note movmax:
     XREFmovmax, *note movmean: XREFmovmean, *note movmin: XREFmovmin,
     *note movprod: XREFmovprod, *note movstd: XREFmovstd, *note movsum:
     XREFmovsum, *note movvar: XREFmovvar.

 -- : Y = movmin (X, WLEN)
 -- : Y = movmin (X, [NB, NA])
 -- : Y = movmin (..., DIM)
 -- : Y = movmin (..., NANCOND)
 -- : Y = movmin (..., PROPERTY, VALUE)
     Calculate the moving minimum over a sliding window of length WLEN
     on data X.

     The moving window length input WLEN can either be a numeric scalar
     or a 2-element numeric array ‘[NB, NA]’.  The elements included in
     the moving window depend on the size and value of WLEN as well as
     whether the "SamplePoints" option has been specified.  For full
     details of element inclusion, *note ‘movslice’: XREFmovslice.

     If the optional argument DIM is given, operate along this
     dimension.

     The optional argument NANCOND is a string that controls how ‘NaN’
     and ‘NA’ values affect the output of "movmin".  The value
     "includenan" causes ‘NaN’ and ‘NA’ values to be included in the
     moving window, and any window slice containing ‘NaN’ or ‘NA’ values
     will return ‘NaN’ for that element.  The value "omitnan" (default)
     causes "movmin" to ignore any ‘NaN’ or ‘NA’ values resulting in
     fewer elements being used to calculate the minimum for that window
     slice.  If "omitnan" is specified and a window slice contains all
     ‘NaN’ or ‘NA’ values, "movmin" returns ‘NaN’ for that element.  The
     values "includemissing" and "omitmissing" may be used synonymously
     with "includenan" and "omitnan", respectively.

     The calculation can be controlled by specifying PROPERTY/VALUE
     pairs.  Valid properties are "Endpoints" and "SamplePoints".  For
     full descriptions of these properties and valid options, *note
     ‘movfun’: XREFmovfun.

     Programming Note: This function is a wrapper which calls ‘movfun’.
     For full documentation of inputs and options, *note ‘movfun’:
     XREFmovfun.

     See also: *note min: XREFmin, *note movfun: XREFmovfun, *note
     movslice: XREFmovslice, *note movmad: XREFmovmad, *note movmax:
     XREFmovmax, *note movmean: XREFmovmean, *note movmedian:
     XREFmovmedian, *note movprod: XREFmovprod, *note movstd:
     XREFmovstd, *note movsum: XREFmovsum, *note movvar: XREFmovvar.

 -- : Y = movprod (X, WLEN)
 -- : Y = movprod (X, [NB, NA])
 -- : Y = movprod (..., DIM)
 -- : Y = movprod (..., NANCOND)
 -- : Y = movprod (..., PROPERTY, VALUE)
     Calculate the moving product over a sliding window of length WLEN
     on data X.

     The moving window length input WLEN can either be a numeric scalar
     or a 2-element numeric array ‘[NB, NA]’.  The elements included in
     the moving window depend on the size and value of WLEN as well as
     whether the "SamplePoints" option has been specified.  For full
     details of element inclusion, *note ‘movslice’: XREFmovslice.

     If the optional argument DIM is given, operate along this
     dimension.

     The optional argument NANCOND is a string that controls how ‘NaN’
     and ‘NA’ values affect the output of "movprod".  The value
     "includenan" (default) causes ‘NaN’ and ‘NA’ values to be included
     in the moving window, and any window slice containing ‘NaN’ or ‘NA’
     values will return ‘NaN’ for that element.  The value "omitnan"
     causes "movprod" to ignore any ‘NaN’ or ‘NA’ values resulting in
     fewer elements being used to calculate the product for that window
     slice.  If "omitnan" is specified and a window slice contains all
     ‘NaN’ or ‘NA’ values, "movprod" returns 1 for that element.  The
     values "includemissing" and "omitmissing" may be used synonymously
     with "includenan" and "omitnan", respectively.

     The calculation can be controlled by specifying PROPERTY/VALUE
     pairs.  Valid properties are "Endpoints" and "SamplePoints".  For
     full descriptions of these properties and valid options, *note
     ‘movfun’: XREFmovfun.

     Programming Note: This function is a wrapper which calls ‘movfun’.
     For full documentation of inputs and options, *note ‘movfun’:
     XREFmovfun.

     See also: *note prod: XREFprod, *note movfun: XREFmovfun, *note
     movslice: XREFmovslice, *note movmad: XREFmovmad, *note movmax:
     XREFmovmax, *note movmean: XREFmovmean, *note movmedian:
     XREFmovmedian, *note movmin: XREFmovmin, *note movstd: XREFmovstd,
     *note movsum: XREFmovsum, *note movvar: XREFmovvar.

 -- : Y = movstd (X, WLEN)
 -- : Y = movstd (X, [NB, NA])
 -- : Y = movstd (..., OPT)
 -- : Y = movstd (..., OPT, DIM)
 -- : Y = movstd (..., NANCOND)
 -- : Y = movstd (..., PROPERTY, VALUE)
     Calculate the moving standard deviation over a sliding window of
     length WLEN on data X.

     The moving window length input WLEN can either be a numeric scalar
     or a 2-element numeric array ‘[NB, NA]’.  The elements included in
     the moving window depend on the size and value of WLEN as well as
     whether the "SamplePoints" option has been specified.  For full
     details of element inclusion, *note ‘movslice’: XREFmovslice.

     The optional argument OPT determines the type of normalization to
     use.  Valid values are

     0:
          normalize with N-1, provides the square root of the best
          unbiased estimator of the variance [default]

     1:
          normalize with N, this provides the square root of the second
          moment around the mean

     If the optional argument DIM is given, operate along this
     dimension.  The normalization argument OPT must be given before the
     dimension.  To use the default value for OPT you may pass an empty
     input argument ‘[]’.

     The optional argument NANCOND is a string that controls how ‘NaN’
     and ‘NA’ values affect the output of "movstd".  The value
     "includenan" (default) causes ‘NaN’ and ‘NA’ values to be included
     in the moving window, and any window slice containing ‘NaN’ or ‘NA’
     values will return ‘NaN’ for that element.  The value "omitnan"
     causes "movstd" to ignore any ‘NaN’ or ‘NA’ values resulting in
     fewer elements being used to calculate the standard deviation for
     that window slice.  If "omitnan" is specified and a window slice
     contains all ‘NaN’ or ‘NA’ values, "movstd" returns ‘NaN’ for that
     element.  The values "includemissing" and "omitmissing" may be used
     synonymously with "includenan" and "omitnan", respectively.

     The calculation can be controlled by specifying PROPERTY/VALUE
     pairs.  Valid properties are "Endpoints" and "SamplePoints".  For
     full descriptions of these properties and valid options, *note
     ‘movfun’: XREFmovfun.

     Programming Note: This function is a wrapper which calls ‘movfun’.
     For full documentation of inputs and options, *note ‘movfun’:
     XREFmovfun.

     See also: *note std: XREFstd, *note movfun: XREFmovfun, *note
     movslice: XREFmovslice, *note movmad: XREFmovmad, *note movmax:
     XREFmovmax, *note movmean: XREFmovmean, *note movmedian:
     XREFmovmedian, *note movmin: XREFmovmin, *note movprod:
     XREFmovprod, *note movsum: XREFmovsum, *note movvar: XREFmovvar.

 -- : Y = movsum (X, WLEN)
 -- : Y = movsum (X, [NB, NA])
 -- : Y = movsum (..., DIM)
 -- : Y = movsum (..., NANCOND)
 -- : Y = movsum (..., PROPERTY, VALUE)
     Calculate the moving sum over a sliding window of length WLEN on
     data X.

     The moving window length input WLEN can either be a numeric scalar
     or a 2-element numeric array ‘[NB, NA]’.  The elements included in
     the moving window depend on the size and value of WLEN as well as
     whether the "SamplePoints" option has been specified.  For full
     details of element inclusion, *note ‘movslice’: XREFmovslice.

     If the optional argument DIM is given, operate along this
     dimension.

     The optional argument NANCOND is a string that controls how ‘NaN’
     and ‘NA’ values affect the output of "movsum".  The value
     "includenan" (default) causes ‘NaN’ and ‘NA’ values to be included
     in the moving window, and any window slice containing ‘NaN’ or ‘NA’
     values will return ‘NaN’ for that element.  The value "omitnan"
     causes "movsum" to ignore any ‘NaN’ or ‘NA’ values resulting in
     fewer elements being used to calculate the sum for that window
     slice.  If "omitnan" is specified and a window slice contains all
     ‘NaN’ or ‘NA’ values, "movsum" returns 0 for that element.  The
     values "includemissing" and "omitmissing" may be used synonymously
     with "includenan" and "omitnan", respectively.

     The calculation can be controlled by specifying PROPERTY/VALUE
     pairs.  Valid properties are "Endpoints" and "SamplePoints".  For
     full descriptions of these properties and valid options, *note
     ‘movfun’: XREFmovfun.

     Programming Note: This function is a wrapper which calls ‘movfun’.
     For full documentation of inputs and options, *note ‘movfun’:
     XREFmovfun.

     See also: *note sum: XREFsum, *note movfun: XREFmovfun, *note
     movslice: XREFmovslice, *note movmad: XREFmovmad, *note movmax:
     XREFmovmax, *note movmean: XREFmovmean, *note movmedian:
     XREFmovmedian, *note movmin: XREFmovmin, *note movprod:
     XREFmovprod, *note movstd: XREFmovstd, *note movvar: XREFmovvar.

 -- : Y = movvar (X, WLEN)
 -- : Y = movvar (X, [NB, NA])
 -- : Y = movvar (..., OPT)
 -- : Y = movvar (..., OPT, DIM)
 -- : Y = movvar (..., NANCOND)
 -- : Y = movvar (..., PROPERTY, VALUE)
     Calculate the moving variance over a sliding window of length WLEN
     on data X.

     The moving window length input WLEN can either be a numeric scalar
     or a 2-element numeric array ‘[NB, NA]’.  The elements included in
     the moving window depend on the size and value of WLEN as well as
     whether the "SamplePoints" option has been specified.  For full
     details of element inclusion, *note ‘movslice’: XREFmovslice.

     The optional argument OPT determines the type of normalization to
     use.  Valid values are:

     0:
          normalize with N-1, provides the best unbiased estimator of
          the variance [default]

     1:
          normalizes with N, this provides the second moment around the
          mean

     If the optional argument DIM is given, operate along this
     dimension.  The normalization argument OPT must be given before the
     dimension.  To use the default value for OPT you may pass an empty
     input argument ‘[]’.

     The optional argument NANCOND is a string that controls how ‘NaN’
     and ‘NA’ values affect the output of "movvar".  The value
     "includenan" (default) causes ‘NaN’ and ‘NA’ values to be included
     in the moving window, and any window slice containing ‘NaN’ or ‘NA’
     values will return ‘NaN’ for that element.  The value "omitnan"
     causes "movvar" to ignore any ‘NaN’ or ‘NA’ values resulting in
     fewer elements being used to calculate the variance for that window
     slice.  If "omitnan" is specified and a window slice contains all
     ‘NaN’ or ‘NA’ values, "movvar" returns ‘NaN’ for that element.  The
     values "includemissing" and "omitmissing" may be used synonymously
     with "includenan" and "omitnan", respectively.

     The calculation can be controlled by specifying PROPERTY/VALUE
     pairs.  Valid properties are "Endpoints" and "SamplePoints".  For
     full descriptions of these properties and valid options, *note
     ‘movfun’: XREFmovfun.

     Programming Note: This function is a wrapper which calls ‘movfun’.
     For full documentation of inputs and options, *note ‘movfun’:
     XREFmovfun.

     See also: *note var: XREFvar, *note movfun: XREFmovfun, *note
     movslice: XREFmovslice, *note movmad: XREFmovmad, *note movmax:
     XREFmovmax, *note movmean: XREFmovmean, *note movmedian:
     XREFmovmedian, *note movmin: XREFmovmin, *note movprod:
     XREFmovprod, *note movstd: XREFmovstd, *note movsum: XREFmovsum.


File: octave.info,  Node: Basic Statistical Functions,  Next: Forecasting Metrics,  Prev: Statistics on Sliding Windows of Data,  Up: Statistics

26.3 Basic Statistical Functions
================================

Octave supports various helpful statistical functions.  Many are useful
as initial steps to prepare a data set for further analysis.  Others
provide different measures from those of the basic descriptive
statistics.

 -- : Y = center (X)
 -- : Y = center (X, DIM)
 -- : Y = center (X, VECDIM)
 -- : Y = center (X, "all")
 -- : Y = center (..., NANFLAG)
     Center data by subtracting its mean.

     If X is a vector, then ‘center (X)’ computes the centered data by
     subtracting the mean of X from each element of X.

     If X is a matrix, then ‘center (X)’ returns a row vector with each
     element containing the centered data for each column of X.

     If X is an array, then ‘center (X)’ centers the data alonng the
     first non-singleton dimension of X.

     The data in X must be numeric.  The size of Y is equal to the size
     of X.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension of
     X, including any dimension exceeding ‘ndims (X)’, will return ‘X’.

     Specifying multiple dimensions with input VECDIM, a vector of
     non-repeating dimensions, will operate along the array slice
     defined by VECDIM.  If VECDIM indexes all dimensions of X, then it
     is equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (X)’ is ignored.

     Specifying the dimension as "all" will cause ‘center’ to compute
     the center of all elements of X, and is equivalent to ‘center
     (X(:))’.

     The optional variable NANFLAG specifies whether to include or
     exclude NaN values from the calculation of the mean using any of
     the previously specified input argument combinations.  The default
     value for NANFLAG is "includenan" which keeps NaN values in the
     calculation.  To exclude NaN values set the value of NANFLAG to
     "omitnan".  Any NaN value along the operating dimensions will
     result in all corresponding element in Y being NaN.

     Programming Note: ‘center’ has obvious application for normalizing
     statistical data.  It is also useful for improving the precision of
     general numerical calculations.  Whenever there is a large value
     that is common to a batch of data, the mean can be subtracted off,
     the calculation performed, and then the mean added back to obtain
     the final answer.

     See also: *note zscore: XREFzscore.

 -- : Z = zscore (X)
 -- : Z = zscore (X, OPT)
 -- : Z = zscore (X, OPT, DIM)
 -- : Z = zscore (X, OPT, VECDIM)
 -- : Z = zscore (X, OPT, "all")
 -- : Z = zscore (..., NANFLAG)
 -- : [Z, MU, SIGMA] = zscore (...)
     Compute the z-score of X.

     For a vector X, the z-score is calculated by subtracting the mean
     and dividing by its standard deviation.  If the standard deviation
     is zero, then divide by 1 instead.

     If X is a vector, then ‘zscore (X)’ returns the z-score of the
     elements in X.

     If X is a matrix, then ‘zscore (X)’ returns a row vector with each
     element containing the z-score of the corresponding column in X.

     If X is an array, then ‘zscore (X)’ computes the z-score along the
     first non-singleton dimension of X.

     The optional parameter OPT determines the normalization to use when
     computing the standard deviation and has the same definition as the
     corresponding parameter for ‘std’.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension of
     X, including any dimension exceeding ‘ndims (X)’, will return
     ‘zeros (size (X))’.

     Specifying multiple dimensions with input VECDIM, a vector of
     non-repeating dimensions, will operate along the array slice
     defined by VECDIM.  If VECDIM indexes all dimensions of X, then it
     is equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (X)’ is ignored.

     Specifying the dimension as "all" will cause ‘zscore’ to operate on
     all elements of X, and is equivalent to ‘zscore (X(:))’.

     The optional variable NANFLAG specifies whether to include or
     exclude ‘NaN’ values from the calculation using any of the
     previously specified input argument combinations.  The default
     value for NANFLAG is "includenan" which keeps ‘NaN’ values in the
     calculation.  To exclude ‘NaN’ values, set the value of NANFLAG to
     "omitnan".  The output will still contain ‘NaN’ values at the same
     locations as in X.

     The optional outputs MU and SIGMA contain the mean and standard
     deviation.

     See also: *note mean: XREFmean, *note std: XREFstd, *note center:
     XREFcenter.

 -- : Z = normalize (X)
 -- : Z = normalize (X, DIM)
 -- : Z = normalize (..., METHOD)
 -- : Z = normalize (..., METHOD, OPTION)
 -- : Z = normalize (..., SCALE, SCALEOPTION, CENTER, CENTEROPTION)
 -- : [Z, C, S] = normalize (...)
     Return a normalization of the data in X using one of several
     available scaling and centering methods.

     ‘normalize’ by default will return the ‘zscore’ of X, defined as
     the number of standard deviations each element is from the mean of
     X.  This is equivalent to centering at the mean of the data and
     scaling by the standard deviation.  X must be a numeric array of
     double or single floating point numbers.

     The returned value Z will have the same size as X.  The optional
     return variables C and S are the centering and scaling factors used
     in the normalization such that:

            Z = (X - C) ./ S

     If X is a vector, ‘normalize’ will operate on the data in X.

     If X is a matrix, ‘normalize’ will operate independently on each
     column in X.

     If X is an N-dimensional array, ‘normalize’ will operate
     independently on the first non-singleton dimension in X.

     If the optional second argument DIM is given, operate along this
     dimension.

     ‘normalize’ ignores NaN values is X similar to the behavior of the
     omitnan option in ‘std’, ‘mean’, and ‘median’.

     The optional inputs METHOD and OPTION can be used to specify the
     type of normalization performed on X.  Note that only the ‘scale’
     and ‘center’ options may be specified together using any of the
     methods defined below.  Valid normalization methods are:

     ‘zscore’
          (Default) Normalizes the elements in X to the scaled distance
          from a central value.  Valid Options:

          ‘std’
               (Default) Data is centered at ‘mean (X)’ and scaled by
               the standard deviation.

          ‘robust’
               Data is centered at ‘median (X)’ and scaled by the median
               absolute deviation.

     ‘norm’
          Z is the general vector norm of X, with OPTION being the
          normalization factor P that determines the vector norm type
          according to:

                 Z = [sum (abs (X) .^ P)] ^ (1/P)

          P can be any positive scalar, specific values being:

          ‘P = 1’
               X is normalized by ‘sum (abs (X))’.

          ‘P = 2’
               (Default) X is normalized by the Euclidian norm, or
               vector magnitude, of the elements.

          ‘P = Inf’
               X is normalized by ‘max (abs (X))’.

     ‘scale’
          X is scaled by a factor determined by OPTION, which can be a
          numeric scalar or one of the following:

          ‘std’
               (Default) X is scaled by its standard deviation.

          ‘mad’
               X is scaled by its median absolute deviation.

          ‘first’
               X is scaled by its first element.

          ‘iqr’
               X is scaled by its interquartile range.

     ‘range’
          X is scaled to fit the range specified by OPTION as a two
          element scalar row vector.  The default range is [0, 1].

     ‘center’
          X is shifted by an amount determined by OPTION, which can be a
          numeric scalar or one of the following:

          ‘mean’
               (Default) X is shifted by ‘mean (X)’.

          ‘median’
               X is shifted by ‘median (X)’.

     ‘medianiqr’
          X is shifted by ‘median (X)’ and scaled by the interquartile
          range.

     Known MATLAB incompatibilities:

       1. The option ‘DataVariables’ is only available when input X is a
          table class, which is not yet implemented in core Octave.  See
          the datatypes and tablicious Octave Packages for an available
          overloaded method.

     See also: *note zscore: XREFzscore, *note iqr: XREFiqr, *note norm:
     XREFnorm, *note rescale: XREFrescale, *note std: XREFstd, *note
     median: XREFmedian, *note mean: XREFmean, *note mad: XREFmad.

 -- : N = histc (X, EDGES)
 -- : N = histc (X, EDGES, DIM)
 -- : [N, IDX] = histc (...)
     Compute histogram counts.

     When X is a vector, the function counts the number of elements of X
     that fall in the histogram bins defined by EDGES.  This must be a
     vector of monotonically increasing values that define the edges of
     the histogram bins.  ‘N(k)’ contains the number of elements in X
     for which ‘EDGES(k) <= X < EDGES(k+1)’.  The final element of N
     contains the number of elements of X exactly equal to the last
     element of EDGES.

     When X is an N-dimensional array, the computation is carried out
     along dimension DIM.  If not specified DIM defaults to the first
     non-singleton dimension.

     When a second output argument is requested an index matrix is also
     returned.  The IDX matrix has the same size as X.  Each element of
     IDX contains the index of the histogram bin in which the
     corresponding element of X was counted.

     See also: *note hist: XREFhist.

‘unique’ function documented at *note unique: XREFunique. is often
useful for statistics.

 -- : C = nchoosek (N, K)
 -- : C = nchoosek (SET, K)

     Compute the binomial coefficient of N or list all possible
     combinations of a SET of items.

     If N is a scalar then calculate the binomial coefficient of N and K
     which is defined as

           /   \
           | n |    n (n-1) (n-2) ... (n-k+1)       n!
           |   |  = ------------------------- =  ---------
           | k |               k!                k! (n-k)!
           \   /

     This is the number of combinations of N items taken in groups of
     size K.

     If the first argument is a vector, SET, then generate all
     combinations of the elements of SET, taken K at a time, with one
     row per combination.  The result C has K columns and
     ‘nchoosek (length (SET), K)’ rows.

     For example:

     How many ways can three items be grouped into pairs?

          nchoosek (3, 2)
             ⇒ 3

     What are the possible pairs?

          nchoosek (1:3, 2)
             ⇒  1   2
                 1   3
                 2   3

     Programming Note: When calculating the binomial coefficient
     ‘nchoosek’ works only for non-negative, integer arguments.  Use
     ‘bincoeff’ for non-integer and negative scalar arguments, or for
     computing many binomial coefficients at once with vector inputs for
     N or K.

     See also: *note bincoeff: XREFbincoeff, *note perms: XREFperms.

 -- : P = perms (V)
 -- : P = perms (V, "unique")
     Generate all permutations of vector V with one row per permutation.

     Results are returned in reverse lexicographic order if V is in
     ascending order.  If V is in a different permutation, then the
     result is permuted that way too.  Consequently, an input in
     descending order yields a result in normal lexicographic order.
     The result has size ‘factorial (N) * N’, where N is the length of
     V.  Any repeated elements are included in the output.

     If the optional argument "unique" is given then only unique
     permutations are returned, using less memory and taking less time
     than calling ‘unique (perms (V), "rows")’.

     Example 1

          perms ([1, 2, 3])
          ⇒
          3   2   1
          3   1   2
          2   3   1
          2   1   3
          1   3   2
          1   2   3

     Example 2

          perms ([1, 1, 2, 2], "unique")
          ⇒
          2   2   1   1
          2   1   2   1
          2   1   1   2
          1   2   2   1
          1   2   1   2
          1   1   2   2

     Programming Note: If the "unique" option is not used, the length of
     V should be no more than 10-12 to limit memory consumption.  Even
     with "unique", there should be no more than 10-12 unique elements
     in V.

     See also: *note permute: XREFpermute, *note randperm: XREFrandperm,
     *note nchoosek: XREFnchoosek.

 -- : Y = ranks (X)
 -- : Y = ranks (X, DIM)
 -- : Y = ranks (X, DIM, RTYPE)
     Return the ranks (in the sense of order statistics) of X along the
     first non-singleton dimension adjusted for ties.

     If the optional DIM argument is given, operate along this
     dimension.

     The optional parameter RTYPE determines how ties are handled.  All
     examples below assume an input of ‘[ 1, 2, 2, 4 ]’.

     0 or "fractional" (default) for fractional ranking (1, 2.5,
          2.5, 4);

     1 or "competition" for competition ranking (1, 2, 2, 4);

     2 or "modified" for modified competition ranking (1, 3, 3, 4);

     3 or "ordinal" for ordinal ranking (1, 2, 3, 4);

     4 or "dense" for dense ranking (1, 2, 2, 3).

     See also: *note spearman: XREFspearman, *note kendall: XREFkendall.

 -- : CNT = run_count (X, N)
 -- : CNT = run_count (X, N, DIM)
     Count the upward runs along the first non-singleton dimension of X
     of length 1, 2, ..., N-1 and greater than or equal to N.

     If the optional argument DIM is given then operate along this
     dimension.

     See also: *note runlength: XREFrunlength.

 -- : count = runlength (X)
 -- : [count, value] = runlength (X)
     Find the lengths of all sequences of common values.

     COUNT is a vector with the lengths of each repeated value.

     The optional output VALUE contains the value that was repeated in
     the sequence.

          runlength ([2, 2, 0, 4, 4, 4, 0, 1, 1, 1, 1])
          ⇒   2   1   3   1   4

     See also: *note run_count: XREFrun_count.


File: octave.info,  Node: Forecasting Metrics,  Next: Correlation and Regression Analysis,  Prev: Basic Statistical Functions,  Up: Statistics

26.4 Forecasting Metrics
========================

Octave supports the following functions for calculating forecasting
metrics.

 -- : E = mape (F, A)
 -- : E = mape (F, A, DIM)
 -- : E = mape (F, A, VECDIM)
 -- : E = mape (F, A, "all")
 -- : E = mape (..., NANFLAG)
 -- : E = mape (..., ZEROFLAG)
 -- : E = mape (..., 'Weights', W)
     Compute the mean absolute percentage error between arrays.

     The mean absolute percentage error is defined as

          mape (F, A) = SUM_i (abs ((A(i) - F(i)) / A(i))) * 100 / N

     where N is the number of elements in F and A after broadcasting is
     applied to the subtraction operation.

     The weighted mean absolute percentage error is defined as

          weighted_mape (F, A) = SUM_i (W(i) * (abs ((A(i) - F(i)) / A(i)))) * 100 / SUM_i (W(i)

     where N is the number of elements in F and A after broadcasting is
     applied to the subtraction operation.

     F and A must either be the same size or have compatible sizes.

     If F and A are vectors of the same size, then ‘mape (F, A)’ returns
     a scalar with the MAPE between the elements of F and A.

     If ‘A - F’ is a matrix, then ‘mape (F, A)’ returns a row vector
     with each element containing the MAPE between the corresponding
     columns of ‘A - F’.

     If ‘A - F’ is an array, then ‘mape (F, A)’ computes the MAPE along
     the first non-singleton dimension of the difference between the
     input arrays F and A.  The size of E along the operating dimension
     is 1, while all other dimensions are the same as in ‘A - F’.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension,
     including any dimension exceeding ‘ndims (A - F)’, will return ‘abs
     ((A - F) ./ A)’.

     Specifying the dimensions as VECDIM, a vector of non-repeating
     dimensions, will return the mape over the array slice defined by
     VECDIM.  If VECDIM indexes all dimensions in ‘A - F’, then it is
     equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (A - F)’ is ignored.

     Specifying the dimension as "all" will cause ‘mape’ to operate on
     all elements of ‘A - F’, and is equivalent to ‘mape ((A - F)(:))’.

     The optional variable NANFLAG specifies whether to include or
     exclude NaN values from the calculation using any of the previously
     specified input argument combinations.  The default value for
     NANFLAG is "includenan" which keeps NaN values in the calculation.
     To exclude NaN values set the value of NANFLAG to "omitnan".  The
     output will still contain NaN values if ‘A - F’ consists of all NaN
     values in the operating dimension.

     The optional variable ZEROFLAG specifies whether to include or omit
     zero values from the calculation using any of the previously
     specified input argument combinations.  The default value for
     ZEROFLAG is "includezero", in which case the calculated MAPE is
     Inf, if A contains one or more zeros.  To ignore any zero values in
     A, set the value of ZEROFLAG to "omitzero".  If A consists of all
     zero values in the operating dimension, then MAPE is NaN.

     The optional paired argument ‘..., "Weights", W’ specifies a
     weighting scheme W, which is applied on the difference of the input
     arrays F and A, so that ‘mape’ computes the weighted MAPE. When
     operating along a single dimension, W must be a vector of the same
     length as the operating dimension or it must have the same size as
     X.  When operating over an array slice defined by VECDIM, W must
     have the same size as the operating array slice, i.e.  ‘size (A -
     F)(VECDIM)’, or the same size as ‘A - F’.

     See also: *note rmse: XREFrmse, *note mean: XREFmean, *note abs:
     XREFabs.

 -- : E = rmse (F, A)
 -- : E = rmse (F, A, DIM)
 -- : E = rmse (F, A, VECDIM)
 -- : E = rmse (F, A, "all")
 -- : E = rmse (..., NANFLAG)
 -- : E = rmse (..., 'Weights', W)
     Compute the root mean squared error between arrays.

     The root mean squared error is defined as

          rmse (F, A) = sqrt (SUM_i ((A(i) - F(i)) ^ 2) / N)

     where N is the number of elements in F and A after broadcasting is
     applied to the subtraction operation.

     The weighted root mean squared error is defined as

          weighted_rmse (F, A) = sqrt (SUM_i (W(i) * ((A(i) - F(i)) ^ 2)) / SUM_i (W(i)))

     where N is the number of elements in F and A after broadcasting is
     applied to the subtraction operation.

     F and A must either be the same size or have compatible sizes.

     If F and A are vectors of the same size, then ‘rmse (F, A)’ returns
     a scalar with the RMSE between the elements of F and A.

     If ‘A - F’ is a matrix, then ‘rmse (F, A)’ returns a row vector
     with each element containing the RMSE between the corresponding
     columns of ‘A - F’.

     If ‘A - F’ is an array, then ‘rmse (F, A)’ computes the RMSE along
     the first non-singleton dimension of the difference between the
     input arrays F and A.  The size of E along the operating dimension
     is 1, while all other dimensions are the same as in ‘A - F’.

     The optional input DIM specifies the dimension to operate on and
     must be a positive integer.  Specifying any singleton dimension,
     including any dimension exceeding ‘ndims (A - F)’, will return ‘abs
     ((A - F) ./ A)’.

     Specifying the dimensions as VECDIM, a vector of non-repeating
     dimensions, will return the rmse over the array slice defined by
     VECDIM.  If VECDIM indexes all dimensions in ‘A - F’, then it is
     equivalent to the option "all".  Any dimension in VECDIM greater
     than ‘ndims (A - F)’ is ignored.

     Specifying the dimension as "all" will cause ‘rmse’ to operate on
     all elements of ‘A - F’, and is equivalent to ‘rmse ((A - F)(:))’.

     The optional variable NANFLAG specifies whether to include or
     exclude NaN values from the calculation using any of the previously
     specified input argument combinations.  The default value for
     NANFLAG is "includenan" which keeps NaN values in the calculation.
     To exclude NaN values set the value of NANFLAG to "omitnan".  The
     output will still contain NaN values if ‘A - F’ consists of all NaN
     values in the operating dimension.

     The optional paired argument ‘..., "Weights", W’ specifies a
     weighting scheme W, which is applied on the difference of the input
     arrays F and A, so that ‘rmse’ computes the weighted RMSE. When
     operating along a single dimension, W must be a vector of the same
     length as the operating dimension or it must have the same size as
     X.  When operating over an array slice defined by VECDIM, W must
     have the same size as the operating array slice, i.e.  ‘size (A -
     F)(VECDIM)’, or the same size as ‘A - F’.

     See also: *note mape: XREFmape, *note meansq: XREFmeansq, *note
     rms: XREFrms.

