GNU Octave Manual Version 3by John W. Eaton, David Bateman, Søren Hauberg Paperback (6"x9"), 568 pages ISBN 095461206X RRP £24.95 ($39.95) |

## 21.3 Functions of Multiple Variables

Octave does not have built-in functions for computing the integral of functions of multiple variables. It is however possible to compute the integral of a function of multiple variables using the functions for one-dimensional integrals.

To illustrate how the integration can be performed, we will integrate the function

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

for x and y between 0 and 1.

The first approach creates a function that integrates f with
respect to x, and then integrates that function with respect to
y. Since `quad`

is written in Fortran it cannot be called
recursively. This means that `quad`

cannot integrate a function
that calls `quad`

, and hence cannot be used to perform the double
integration. It is however possible with `quadl`

, which is what
the following code does.

function I = g(y) I = ones(1, length(y)); for i = 1:length(y) f = @(x) sin(pi.*x.*y(i)).*sqrt(x.*y(i)); I(i) = quadl(f, 0, 1); endfor endfunction I = quadl("g", 0, 1) => 0.30022

The above mentioned approach works but is fairly slow, and that problem
increases exponentially with the dimensionality the problem. Another
possible solution is to use Orthogonal Collocation as described in the
previous section. 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
generalisation to more than two variables is straight forward. The
following code computes the studied integral using n=7 points.

f = @(x,y) sin(pi*x*y').*sqrt(x*y'); n = 7; [t, A, B, 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, a change of variables is needed.

ISBN 095461206X | GNU Octave Manual Version 3 | See the print edition |