1 ## Copyright (C) 2000-2012 Kai Habel
2 ## Copyright (C) 2007 David Bateman
4 ## This file is part of Octave.
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
21 ## @deftypefn {Function File} {@var{d} =} del2 (@var{M})
22 ## @deftypefnx {Function File} {@var{d} =} del2 (@var{M}, @var{h})
23 ## @deftypefnx {Function File} {@var{d} =} del2 (@var{M}, @var{dx}, @var{dy}, @dots{})
25 ## Calculate the discrete Laplace
27 ## operator $( \nabla^2 )$.
32 ## For a 2-dimensional matrix @var{M} this is defined as
34 ## $$d = {1 \over 4} \left( {d^2 \over dx^2} M(x,y) + {d^2 \over dy^2} M(x,y) \right)$$
41 ## D = --- * | --- M(x,y) + --- M(x,y) |
47 ## For N-dimensional arrays the sum in parentheses is expanded to include second
48 ## derivatives over the additional higher dimensions.
50 ## The spacing between evaluation points may be defined by @var{h}, which is a
51 ## scalar defining the equidistant spacing in all dimensions. Alternatively,
52 ## the spacing in each dimension may be defined separately by @var{dx},
53 ## @var{dy}, etc. A scalar spacing argument defines equidistant spacing,
54 ## whereas a vector argument can be used to specify variable spacing. The
55 ## length of the spacing vectors must match the respective dimension of
56 ## @var{M}. The default spacing value is 1.
58 ## At least 3 data points are needed for each dimension. Boundary points are
59 ## calculated from the linear extrapolation of interior points.
61 ## @seealso{gradient, diff}
64 ## Author: Kai Habel <kai.habel@gmx.de>
66 function D = del2 (M, varargin)
75 if (nargin == 2 || nargin == 1)
83 dx{i} = h * ones (sz (i), 1);
85 if (length (h) == sz (i))
88 error ("del2: dimensionality mismatch in %d-th spacing vector", i);
92 elseif (nargin - 1 == nd)
93 ## Reverse dx{1} and dx{2} as the X-dim is the 2nd dim of the ND array
95 varargin{1} = varargin{2};
99 if (isscalar (varargin{i}))
100 dx{i} = varargin{i} * ones (sz (i), 1);
102 if (length (varargin{i}) == sz (i))
103 dx{i} = diff (varargin{i})(:);
105 error ("del2: dimensionality mismatch in %d-th spacing vector", i);
122 idx1 = idx2 = idx3 = idx;
125 idx1{i} = 1 : sz(i) - 2;
126 idx2{i} = 2 : sz(i) - 1;
131 h1 = repmat (shiftdim (dx{i}(1 : sz(i) - 2), 1 - i), szi);
132 h2 = repmat (shiftdim (dx{i}(2 : sz(i) - 1), 1 - i), szi);
133 DD(idx2{:}) = ((M(idx1{:}) - M(idx2{:})) ./ h1 + ...
134 (M(idx3{:}) - M(idx2{:})) ./ h2) ./ (h1 + h2);
136 ## left and right boundary
138 DD(idx1{:}) = DD(idx3{:}) = DD(idx2{:});
143 DD(idx1{:}) = (dx{i}(1) + dx{i}(2)) / dx{i}(2) * DD (idx2{:}) - ...
144 dx{i}(1) / dx{i}(2) * DD (idx3{:});
149 DD(idx1{:}) = (dx{i}(sz(i) - 1) + dx{i}(sz(i) - 2)) / ...
150 dx{i}(sz(i) - 2) * DD (idx2{:}) - ...
151 dx{i}(sz(i) - 1) / dx{i}(sz(i) - 2) * DD (idx3{:});