annotate scripts/general/gradient.m @ 10635:d1978e7364ad

Print name of function in error() string messages.
author Rik <octave@nomad.inbox5.com>
date Sun, 16 May 2010 22:26:54 -0700
parents 95c3e38098bf
children be55736a0783
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8601
diff changeset
1 ## Copyright (C) 2000, 2006, 2007, 2008, 2009 Kai Habel
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
2 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
4 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
8 ## your option) any later version.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
9 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
14 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
17 ## <http://www.gnu.org/licenses/>.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
18
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
20 ## @deftypefn {Function File} {@var{dx} =} gradient (@var{m})
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
21 ## @deftypefnx {Function File} {[@var{dx}, @var{dy}, @var{dz}, @dots{}] =} gradient (@var{m})
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
22 ## @deftypefnx {Function File} {[@dots{}] =} gradient (@var{m}, @var{s})
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
23 ## @deftypefnx {Function File} {[@dots{}] =} gradient (@var{m}, @var{x}, @var{y}, @var{z}, @dots{})
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
24 ## @deftypefnx {Function File} {[@dots{}] =} gradient (@var{f}, @var{x0})
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
25 ## @deftypefnx {Function File} {[@dots{}] =} gradient (@var{f}, @var{x0}, @var{s})
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
26 ## @deftypefnx {Function File} {[@dots{}] =} gradient (@var{f}, @var{x0}, @var{x}, @var{y}, @dots{})
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
27 ##
9165
8c71a86c4bf4 Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents: 9141
diff changeset
28 ## Calculate the gradient of sampled data or a function. If @var{m}
8c71a86c4bf4 Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents: 9141
diff changeset
29 ## is a vector, calculate the one-dimensional gradient of @var{m}. If
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
30 ## @var{m} is a matrix the gradient is calculated for each dimension.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
31 ##
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
32 ## @code{[@var{dx}, @var{dy}] = gradient (@var{m})} calculates the one
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
33 ## dimensional gradient for @var{x} and @var{y} direction if @var{m} is a
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 9015
diff changeset
34 ## matrix. Additional return arguments can be use for multi-dimensional
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
35 ## matrices.
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
36 ##
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
37 ## A constant spacing between two points can be provided by the
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 9015
diff changeset
38 ## @var{s} parameter. If @var{s} is a scalar, it is assumed to be the spacing
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
39 ## for all dimensions.
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
40 ## Otherwise, separate values of the spacing can be supplied by
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 9015
diff changeset
41 ## the @var{x}, @dots{} arguments. Scalar values specify an equidistant spacing.
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
42 ## Vector values for the @var{x}, @dots{} arguments specify the coordinate for that
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 9015
diff changeset
43 ## dimension. The length must match their respective dimension of @var{m}.
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
44 ##
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 9015
diff changeset
45 ## At boundary points a linear extrapolation is applied. Interior points
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
46 ## are calculated with the first approximation of the numerical gradient
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
47 ##
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
48 ## @example
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
49 ## y'(i) = 1/(x(i+1)-x(i-1)) * (y(i-1)-y(i+1)).
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
50 ## @end example
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
51 ##
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
52 ## If the first argument @var{f} is a function handle, the gradient of the
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
53 ## function at the points in @var{x0} is approximated using central
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
54 ## difference. For example, @code{gradient (@@cos, 0)} approximates the
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 9015
diff changeset
55 ## gradient of the cosine function in the point @math{x0 = 0}. As with
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
56 ## sampled data, the spacing values between the points from which the
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
57 ## gradient is estimated can be set via the @var{s} or @var{dx},
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 9015
diff changeset
58 ## @var{dy}, @dots{} arguments. By default a spacing of 1 is used.
9141
c1fff751b5a8 Update section 17.1 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents: 9051
diff changeset
59 ## @seealso{diff, del2}
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
60 ## @end deftypefn
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
61
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
62 ## Author: Kai Habel <kai.habel@gmx.de>
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
63 ## Modified: David Bateman <dbateman@free.fr> Added NDArray support
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
64
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
65 function varargout = gradient (m, varargin)
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
66
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
67 if (nargin < 1)
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
68 print_usage ()
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
69 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
70
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
71 nargout_with_ans = max(1,nargout);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
72 if (ismatrix (m))
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
73 [varargout{1:nargout_with_ans}] = matrix_gradient (m, varargin{:});
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
74 elseif (isa (m, "function_handle"))
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
75 [varargout{1:nargout_with_ans}] = handle_gradient (m, varargin{:});
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
76 elseif (ischar(m))
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
77 [varargout{1:nargout_with_ans}] = handle_gradient (str2func (m), varargin{:});
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
78 else
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
79 error ("gradient: first input must be an array or a function");
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
80 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
81
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
82 endfunction
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
83
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
84 function varargout = matrix_gradient (m, varargin)
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
85 transposed = false;
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
86 if (isvector (m))
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
87 ## make a row vector.
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
88 transposed = (size (m, 2) == 1);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
89 m = m(:)';
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
90 endif
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
91
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
92 nd = ndims (m);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
93 sz = size (m);
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
94 if (length(sz) > 1)
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
95 tmp = sz(1); sz(1) = sz(2); sz(2) = tmp;
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
96 endif
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
97
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
98 if (nargin > 2 && nargin != nd + 1)
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
99 print_usage ()
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
100 endif
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
101
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
102 ## cell d stores a spacing vector for each dimension
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
103 d = cell (1, nd);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
104 if (nargin == 1)
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
105 ## no spacing given - assume 1.0 for all dimensions
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7650
diff changeset
106 for i = 1:nd
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
107 d{i} = ones (sz(i) - 1, 1);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
108 endfor
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
109 elseif (nargin == 2)
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
110 if (isscalar (varargin{1}))
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
111 ## single scalar value for all dimensions
5838
376e02b2ce70 [project @ 2006-06-01 20:23:53 by jwe]
jwe
parents: 5837
diff changeset
112 for i = 1:nd
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
113 d{i} = varargin{1} * ones (sz(i) - 1, 1);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
114 endfor
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
115 else
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
116 ## vector for one-dimensional derivative
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
117 d{1} = diff (varargin{1}(:));
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
118 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
119 else
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
120 ## have spacing value for each dimension
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
121 if (length(varargin) != nd)
10635
d1978e7364ad Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
122 error ("gradient: dimensions and number of spacing values do not match.");
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
123 end
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7650
diff changeset
124 for i = 1:nd
7120
a2174fb073d4 [project @ 2007-11-07 21:26:43 by jwe]
jwe
parents: 7017
diff changeset
125 if (isscalar (varargin{i}))
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
126 d{i} = varargin{i} * ones (sz(i) - 1, 1);
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
127 else
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
128 d{i} = diff (varargin{i}(:));
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
129 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
130 endfor
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
131 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
132
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
133 m = shiftdim (m, 1);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
134 for i = 1:min (nd, nargout)
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
135 mr = rows (m);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
136 mc = numel (m) / mr;
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
137 Y = zeros (size (m), class (m));
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9165
diff changeset
138
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
139 if (mr > 1)
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7650
diff changeset
140 ## Top and bottom boundary.
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
141 Y(1,:) = diff (m(1:2, :)) / d{i}(1);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
142 Y(mr,:) = diff (m(mr-1:mr, :) / d{i}(mr - 1));
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
143 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
144
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
145 if (mr > 2)
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7650
diff changeset
146 ## Interior points.
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
147 Y(2:mr-1,:) = ((m(3:mr,:) - m(1:mr-2,:))
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
148 ./ kron (d{i}(1:mr-2) + d{i}(2:mr-1), ones (1, mc)));
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
149 endif
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
150
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
151 ## turn multi-dimensional matrix in a way, that gradient
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
152 ## along x-direction is calculated first then y, z, ...
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
153
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
154 if (i == 1)
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
155 varargout{i} = shiftdim (Y, nd - 1);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
156 m = shiftdim (m, nd - 1);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
157 elseif (i == 2)
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
158 varargout{i} = Y;
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
159 m = shiftdim (m, 2);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
160 else
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
161 varargout{i} = shiftdim (Y, nd - i + 1);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
162 m = shiftdim (m, 1);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
163 endif
5837
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
164 endfor
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
165
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
166 if (transposed)
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
167 varargout{1} = varargout{1}.';
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
168 endif
55404f3b0da1 [project @ 2006-06-01 19:05:31 by jwe]
jwe
parents:
diff changeset
169 endfunction
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
170
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
171 function varargout = handle_gradient (f, p0, varargin)
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
172 ## Input checking
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
173 p0_size = size (p0);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
174
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
175 if (numel (p0_size) != 2)
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
176 error ("gradient: the second input argument should either be a vector or a matrix");
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
177 endif
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
178
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
179 if (any (p0_size == 1))
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
180 p0 = p0 (:);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
181 dim = 1;
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
182 num_points = numel (p0);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
183 else
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
184 num_points = p0_size (1);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
185 dim = p0_size (2);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
186 endif
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
187
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
188 if (length (varargin) == 0)
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
189 delta = 1;
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
190 elseif (length (varargin) == 1 || length (varargin) == dim)
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
191 try
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
192 delta = [varargin{:}];
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
193 catch
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
194 error ("gradient: spacing parameters must be scalars or a vector");
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
195 end_try_catch
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
196 else
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
197 error ("gradient: incorrect number of spacing parameters");
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
198 endif
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
199
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
200 if (isscalar (delta))
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
201 delta = repmat (delta, 1, dim);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
202 elseif (!isvector (delta))
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
203 error ("gradient: spacing values must be scalars or a vector");
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
204 endif
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
205
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
206 ## Calculate the gradient
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
207 p0 = mat2cell (p0, num_points, ones (1, dim));
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
208 varargout = cell (1, dim);
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
209 for d = 1:dim
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
210 s = delta (d);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
211 df_dx = (f (p0{1:d-1}, p0{d}+s, p0{d+1:end})
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
212 - f (p0{1:d-1}, p0{d}-s, p0{d+1:end})) ./ (2*s);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
213 if (dim == 1)
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
214 varargout{d} = reshape (df_dx, p0_size);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
215 else
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
216 varargout{d} = df_dx;
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
217 endif
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
218 endfor
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
219 endfunction
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
220
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
221 %!test
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
222 %! data = [1, 2, 4, 2];
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
223 %! dx = gradient (data);
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
224 %! dx2 = gradient (data, 0.25);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
225 %! dx3 = gradient (data, [0.25, 0.5, 1, 3]);
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
226 %! assert (dx, [1, 3/2, 0, -2]);
9015
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
227 %! assert (dx2, [4, 6, 0, -8]);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
228 %! assert (dx3, [4, 4, 0, -1]);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
229 %! assert (size_equal(data, dx));
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
230
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
231 %!test
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
232 %! [Y,X,Z,U] = ndgrid (2:2:8,1:5,4:4:12,3:5:30);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
233 %! [dX,dY,dZ,dU] = gradient (X);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
234 %! assert (all(dX(:)==1));
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
235 %! assert (all(dY(:)==0));
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
236 %! assert (all(dZ(:)==0));
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
237 %! assert (all(dU(:)==0));
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
238 %! [dX,dY,dZ,dU] = gradient (Y);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
239 %! assert (all(dX(:)==0));
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
240 %! assert (all(dY(:)==2));
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
241 %! assert (all(dZ(:)==0));
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
242 %! assert (all(dU(:)==0));
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
243 %! [dX,dY,dZ,dU] = gradient (Z);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
244 %! assert (all(dX(:)==0));
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
245 %! assert (all(dY(:)==0));
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
246 %! assert (all(dZ(:)==4));
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
247 %! assert (all(dU(:)==0));
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
248 %! [dX,dY,dZ,dU] = gradient (U);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
249 %! assert (all(dX(:)==0));
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
250 %! assert (all(dY(:)==0));
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
251 %! assert (all(dZ(:)==0));
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
252 %! assert (all(dU(:)==5));
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
253 %! assert (size_equal(dX, dY, dZ, dU, X, Y, Z, U));
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
254 %! [dX,dY,dZ,dU] = gradient (U, 5.0);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
255 %! assert (all(dU(:)==1));
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
256 %! [dX,dY,dZ,dU] = gradient (U, 1.0, 2.0, 3.0, 2.5);
06cebb6c5dde Fix calculation of gradient for dims>2. Vector arguments are interpreted as
Kai Habel <kai.habel@gmx.de>
parents: 8920
diff changeset
257 %! assert (all(dU(:)==2));
8601
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
258
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
259 %!test
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
260 %! x = 0:10;
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
261 %! f = @cos;
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
262 %! df_dx = @(x) -sin (x);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
263 %! assert (gradient (f, x), df_dx (x), 0.2);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
264 %! assert (gradient (f, x, 0.5), df_dx (x), 0.1);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
265
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
266 %!test
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
267 %! xy = reshape (1:10, 5, 2);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
268 %! f = @(x,y) sin (x) .* cos (y);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
269 %! df_dx = @(x, y) cos (x) .* cos (y);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
270 %! df_dy = @(x, y) -sin (x) .* sin (y);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
271 %! [dx, dy] = gradient (f, xy);
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
272 %! assert (dx, df_dx (xy (:, 1), xy (:, 2)), 0.1)
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
273 %! assert (dy, df_dy (xy (:, 1), xy (:, 2)), 0.1)
b297b86f4ad9 gradient.m: Add support for computing the gradient of a function handle
sh@sh-t400
parents: 8507
diff changeset
274