Mercurial > hg > octave-lyh
annotate scripts/general/del2.m @ 11188:4cb1522e4d0f
Use function handle as input to cellfun,
rather than quoted function name or anonymous function wrapper.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 03 Nov 2010 17:20:56 -0700 |
parents | 693e22af08ae |
children | c776f063fefe |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2007, 2008, 2009 Kai Habel |
6788 | 2 ## Copyright (C) 2007 David Bateman |
3 ## | |
4 ## This file is part of Octave. | |
5 ## | |
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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
6788 | 10 ## |
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. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
6788 | 19 |
20 ## -*- texinfo -*- | |
9141
c1fff751b5a8
Update section 17.1 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
21 ## @deftypefn {Function File} {@var{d} =} del2 (@var{m}) |
6788 | 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{}) | |
24 ## | |
9165
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9141
diff
changeset
|
25 ## Calculate the discrete Laplace |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9141
diff
changeset
|
26 ## @tex |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9141
diff
changeset
|
27 ## operator $( \nabla^2 )$. |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9141
diff
changeset
|
28 ## @end tex |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9141
diff
changeset
|
29 ## @ifnottex |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9141
diff
changeset
|
30 ## operator. |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9141
diff
changeset
|
31 ## @end ifnottex |
9167
1231b1762a9a
Simplify TeXinfo and eliminate use of @iftex in arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9165
diff
changeset
|
32 ## For a 2-dimensional matrix @var{m} this is defined as |
6788 | 33 ## @tex |
8499 | 34 ## $$d = {1 \over 4} \left( {d^2 \over dx^2} M(x,y) + {d^2 \over dy^2} M(x,y) \right)$$ |
6788 | 35 ## @end tex |
36 ## @ifnottex | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
37 ## |
6788 | 38 ## @example |
39 ## @group | |
40 ## 1 / d^2 d^2 \ | |
41 ## D = --- * | --- M(x,y) + --- M(x,y) | | |
42 ## 4 \ dx^2 dy^2 / | |
43 ## @end group | |
44 ## @end example | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
45 ## |
6788 | 46 ## @end ifnottex |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
47 ## For N-dimensional arrays the sum in parentheses is expanded to include second |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
48 ## derivatives over the additional higher dimensions. |
6788 | 49 ## |
50 ## The spacing between evaluation points may be defined by @var{h}, which is a | |
9141
c1fff751b5a8
Update section 17.1 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
51 ## scalar defining the equidistant spacing in all dimensions. Alternatively, |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
52 ## the spacing in each dimension may be defined separately by @var{dx}, |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
53 ## @var{dy}, etc. A scalar spacing argument defines equidistant spacing, |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
54 ## whereas a vector argument can be used to specify variable spacing. The |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
55 ## length of the spacing vectors must match the respective dimension of |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
56 ## @var{m}. The default spacing value is 1. |
6788 | 57 ## |
9141
c1fff751b5a8
Update section 17.1 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
58 ## At least 3 data points are needed for each dimension. Boundary points are |
c1fff751b5a8
Update section 17.1 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
59 ## calculated from the linear extrapolation of interior points. |
6788 | 60 ## |
61 ## @seealso{gradient, diff} | |
62 ## @end deftypefn | |
63 | |
64 ## Author: Kai Habel <kai.habel@gmx.de> | |
65 | |
66 function D = del2 (M, varargin) | |
67 | |
68 if (nargin < 1) | |
69 print_usage (); | |
70 endif | |
71 | |
72 nd = ndims (M); | |
73 sz = size (M); | |
74 dx = cell (1, nd); | |
75 if (nargin == 2 || nargin == 1) | |
76 if (nargin == 1) | |
77 h = 1; | |
78 else | |
7669 | 79 h = varargin{1}; |
6788 | 80 endif |
81 for i = 1 : nd | |
82 if (isscalar (h)) | |
10549 | 83 dx{i} = h * ones (sz (i), 1); |
6788 | 84 else |
10549 | 85 if (length (h) == sz (i)) |
86 dx{i} = diff (h)(:); | |
87 else | |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
88 error ("del2: dimensionality mismatch in %d-th spacing vector", i); |
10549 | 89 endif |
6788 | 90 endif |
91 endfor | |
92 elseif (nargin - 1 == nd) | |
93 ## Reverse dx{1} and dx{2} as the X-dim is the 2nd dim of the ND array | |
94 tmp = varargin{1}; | |
95 varargin{1} = varargin{2}; | |
96 varargin{2} = tmp; | |
97 | |
98 for i = 1 : nd | |
99 if (isscalar (varargin{i})) | |
10549 | 100 dx{i} = varargin{i} * ones (sz (i), 1); |
6788 | 101 else |
10549 | 102 if (length (varargin{i}) == sz (i)) |
103 dx{i} = diff (varargin{i})(:); | |
104 else | |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
105 error ("del2: dimensionality mismatch in %d-th spacing vector", i); |
10549 | 106 endif |
6788 | 107 endif |
108 endfor | |
109 else | |
110 print_usage (); | |
111 endif | |
112 | |
113 idx = cell (1, nd); | |
114 for i = 1: nd | |
115 idx{i} = ":"; | |
116 endfor | |
117 | |
118 D = zeros (sz); | |
119 for i = 1: nd | |
120 if (sz(i) >= 3) | |
121 DD = zeros (sz); | |
122 idx1 = idx2 = idx3 = idx; | |
123 | |
124 ## interior points | |
125 idx1{i} = 1 : sz(i) - 2; | |
126 idx2{i} = 2 : sz(i) - 1; | |
127 idx3{i} = 3 : sz(i); | |
128 szi = sz; | |
129 szi (i) = 1; | |
130 | |
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 + ... | |
10549 | 134 (M(idx3{:}) - M(idx2{:})) ./ h2) ./ (h1 + h2); |
6788 | 135 |
136 ## left and right boundary | |
137 if (sz(i) == 3) | |
10549 | 138 DD(idx1{:}) = DD(idx3{:}) = DD(idx2{:}); |
6788 | 139 else |
10549 | 140 idx1{i} = 1; |
141 idx2{i} = 2; | |
142 idx3{i} = 3; | |
143 DD(idx1{:}) = (dx{i}(1) + dx{i}(2)) / dx{i}(2) * DD (idx2{:}) - ... | |
144 dx{i}(1) / dx{i}(2) * DD (idx3{:}); | |
6788 | 145 |
10549 | 146 idx1{i} = sz(i); |
147 idx2{i} = sz(i) - 1; | |
148 idx3{i} = sz(i) - 2; | |
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{:}); | |
6788 | 152 endif |
153 | |
154 D += DD; | |
155 endif | |
156 endfor | |
157 | |
158 D = D ./ nd; | |
159 endfunction |