Go to the first, previous, next, last section, table of contents.

# Quadrature

## Functions of One Variable

Loadable Function: [v, ier, nfun, err] = quad (f, a, b, tol, sing)
  Integrate a nonlinear function of one variable using Quadpack. The first argument is the name of the function to call to compute the value of the integrand. It must have the form

      y = f (x)

  where y and x are scalars.

  The second and third arguments are 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.

  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 v and ier contains an integer error code (0 indicates a successful integration). The value of nfun indicates how many function evaluations were required, and err contains an estimate of the error in the solution.

Loadable Function: quad_options (opt, val)
  When called with two arguments, this function allows you set options parameters for the function `quad`. Given one argument, `quad_options` returns the value of the corresponding option. If no arguments are supplied, the names of all the available options and their current values are displayed.

Here is an example of using `quad` to integrate the function

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 call to `quad`, but it makes it much easier to generate a set of points for plotting (because it makes it possible to call the function with a vector argument to produce a vector result).

Then we simply call quad:

    [v, 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.).

## Orthogonal Collocation

Loadable Function: [r, A, B, q] = colloc (n, "left", "right")
  Compute derivative and integral weight matrices for orthogonal collocation using the subroutines given in J. Villadsen and 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

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

    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 ]

Go to the first, previous, next, last section, table of contents.