This repository has been archived by the owner on Jun 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Julia_comparison_stationary_test.m
169 lines (147 loc) · 6.26 KB
/
Julia_comparison_stationary_test.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
% This is test function that try to replicate Julia problem with same set
% up
%Using the unit testing framework in matlab. See https://www.mathworks.com/help/matlab/matlab_prog/write-simple-test-case-with-functions.html
%To run tests:
% runtests %would run all of them in the current directory
% runtests('my_test') %runs just the my_test.m file
% runtests('my_test/my_function_test') %runs only `my_function_test function in `my_test'.
function tests = Julia_comparison_stationary_test
tests = functiontests(localfunctions);
end
%This is run at the beginning of the test. Not required.
function setupOnce(testCase)
addpath('../lib/');
testCase.TestData.tolerances.test_tol = 1e-9;
testCase.TestData.tolerances.lower_test_tol = 1e-8; %For huge matrices, the inf norm can get a little different.
testCase.TestData.tolerances.default_csv_precision = '%.10f'; %Should be higher precision than tolerances.test_tol
end
function uniform_more_grid_test(testCase)
tolerances = testCase.TestData.tolerances;
r = 0.05;
zeta = 14.5;
gamma = 0.005;
g = 0.020758;
mu_x = @(x) gamma-g;
sigma_bar = 0.02;
sigma_2_x = @(x) (sigma_bar).^2+0.0*x;
u_x = @(x) exp(0.5*x);
rho = r-g;
x_min = 0.0;
x_max = 5.0;
I = 500;
x = linspace(x_min,x_max,I)';
A = discretize_univariate_diffusion(x, mu_x(x), sigma_2_x(x));
u = u_x(x);
%Solve the simple problem: rho v(x) = u(x) + A v(x) for the above process.
[v, success] = simple_HJBE_discretized_univariate(A, x, u, rho);
%dlmwrite(strcat(mfilename, '_1_v_output.csv'), v, 'precision', tolerances.default_csv_precision); %Uncomment to save again
%v_check = dlmread(strcat(mfilename, '_1_v_output.csv'));
%verifyTrue(testCase,norm(v - v_check, Inf) < tolerances.test_tol, 'v value no longer matches');
%verifyTrue(testCase, success==true, 'unsuccesful');
%Solve with a uniform grid and check if similar after interpolation
x_2 = linspace(x_min, x_max, 701)'; %Twice as many points to be sure.
A_2 = discretize_univariate_diffusion(x_2, mu_x(x_2), sigma_2_x(x_2));
%Solve the simple problem: rho v(x) = u(x) + A v(x) for the above process.
[v_2, success] = simple_HJBE_discretized_univariate(A_2, x_2, u_x(x_2), rho);
figure()
v_2int=interp1(x_2, v_2, x);
dif=v-v_2int;
plot(x,dif,'LineWidth',2);hold on
title('difference of v and v_2 along z_grid, z_bar=5')
%Make sure within range. This seems too large.
verifyTrue(testCase, norm(interp1(x_2, v_2, x) - v,Inf) < 0.02, 'Not within range of interpolation');
end
function uniform_more_grid_test2(testCase)
tolerances = testCase.TestData.tolerances;
r = 0.05;
zeta = 14.5;
gamma = 0.005;
g = 0.020758;
mu_x = @(x) gamma-g;
sigma_bar = 0.02;
sigma_2_x = @(x) (sigma_bar).^2+0.0*x;
u_x = @(x) exp(x);
rho = r-g;
x_min = 0.0;
x_max = 2.0; % lower xbar
I = 301;
x = linspace(x_min,x_max,I)';
A = discretize_univariate_diffusion(x, mu_x(x), sigma_2_x(x));
u = u_x(x);
%Solve the simple problem: rho v(x) = u(x) + A v(x) for the above process.
[v, success] = simple_HJBE_discretized_univariate(A, x, u, rho);
%dlmwrite(strcat(mfilename, '_1_v_output.csv'), v, 'precision', tolerances.default_csv_precision); %Uncomment to save again
%v_check = dlmread(strcat(mfilename, '_1_v_output.csv'));
%verifyTrue(testCase,norm(v - v_check, Inf) < tolerances.test_tol, 'v value no longer matches');
%verifyTrue(testCase, success==true, 'unsuccesful');
%Solve with a uniform grid and check if similar after interpolation
x_2 = linspace(x_min, x_max, 701)'; %Twice as many points to be sure.
A_2 = discretize_univariate_diffusion(x_2, mu_x(x_2), sigma_2_x(x_2));
%Solve the simple problem: rho v(x) = u(x) + A v(x) for the above process.
[v_2, success] = simple_HJBE_discretized_univariate(A_2, x_2, u_x(x_2), rho);
figure()
v_2int=interp1(x_2, v_2, x);
dif=v-v_2int;
plot(x,dif,'LineWidth',2);hold on
title('difference of v and v_2 along z_grid,z_bar=2')
%Make sure within range. This seems too large.
verifyTrue(testCase, norm(interp1(x_2, v_2, x) - v,Inf) < 0.02, 'Not within range of interpolation');
end
function uniform_plot_test(testCase)
tolerances = testCase.TestData.tolerances;
r = 0.05;
zeta = 14.5;
gamma = 0.005;
g = 0.020758;
mu_x = @(x) gamma-g;
sigma_bar = 0.02;
sigma_2_x = @(x) (sigma_bar).^2+0.0*x;
u_x = @(x) exp(x);
rho = r-g;
x_min = 0.0;
x_max = 5.0; % lower xbar
%Solve the simple problem: rho v(x) = u(x) + A v(x) for the above process.
I_c=[101 201 301 401 501];
for i=1:5
I = I_c(i);
x = linspace(x_min,x_max,I)';
A = discretize_univariate_diffusion(x, mu_x(x), sigma_2_x(x));
u = u_x(x);
[v_{i}, success] = simple_HJBE_discretized_univariate(A, x, u, rho);
x_{i}=x;
end
figure()
for i=1:5
plot(x_{i}(end-5:end),v_{i}(end-5:end)); hold on
end
legend('101','201','301','401','500')
title('value function at last 5 grid point')
end
function uniform_to_nonuniform_test(testCase)
tolerances = testCase.TestData.tolerances;
r = 0.05;
zeta = 14.5;
gamma = 0.005;
g = 0.020758;
mu_x = @(x) gamma-g;
sigma_bar = 0.02;
sigma_2_x = @(x) (sigma_bar).^2+0.0*x;
u_x = @(x) exp(x);
rho = r-g;
x_min = 0.0;
x_max = 2.0; % lower xbar
%Solve the simple problem: rho v(x) = u(x) + A v(x) for the above process.
I = 301;
x = linspace(x_min,x_max,I)';
A = discretize_univariate_diffusion(x, mu_x(x), sigma_2_x(x));
u = u_x(x);
[v, success] = simple_HJBE_discretized_univariate(A, x, u, rho);
I2=floor(I*1/3); % propotional adding grid points
x_2 = unique([linspace(x_min, x_max, I)'; linspace(1.7,x_max,I2)']); %Twice as many points to be sure.
A_2 = discretize_univariate_diffusion(x_2, mu_x(x_2), sigma_2_x(x_2));
%Solve the simple problem: rho v(x) = u(x) + A v(x) for the above process.
[v_2, success] = simple_HJBE_discretized_univariate(A_2, x_2, u_x(x_2), rho);
v_2int=interp1(x_2, v_2, x);
%Make sure within range. This seems too large.
verifyTrue(testCase, norm(interp1(x_2, v_2, x) - v,Inf) < 0.02, 'Not within range of interpolation');
end