-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProblem2.m
More file actions
198 lines (167 loc) · 4.36 KB
/
Copy pathProblem2.m
File metadata and controls
198 lines (167 loc) · 4.36 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
clear
close all
load final_al_data.txt
udata = final_al_data(2:16);
udata_sample = udata(4:12);
xdata = [10 14 18 22 26 30 34 38 42 46 50 54 58 62 66];
xdata_sample = xdata(4:12);
u_amb = final_al_data(17);
% Input dimensions and material constants
a = 0.95; % cm
b = 0.95; % cm
L = 70.0; % cm
k = 2.37; % W/cm C
h = 0.00191;
Q = -18.41;
n = 15;
p = 2;
% Construct constants and solution
%
gamma = sqrt(2*(a+b)*h/(a*b*k));
gamma_h = (1/(2*h))*gamma;
f1 = exp(gamma*L)*(h + k*gamma);
f2 = exp(-gamma*L)*(h - k*gamma);
f3 = f1/(f2 + f1);
f1_h = exp(gamma*L)*(gamma_h*L*(h+k*gamma) + 1 + k*gamma_h);
f2_h = exp(-gamma*L)*(-gamma_h*L*(h-k*gamma) + 1 - k*gamma_h);
c1 = -Q*f3/(k*gamma);
c2 = Q/(k*gamma) + c1;
f4 = Q/(k*gamma*gamma);
den2 = (f1+f2)^2;
f3_h = (f1_h*(f1+f2) - f1*(f1_h+f2_h))/den2;
c1_h = f4*gamma_h*f3 - (Q/(k*gamma))*f3_h;
c2_h = -f4*gamma_h + c1_h;
c1_Q = -(1/(k*gamma))*f3;
c2_Q = (1/(k*gamma)) + c1_Q;
uvals_data = c1*exp(-gamma*xdata) + c2*exp(gamma*xdata) + u_amb;
uvals_Q_data = c1_Q*exp(-gamma*xdata) + c2_Q*exp(gamma*xdata);
uvals_h_data = c1_h*exp(-gamma*xdata) + c2_h*exp(gamma*xdata) + gamma_h*xdata.*(-c1*exp(-gamma*xdata) + c2*exp(gamma*xdata));
res = udata - uvals_data;
sens_mat = [uvals_Q_data; uvals_h_data];
sigma2 = (1/(n-p))*(res*res');
V = (sigma2*eye(p))/(sens_mat*sens_mat');
% Construct the xdata, udata and covariance matrix V employed in DRAM.
% Set the options employed in DRAM.
clear data model options
data.xdata = xdata_sample';
data.ydata = udata_sample';
tcov = V;
tmin = [Q; h];
params = {
{'q1',tmin(1), -20}
{'q2',tmin(2), 0}};
model.ssfun = @heatss;
model.sigma2 = sigma2;
options.qcov = tcov;
options.nsimu = 10000;
options.updatesigma = 1;
N = 10000;
%%
% Run DRAM to construct the chains (stored in chain) and measurement
% variance (stored in s2chain).
%
[results,chain,s2chain] = mcmcrun(model,data,params,options);
%%
% Construct the densities for Q and h.
%
Qvals = chain(:,1);
hvals = chain(:,2);
[bandwidth_Q,density_Q,Qmesh,cdf_Q]=kde(Qvals);
[bandwidth_h,density_h,hmesh,cdf_h]=kde(hvals);
figure(1); clf
mcmcplot(chain,[],results,'chainpanel');
figure(2); clf
mcmcplot(chain,[],results,'pairs');
cov(chain)
chainstats(chain,results)
figure(3); clf
plot(Qvals,'-')
set(gca,'Fontsize',22);
%axis([0 N -19.3 -17.5])
xlabel('Chain Iteration')
ylabel('Parameter Q')
figure(4); clf
plot(hvals,'-')
set(gca,'Fontsize',22);
%axis([0 N 1.84e-3 2e-3])
xlabel('Chain Iteration')
ylabel('Parameter h')
figure(5); clf
plot(Qmesh,density_Q,'k-','linewidth',3)
%axis([-19.5 -17.5 0 3])
set(gca,'Fontsize',22);
xlabel('Parameter \Phi')
figure(6); clf
plot(hmesh,density_h,'k-','linewidth',3)
%axis([1.8e-3 2e-3 0 3e4])
set(gca,'Fontsize',22);
xlabel('Parameter h')
figure(7); clf
scatter(Qvals,hvals)
box on
%axis([-19.2 -17.6 1.83e-3 2e-3])
set(gca,'Fontsize',23);
xlabel('Parameter Q')
ylabel('Parameter h')
figure(8); clf
plot(s2chain,'-')
set(gca,'Fontsize',22);
%axis([0 N -19.3 -17.5])
xlabel('Chain Iteration')
ylabel('\sigma^2')
sigma2_d = mean(s2chain);
x_ep = -3*sigma2_d:0.001:3*sigma2_d;
y_ep = normpdf(x_ep, 0, sigma2_d );
figure(9); clf
plot(x_ep,y_ep,'k-','linewidth',3)
%axis([1.8e-3 2e-3 0 3e4])
set(gca,'Fontsize',22);
xlabel('\epsilon')
figure(10)
out = mcmcpred(results,chain,s2chain,xdata',@heat_solution,2000);
mcmcpredplot(out);
hold on
set(gca,'Fontsize',26);
plot(xdata, udata, '*r', 'linewidth',3)
hold off
function ss = heatss(params,data)
udata = data.ydata;
xdata = data.xdata;
% Input dimensions and material constants
a = 0.95; % cm
b = 0.95; % cm
L = 70.0; % cm
k = 2.37; % W/cm C
Q = params(1);
h = params(2);
u_amb = 21.2897;
% Construct constants and solution
gamma = sqrt(2*(a+b)*h/(a*b*k));
f1 = exp(gamma*L)*(h + k*gamma);
f2 = exp(-gamma*L)*(h - k*gamma);
f3 = f1/(f2 + f1);
c1 = -Q*f3/(k*gamma);
c2 = Q/(k*gamma) + c1;
uvals_data = c1*exp(-gamma*xdata) + c2*exp(gamma*xdata) + u_amb;
res = udata - uvals_data;
ss = res'*res;
end
function uvals_data = heat_solution(data, params)
xdata = data;
% Input dimensions and material constants
a = 0.95; % cm
b = 0.95; % cm
L = 70.0; % cm
k = 2.37; % W/cm C
Q = params(1);
h = params(2);
u_amb = 21.2897;
% Construct constants and solution
gamma = sqrt(2*(a+b)*h/(a*b*k));
f1 = exp(gamma*L)*(h + k*gamma);
f2 = exp(-gamma*L)*(h - k*gamma);
f3 = f1/(f2 + f1);
c1 = -Q*f3/(k*gamma);
c2 = Q/(k*gamma) + c1;
uvals_data = c1*exp(-gamma*xdata) + c2*exp(gamma*xdata) + u_amb;
end