-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW3.m
164 lines (140 loc) · 3.51 KB
/
HW3.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
clear all;
close all;
clc;
load reaction_diffusion_data.mat;
% %%
jj = [150 160 170 180 190 200];
n = length(jj);
figure();
hold on;
for i = 1:6
j = jj(i);
subplot(2,n,i);
pcolor(x,y,u(:,:,j)); shading interp; colormap(hot); colorbar;
caxis([-1 1])
subplot(2,n,n+i);
pcolor(x,y,v(:,:,j)); shading interp; colormap(hot); colorbar;
caxis([-1 1])
end
dt = t(2)-t(1);
%% Check truncation
u_data = reshape(u,[],length(t));
v_data = reshape(v,[],length(t));
X = [u_data;v_data];
[U,S,V] = svd(X,'econ');
figure();
hold on;
subplot(2,1,1);
plot(diag(S),'o');
xlim([0,70]);
subplot(2,1,2);
semilogy(diag(S),'o');
xlim([0,70]);
r = 10;
U = U(:,1:r);
V = V(:,1:r);
S = S(1:r,1:r);
for i = 1:size(X,2)
temp = U'*X(:,i);
X_cut(:,i) = U*temp;
end
u_cut = reshape(X_cut(1:size(u_data,1),:),size(u));
v_cut = reshape(X_cut(size(u_data,1)+1:end,:),size(u));
% jj = [150 160 170 180 190 200];
n = length(jj);
figure();
hold on;
for i = 1:6
j = jj(i);
subplot(2,n,i);
pcolor(x,y,u(:,:,j)); shading interp; colormap(hot); colorbar;
caxis([-1 1])
subplot(2,n,n+i);
pcolor(x,y,u_cut(:,:,j)); shading interp; colormap(hot); colorbar;
caxis([-1 1])
end
figure();
hold on;
for i = 1:6
j = jj(i);
subplot(2,n,i);
pcolor(x,y,v(:,:,j)); shading interp; colormap(hot); colorbar;
caxis([-1 1])
subplot(2,n,n+i);
pcolor(x,y,v_cut(:,:,j)); shading interp; colormap(hot); colorbar;
caxis([-1 1])
end
%% NN
V = U'*X;
aveV = mean(V,2);
input = V;
input = input(:,1:159)-aveV;
output = V;
output = output(:,2:160)-aveV;
% % net = feedforwardnet([20 10 20]);
% % net.layers{1}.transferFcn = 'poslin';
% % net.layers{2}.transferFcn = 'poslin';
% % net.layers{3}.transferFcn = 'poslin';
% % net = train(net,input,output);
numFeatures = 10;
numResponses = 10;
numHiddenUnits = 10;
layers = [ ...
sequenceInputLayer(numFeatures)
fullyConnectedLayer(20)
fullyConnectedLayer(10)
fullyConnectedLayer(20)
fullyConnectedLayer(numResponses)
regressionLayer];
options = trainingOptions('adam', ...
'MaxEpochs',500, ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',125, ...
'LearnRateDropFactor',0.2, ...
'Verbose',0, ...
'Plots','training-progress');
net = trainNetwork(input,output,layers,options);
predicted(:,1) = input(:,1);
for i = 2:201
predicted(:,i) = predict(net,predicted(:,i-1));
end
predicted = U*(predicted+aveV);
u_pred = reshape(predicted(1:size(u_data,1),:),size(u));
v_pred = reshape(predicted(size(u_data,1)+1:end,:),size(u));
n = length(jj);
figure();
hold on;
for i = 1:6
j = jj(i);
subplot(2,n,i);
pcolor(x,y,u(:,:,j)); shading interp; colormap(hot); colorbar;
xlabel('x')
ylabel('y')
title(['Time = ' num2str(t(j))])
caxis([-1 1])
subplot(2,n,n+i);
pcolor(x,y,u_pred(:,:,j)); shading interp; colormap(hot); colorbar;
xlabel('x')
ylabel('y')
title(['Time = ' num2str(t(j))])
caxis([-1 1])
end
figure();
hold on;
for i = 1:6
j = jj(i);
subplot(2,n,i);
pcolor(x,y,v(:,:,j)); shading interp; colormap(hot); colorbar;
xlabel('x')
ylabel('y')
title(['Time = ' num2str(t(j))])
caxis([-1 1])
subplot(2,n,n+i);
pcolor(x,y,v_pred(:,:,j)); shading interp; colormap(hot); colorbar;
xlabel('x')
ylabel('y')
title(['Time = ' num2str(t(j))])
caxis([-1 1])
end