-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjaco.jl
97 lines (76 loc) · 2.12 KB
/
jaco.jl
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
# Compute the 1st or 2nd order partial (numerical) derivates of a function
#
# usage: [j,ip] = jaco(fun,x0,V,verbose,order)
#
# (order 1:) Compute the 1st order partial derivatives (gradient)
# of a function using:
# j(ip,:) = ( f(x(ip)+h) - f(x(ip)-h) ) / (2 * h)
#
# (order 2:) Compute the 2nd order derivatives (curvature):
#
# j(ip,:) = [ (f0 - f1) / 2 / d ] ./ [ (f0 - 2 * fx + f1) / d ^ 2 ]
#
#
# if order==1, when j is square, it is the Jacobian
# if order==2, when j is square, it is the Hessian
#
import DataStructures
include("dic_to_vect.jl");
function jaco(fun,x,u,P,M,order)
# state differentiation: df/dx [Jacobian or Hessian matrix]
f0 = fun(x,u,P,M);
fx = copy(f0);
j = zeros( length(x), length(x) );
V = copy(f0);
for i = 1:length(x)
x0 = reshape(float(copy(x)),1,length(x));
x1 = reshape(float(copy(x)),1,length(x));
d = x[i] + V[i]*exp(-8);
if d == 0
d = 0.01;
end
x0[i] = x0[i] + d;
x1[i] = x1[i] - d;
f0 = fun(reshape(x0,size(x)),u,P,M);
f1 = fun(reshape(x1,size(x)),u,P,M);
j[i,:] = (f0 - f1) / (2d);
if order == 2
deriv1 = (f0 - f1) / 2 / d;
deriv2 = (f0 - 2 * fx + f1) / d^2;
j[i,:] = deriv1 ./ deriv2;
end
end
j = j';
return j, fx
end
function dpdx(x,u,P,M,order)
# parameter differentiation function: dp/df or dp/dx
f0 = fun(x,u,P,M);
fx = copy(f0);
P0 = copy(dict_to_vec(P));
if P0[1] == float
P0 = P0[2:end];
end
j = zeros( length(P0), length(x) );
V = copy(P0);
for i = 1:length(P0)
x0 = copy(P0);
x1 = copy(P0);
d = P0[i] + V[i]*exp(-8);
if d == 0
d = 0.01;
end
x0[i] = x0[i] + d;
x1[i] = x1[i] - d;
f0 = fun(x,u,vec_to_dict(x0,P),M);
f1 = fun(x,u,vec_to_dict(x1,P),M);
j[i,:] = (f0 - f1) / (2d);
if order == 2
deriv1 = (f0 - f1) / 2 / d;
deriv2 = (f0 - 2 * fx + f1) / d^2;
j[i,:] = deriv1 ./ deriv2;
end
end
j = j';
return j, fx
end