File: jacob_element.m

package info (click to toggle)
dynare 7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 79,248 kB
  • sloc: cpp: 82,011; ansic: 28,583; objc: 12,573; yacc: 5,105; pascal: 2,374; lex: 1,502; python: 1,118; sh: 1,116; makefile: 605; lisp: 162; xml: 18
file content (150 lines) | stat: -rw-r--r-- 3,576 bytes parent folder | download
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
function d=jacob_element(func,element,args)
% function d=jacob_element(func,element,args)
% returns an entry of the finite differences approximation to the Jacobian of func
%
% INPUTS
%    func       [function name]    string with name of the function
%    element    [int]              the index showing the element within the Jacobian that should be returned
%    args       [cell array]       arguments provided to func
%
% OUTPUTS
%    d          [double]           Jacobian [element]
%
% SPECIAL REQUIREMENTS
%    none

% Copyright © 2010-2025 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <https://www.gnu.org/licenses/>.

assert(element <= length(args));

func = str2func(func);

h=1e-6;
margs=args;

args{element} = args{element} + h;
margs{element} = margs{element} - h;

d=(func(args{:})-func(margs{:}))/(2*h);

return % --*-- Unit tests --*--

%@test:1
% Test polynomial function: f(x,y) = x^3 + 2*x^2*y + 3*y^2 + 4*x + 5*y + 6
% Analytical derivatives:
%   df/dx = 3*x^2 + 4*x*y + 4
%   df/dy = 2*x^2 + 6*y + 5

% Test at point (x,y) = (2, 3)
x0 = 2;
y0 = 3;

% Analytical Jacobian at (2,3):
% df/dx = 3*4 + 4*2*3 + 4 = 12 + 24 + 4 = 40
% df/dy = 2*4 + 6*3 + 5 = 8 + 18 + 5 = 31

try
    jac_x = jacob_element('test_poly_2vars', 1, {x0, y0});
    jac_y = jacob_element('test_poly_2vars', 2, {x0, y0});
    t(1) = true;
catch
    t(1) = false;
end

if t(1)
    t(2) = abs(jac_x - 40) < 1e-6;
    t(3) = abs(jac_y - 31) < 1e-6;
end
T = all(t);
%@eof:1

%@test:2
% Test at origin (0,0) for simpler verification
x0 = 0;
y0 = 0;

% Analytical Jacobian at (0,0):
% df/dx = 0 + 0 + 4 = 4
% df/dy = 0 + 0 + 5 = 5

try
    jac_x = jacob_element('test_poly_2vars', 1, {x0, y0});
    jac_y = jacob_element('test_poly_2vars', 2, {x0, y0});
    t(1) = true;
catch
    t(1) = false;
end

if t(1)
    t(2) = abs(jac_x - 4) < 1e-6;
    t(3) = abs(jac_y - 5) < 1e-6;
end
T = all(t);
%@eof:2

%@test:3
% Test with negative values
x0 = -1;
y0 = -2;

% Analytical Jacobian at (-1,-2):
% df/dx = 3*1 + 4*(-1)*(-2) + 4 = 3 + 8 + 4 = 15
% df/dy = 2*1 + 6*(-2) + 5 = 2 - 12 + 5 = -5

try
    jac_x = jacob_element('test_poly_2vars', 1, {x0, y0});
    jac_y = jacob_element('test_poly_2vars', 2, {x0, y0});
    t(1) = true;
catch
    t(1) = false;
end

if t(1)
    t(2) = abs(jac_x - 15) < 1e-6;
    t(3) = abs(jac_y - (-5)) < 1e-6;
end
T = all(t);
%@eof:3

%@test:4
% Test with large values to check numerical stability
x0 = 10;
y0 = 20;

% Analytical Jacobian at (10,20):
% df/dx = 3*100 + 4*10*20 + 4 = 300 + 800 + 4 = 1104
% df/dy = 2*100 + 6*20 + 5 = 200 + 120 + 5 = 325

try
    jac_x = jacob_element('test_poly_2vars', 1, {x0, y0});
    jac_y = jacob_element('test_poly_2vars', 2, {x0, y0});
    t(1) = true;
catch
    t(1) = false;
end

if t(1)
    t(2) = abs(jac_x - 1104) < 1e-4;
    t(3) = abs(jac_y - 325) < 1e-6;
end
T = all(t);
%@eof:4

function f = test_poly_2vars(x, y)
    % Test polynomial: f(x,y) = x^3 + 2*x^2*y + 3*y^2 + 4*x + 5*y + 6
    f = x^3 + 2*x^2*y + 3*y^2 + 4*x + 5*y + 6;