Hi, happy to help:
Use next code:
clc
clear all
close all
%non-square matrix:
%A=[1 0 0 0;0 17 0 0;0 0 -10 0 ;0 0 0 5;0 0 0 0]
A=[1 0 0 0;0 17 0 0;0 0 -10 0 ;0 0 0 5]
%DMD code:
%version 1:
n1=size(A,1);
n2=size(A,2);
B=eye(n1,n2);
a=0 %interval [a,b]
b=10
m=n2; %iterations
x=ones(n2,1); %constant vector
shift=0.5;
V = zeros(n2,m); %orthonormal basis of Krylov space
V(:,1) = x/norm(x);
H = zeros(min(m+1,m),n1); %upper Hessenberg matrix
W = zeros(n1,m);
Z = zeros(n1,m);
C=A-shift*B;
[L, U] = lu(C, 'vector');
for j=1:m
w=U\(L\(B*V(:,j)))
z = A*V(:,j)
W(:,j) = w;
Z(:,j) = z;
for i=1:j %Gram-schmidt
H(i,j) = V(:,i)'*w;
w = w - V(:,i)*H(i,j);
end
if j < n1
H(j+1,j) = norm(w,2);
if H(j+1,j) == 0, return, end
V(:,j+1) = w/H(j+1,j);
end
end
H
V
W
Z
%find dynamical modes:
%vector of mode amplitudes and ? estimation:
v1 = H*A
v2 = A*diag(Z)
v23 = diag(z)*A
v11 = W*A
v22 = Z*A
display('Accuracy measure 1:')
acc = mean(mean(abs(V-Z)))