-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLorenz_ODE.m
More file actions
249 lines (185 loc) · 5.16 KB
/
Copy pathLorenz_ODE.m
File metadata and controls
249 lines (185 loc) · 5.16 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
% New Generation Reservoir Computing using pseudorandom nonlinear projection
% of time-delay embedded input according to paper
% "Next-Generation Reservoir Computing for Dynamical Inference"
% https://doi.org/10.48550/arXiv.2509.11338
%
% Prediction of Lorenz system
% Map: X_{n+1} = W_{out} * P(X_n,X_{n-1},...)
%
close all
clear all
% discretization step
dt= 0.04;
%Model parameters
sig=10.;
r=28;
b=8/3;
%Lyapunov time for given parameters
Tlyap = 1.1;
P.sig=sig;
P.r=r;
P.b=b;
% feature vector length
M = 1000;
% number of samples for feature vector construction
H = 25;
opts = odeset('RelTol',1e-7,'AbsTol',1e-9);
% Integration time for transitional process
Ttr = 1000;
% Integration time for training data
Ttr1 = 1000;
Ttr1 = round(Ttr1/dt)*dt;
%Integration time for validation
Tvalid = 10%(validation_points-1)*dt;
Tvalid = round(Tvalid/dt)*dt;
%initial conditions
yinit = (randn(3,1)+1);
[T,Y] = ode45(@(t,X)eqsns(t,X,P),[0 Ttr],yinit,opts);
yinit = Y(end,:);
% training signal generation
[T,Y] = ode45(@(t,X)eqsns(t,X,P),0:dt:Ttr1,yinit,opts);
yinit = Y(end,:);
train_points = length(T);
% validation signal generation
[Tvalid,Yvalid] = ode45(@(t,X)eqsns(t,X,P),0:dt:Tvalid,yinit,opts);
% figure
% for i1 = 1:3
% subplot(3,1,i1)
% plot(T,Y(:,i1));
% end
X = Y(:,1);
xmx = max(X);
xmn = min(X);
eps = 0.1;
a = (2*eps-1)/(xmn-xmx);
b= eps-a*xmn;
% normalize training signal to the inteval [-eps; 1-eps];
Xnorm = a*X+b;
% array to keep pairs of indices (size: M x 2)
pairs = pairs_generation(H,M);
% % % Producing feature matrix P % % %
P = feature_matrix(Xnorm, H, M, pairs,0.01);
U = Xnorm(H+1:end);
% figure
% histogram(P(:),1000)
% % % Estimating weight matrix Wout: U = P*Wout % % %
PT = P.';
Wout = pinv(PT*P)*PT*U;
Upred = P*Wout;
figure
subplot(211)
plot(T(H+1:end),U), hold on
plot(T(H+1:end),Upred,'--')
title('Training signal- one step pred')
legend({'measur','prediction'})
xlabel('t')
subplot(212)
plot(T(H+1:end),Upred-U), hold on
ylabel('\Delta x')
xlabel('t')
Loss = mean( (Upred-U(1,:).').^2 );
fprintf('Loss = %e\n',Loss)
% % % % % % V A L I D A T I O N % % % % % % % %
% normalize validation signal
Xvalidnorm = a*Yvalid(:,1)+b;
Pvalid = feature_matrix(Xvalidnorm, H, M, pairs,0.01);
Uvalid = Xvalidnorm(H+1:end);
Uvalid_pred = Pvalid*Wout;
figure
subplot(211)
plot(Tvalid(H+1:end)-Tvalid(H),Uvalid), hold on
plot(Tvalid(H+1:end)-Tvalid(H),Uvalid_pred,'--')
legend({'True','Predic'})
xlabel('t')
title('Validation signal - one step pred.')
subplot(212)
plot(Tvalid(H+1:end)-Tvalid(H),Uvalid-Uvalid_pred), hold on
legend({'True - Predic'})
xlabel('t')
% % % % % % % LONG TIME PREDICTION % % % % % % %
% As starting initial conditions we use the begining of validation signal
validation_points = length(Xvalidnorm);
X = Xvalidnorm(1:H).';
% long time prediction algorithm
P_long_pred = NaN(1,M);
U_long_pred = NaN(validation_points-H,1);
for t1 = 1:validation_points-H-1
Utmp = NaN(H+M,1);
%current state
Utmp(1:H,1) = X;
len = H +1;
for i1 = 1:M
pi1 = pairs(i1,1);
pj1 = pairs(i1,2);
P_long_pred(1,i1) = (1-Utmp(pi1))^Utmp(pj1);
Utmp(len) = P_long_pred(1,i1);
len = len +1;
end
U_long_pred(t1) = P_long_pred*Wout;
X = [X(2:end) U_long_pred(t1)];
end
figure
subplot(211)
plot( (Tvalid-Tvalid(H))/Tlyap,Xvalidnorm), hold on
plot( (Tvalid(H+1:end)-Tvalid(H))/Tlyap,U_long_pred,'--')
legend({'True','Pred'})
xlim([-Tvalid(H)/Tlyap inf])
title('Long time prediction')
xlabel('$\Lambda t$','Interpreter','latex')
subplot(212)
plot( (Tvalid(H+1:end)-Tvalid(H))/Tlyap ,Xvalidnorm(H+1:end)-U_long_pred), hold on
legend({'True-Pred'})
xlabel('$\Lambda t$','Interpreter','latex')
xlim([0,3])
ylim([-0.025 0.025])
function P = feature_matrix(Xnorm, H,M,pairs,eps)
% % % Producing feature matrix P % % %
l1 = 1;
l2 = H;
train_points = length(Xnorm);
P = NaN(train_points-H,M);
Xtrain = Xnorm;
for t1 = 1:train_points-H
%temporal vector to store H steps of signal together with projections
Utmp = NaN(H+M,1);
%current H states
Utmp(1:H,1) = Xtrain(l1:l2)+ eps*randn(H,1);
len = H +1;
for i1 = 1:M
pi1 = pairs(i1,1);
pj1 = pairs(i1,2);
P(t1,i1) = (1-Utmp(pi1))^Utmp(pj1);
Utmp(len) = P(t1,i1);
len = len +1;
end
l1 = l1 + 1;
l2 = l2 + 1;
end
end
function pairs= pairs_generation(H,M)
pairs = NaN(M,2);
% maximal number from which to take two random numbers
len = H;
% initial random pair of integers
pr1 = randi(len,1,2);
for i1 = 1:M
%check if random pair pr1 is not alredy in pairs array,
% also that pr1(1) and pr1(2) have different values
while (any(ismember(pairs,pr1,"rows")) || (pr1(1)==pr1(2)))
pr1 = randi(len,1,2);
end
pairs(i1,:)= pr1;
len = len + 1;
pr1 = randi(len,1,2);
end
end
function dy = eqsns(~,X,P)
sig=P.sig;
r=P.r;
b=P.b;
x=X(1); y=X(2); z=X(3);
dy = zeros(3,1);
dy(1)=-sig*(x-y);
dy(2)= r*x-y-x*z;
dy(3)= x*y - b *z;
end