1 ## Copyright (C) 2011 Fotios Kasolis <fotios.kasolis@gmail.com>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {Df =} jacobs (@var{x}, @var{f})
18 ## @deftypefnx {Function File} {Df =} jacobs (@var{x}, @var{f}, @var{hook})
19 ## Calculate the jacobian of a function using the complex step method.
21 ## Let @var{f} be a user-supplied function. Given a point @var{x} at
22 ## which we seek for the Jacobian, the function @command{jacobs} returns
23 ## the Jacobian matrix @code{d(f(1), @dots{}, df(end))/d(x(1), @dots{},
24 ## x(n))}. The function uses the complex step method and thus can be
25 ## applied to real analytic functions.
27 ## The optional argument @var{hook} is a structure with additional options. @var{hook}
28 ## can have the following fields:
31 ## @code{h} - can be used to define the magnitude of the complex step and defaults
32 ## to 1e-20; steps larger than 1e-3 are not allowed.
34 ## @code{fixed} - is a logical vector internally usable by some optimization
35 ## functions; it indicates for which elements of @var{x} no gradient should be
36 ## computed, but zero should be returned.
43 ## f = @@(x) [x(1)^2 + x(2); x(2)*exp(x(1))];
44 ## Df = jacobs ([1, 2], f)
49 function Df = jacobs (x, f, hook)
51 if ( (nargin < 2) || (nargin > 3) )
56 f = str2func (f, "global");
62 max_h = 1e-3; # must be positive
66 if (isfield (hook, "h"))
67 if (! (isscalar (hook.h)))
68 error ("complex step magnitude must be a scalar");
70 if (abs (hook.h) > max_h)
71 warning ("complex step magnitude larger than allowed, set to %e", ...
81 if (isfield (hook, "fixed"))
82 if (numel (hook.fixed) != n)
83 error ("index of fixed parameters has wrong dimensions");
98 error ("all elements of 'x' are fixed");
101 x = repmat (x(:), 1, n) + h * 1i * eye (n);
103 idx = find (! fixed);
105 ## after first evaluation, dimensionness of 'f' is known
106 t_Df = imag (f (x(:, idx(1)))(:));
111 Df(:, idx(1)) = t_Df;
113 for count = idx(2:end)
114 Df(:, count) = imag (f (x(:, count))(:));
121 %!assert (jacobs (1, @(x) x), 1)
122 %!assert (jacobs (6, @(x) x^2), 12)
123 %!assert (jacobs ([1; 1], @(x) [x(1)^2; x(1)*x(2)]), [2, 0; 1, 1])
124 %!assert (jacobs ([1; 2], @(x) [x(1)^2 + x(2); x(2)*exp(x(1))]), [2, 1; 2*exp(1), exp(1)])
126 %% Test input validation
129 %!error jacobs (1, 2, 3, 4)
130 %!error jacobs (@sin, 1, [1, 1])
131 %!error jacobs (@sin, 1, ones(2, 2))
134 %! # Relative error against several h-values
135 %! k = 3:20; h = 10 .^ (-k); x = 0.3*pi;
136 %! err = zeros (1, numel (k));
137 %! for count = 1 : numel (k)
138 %! err(count) = abs (jacobs (x, @sin, struct ("h", h(count))) - cos (x)) / abs (cos (x)) + eps;
140 %! loglog (h, err); grid minor;
141 %! xlabel ("h"); ylabel ("|Df(x) - cos(x)| / |cos(x)|")
142 %! title ("f(x)=sin(x), f'(x)=cos(x) at x = 0.3pi")