File: jacobian.m

package info (click to toggle)
dynare 5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 77,852 kB
  • sloc: cpp: 94,481; ansic: 28,551; pascal: 14,532; sh: 5,453; objc: 4,671; yacc: 4,442; makefile: 2,923; lex: 1,612; python: 677; ruby: 469; lisp: 156; xml: 22
file content (26 lines) | stat: -rw-r--r-- 610 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function [r, J] = jacobian(fun, x, h)

% Returns the Jacobian matrix associated to a function.
%
% INPUTS:
% - fun    [handle]   Function from Rᵖ to Rⁿ.
% - x      [double]   1×p vector, point where the Jacobian has to be evaluated.
% - h      [double]   scalar, perturbation size.
%
% OUTPUTS:
% - r      [double]   n×1 vector, value of the function at point x.
% - J      [double]   n×p matrix of derivatives.

r = fun(x);

if nargout>1
    z = x;
    n = length(r);
    p = length(x);
    J = zeros(n, p);
    for i=1:p
        x = z;
        x(i) = x(i)+h;
        J(:,i) = (r-fun(x))/h;
    end
end