Skip to content

Commit

Permalink
Replace reduced dimension by energy fraction in MATLAB
Browse files Browse the repository at this point in the history
  • Loading branch information
siuwuncheung committed Nov 3, 2023
1 parent c02db4d commit f88308e
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions unit_tests/matlab/dmdc.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function dmdc(X, Y, r, t, dt, varargin)
function dmdc(X, Y, ef, t, dt, varargin)
% Input: X: n by m snapshot matrix
% Y: n by (m-1) control snapshot matrix
% r: number of basis_vectors to use
% ef: energy fraction
% t: The time to predict
% dt: delta time
% B: (optional) known control matrix
Expand All @@ -12,12 +12,16 @@ function dmdc(X, Y, r, t, dt, varargin)

if nargin == 5
X_in = [X_in; Y];
else
elseif nargin == 6
B = varargin{1};
X_out = X_out - B*Y;
else
error('Invalid input');
end

[U, S, V] = svd(X_in, 'econ');
r = sum(cumsum(diag(S))/sum(diag(S)) < ef)

U = U(:,1:r);
S = S(1:r,1:r);
V = V(:,1:r);
Expand All @@ -26,15 +30,14 @@ function dmdc(X, Y, r, t, dt, varargin)
U1 = U(1:m,:);
if nargin == 5
U2 = U(m+1:end,:);
r_out = r-1;
[U_out, ~, ~] = svd(X_out, 'econ');
[U_out, S_out, ~] = svd(X_out, 'econ');
r_out = sum(cumsum(diag(S_out))/sum(diag(S_out)) < ef)
U_out = U_out(:,1:r_out);
Atilde = U_out'*X_out*V*inv(S)*U1'*U_out;
Btilde = U_out'*X_out*V*inv(S)*U2';
[W,ev] = eig(Atilde);
%Phi = X_out*V*inv(S)*U1'*U_out*W;
else
r_out = r;
U_out = U1;
Atilde = U_out'*X_out*V*inv(S);
Btilde = U_out'*B;
Expand All @@ -46,4 +49,4 @@ function dmdc(X, Y, r, t, dt, varargin)
pred = U_out*W*sum((diag(ev).^(n:-1:0)).*(W\[U_out'*b0, Btilde*Y]),2);

norm(real(pred-X(:,end)))/norm(real(X(:,end)))
end
end

0 comments on commit f88308e

Please sign in to comment.