changelog shortlog tags changeset files revisions annotate raw

scripts/general/dblquad.m

changeset 10289: 4b124317dc38
parent:eb63fbe60fab
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (44 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 2008, 2009 David Bateman
2##
3## This file is part of Octave.
4##
5## Octave is free software; you can redistribute it and/or modify it
6## under the terms of the GNU General Public License as published by
7## the Free Software Foundation; either version 3 of the License, or (at
8## your option) any later version.
9##
10## Octave is distributed in the hope that it will be useful, but
11## WITHOUT ANY WARRANTY; without even the implied warranty of
12## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13## General Public License for more details.
14##
15## You should have received a copy of the GNU General Public License
16## along with Octave; see the file COPYING. If not, see
17## <http://www.gnu.org/licenses/>.
18
19## -*- texinfo -*-
20## @deftypefn {Function File} {} dblquad (@var{f}, @var{xa}, @var{xb}, @var{ya}, @var{yb}, @var{tol}, @var{quadf}, @dots{})
21## Numerically evaluate a double integral. The function over with to
22## integrate is defined by @code{@var{f}}, and the interval for the
23## integration is defined by @code{[@var{xa}, @var{xb}, @var{ya},
24## @var{yb}]}. The function @var{f} must accept a vector @var{x} and a
25## scalar @var{y}, and return a vector of the same length as @var{x}.
26##
27## If defined, @var{tol} defines the absolute tolerance to which to
28## which to integrate each sub-integral.
29##
30## Additional arguments, are passed directly to @var{f}. To use the default
31## value for @var{tol} one may pass an empty matrix.
32## @seealso{triplequad, quad, quadv, quadl, quadgk, trapz}
33## @end deftypefn
34
35function q = dblquad(f, xa, xb, ya, yb, tol, quadf, varargin)
36 if (nargin < 5)
37 print_usage ();
38 endif
39 if (nargin < 6 || isempty (tol))
40 tol = 1e-6;
41 endif
42 if (nargin < 7 || isempty (quadf))
43 quadf = @quadgk;
44 endif
45
46 inner = @__dblquad_inner__;
47 if (ischar (f))
48 f = @(x,y) feval (f, x, y, varargin{:});
49 varargin = {};
50 endif
51
52 q = feval (quadf, @(y) inner (y, f, xa, xb, tol, quadf,
53 varargin{:}), ya, yb, tol);
54endfunction
55
56function q = __dblquad_inner__ (y, f, xa, xb, tol, quadf, varargin)
57 q = zeros (size(y));
58 for i = 1 : length (y)
59 q(i) = feval (quadf, @(x) f(x, y(i), varargin{:}), xa, xb, tol);
60 endfor
61endfunction
62
63%% Nasty integrand to show quadgk off
64%!assert (dblquad (@(x,y) 1 ./ (x+y), 0, 1, 0, 1), 2*log(2), 1e-6)
65
66%!assert (dblquad (@(x,y) exp(-x.^2 - y.^2) , -1, 1, -1, 1, [], @quadgk), pi * erf(1).^2, 1e-6)
67%!assert (dblquad (@(x,y) exp(-x.^2 - y.^2) , -1, 1, -1, 1, [], @quadl), pi * erf(1).^2, 1e-6)
68%!assert (dblquad (@(x,y) exp(-x.^2 - y.^2) , -1, 1, -1, 1, [], @quadv), pi * erf(1).^2, 1e-6)