-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpostprocessEnergyExt3d.m
75 lines (65 loc) · 1.7 KB
/
postprocessEnergyExt3d.m
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
% Get derivatives of value function v, that are responsible for the optimal
% control strategy
% a python version of this file can be found in
% Dissertations/Jan\ Blechschmidt/FEniCS/pythonHJBPostprocessing
function [dec] = postprocessEnergy3d(p, a, v)
A = setupDifferentialOperator(p, a, 'FirstOrder', 2);
dvdy = A*v;
d = 3;
Alpha = p.alpha(a.XY);
Beta = p.beta(a.XY);
% Initialize decision boundaries
dec.MinT = zeros(0,3);
dec.MinQ = zeros(0,3);
dec.SellT = zeros(0,3);
dec.SellQ = zeros(0,3);
dec.BuyT = zeros(0,3);
dec.BuyQ = zeros(0,3);
% Loop over all triangles
for i = 1:p.numtri
t = p.tri(i,:);
xyt = a.XY(t,:);
xt = a.X(t);
vq = dvdy(t);
gmin = Alpha(t);
gmax = Beta(t);
bbuy = vq - (1 + p.costpbuy) * xt - p.costfbuy;
bsell = vq - (1 - p.costpsell) * xt + p.costfsell;
% Boundary Do-nothing
sect = linearIntersection3d(xyt, gmin, 0.0);
switch size(sect,1)
case 3
dec.MinT = [dec.MinT; sect];
case 4
dec.MinQ = [dec.MinQ; sect];
end
% Boundary Sell
gmin(gmin >= 0) = 0;
sect = linearIntersection3d(xyt, bsell.*gmin, p.costcsell);
switch size(sect,1)
case 3
dec.SellT = [dec.SellT; sect];
case 4
dec.SellQ = [dec.SellQ; sect];
end
% Boundary Buy
if min(gmin)<0
sectbuy = linearIntersection3d(xyt, bbuy.*gmax, p.costcbuy);
switch size(sectbuy,1)
case 3
dec.BuyT = [dec.BuyT; sectbuy];
case 4
dec.BuyQ = [dec.BuyQ; sectbuy];
end
else
% if bbuy.*gmax < p.costcbuy
sectNo = linearIntersection3d(xyt, bbuy, 0.0);
switch size(sectNo,1)
case 3
dec.BuyT = [dec.BuyT; sectNo];
case 4
dec.BuyQ = [dec.BuyQ; sectNo];
end
% end
end
end % for i = 1:p.numtri