I have an array of data whose columns are solution vectors to a system of ODEs at a specific time. I want to plot the power spectrum of a solution at a specific time, but when I attempt this I get unexpected results. I want to make sure I am plotting correctly.
First of all, I follow Matlab's format for storing the spatial frequency values $k$:
k=[0:L/2-1 -L/2:-1]*2*pi/L
where L
is the total number of grid points. I do NOT understand why the k
s are stored this way, but I digress.
In the following snippit of code, I pick a column, the sixth, take the FFT, then take the absolute-value squared and plot this against k
in the following way:
plot(k, abs( fft( udata(:,6) ) )'.^2 )
where udata
is the array of solution vectors.
For certain parameters of my code, there is a value of k
for which I expect the power-spectrum to be the most extreme. However, I do not see this reflected in my plotting.
For example, in the below code, the value for which I expect the power-spectrum to be maximized at is k=0.336311
. However, in the plot below, you can see that I do not observe a big peak there. In fact, there is a peak at 0
which I do not understand.
%% Strang splitting for solving the fDNLS
% i ∂_t u_n = (L_alpha u)_n - |u_n|^2 u_n
% Grid and time information
% Integer lattice
L = 100;
dt=0.01; tmax=25; % Time step and max time
nmax=round(tmax/dt); % Maximum cycles related to max time
dx=1; % Unit grid spacing
x=[-L/2:dx:L/2-dx]'; % Grid
k=2*pi*[0:L/2-1 -L/2:-1]'/L;
[row col] = size(x);
%% Parameters
A = 1;
alpha =1;
eps=1;
kmax=0.336311;
% Initial condition
%u = sin(x).^2;
initial_condition = A + exp(1i*kmax*x);
%initial_condition(1:50) = 0;
u = initial_condition;
%u = A*ones(row,col) + ones(row,col).*cos(pi*x);
%u = A*ones(row,col);
udata=u; tdata=0;
% Long Range Interaction matrix
LRI_MATRIX = LRI_Matrix_periodic_direct(L/2,alpha);
for nn=1:nmax
% Cycle through the dense part of the splitting operators
% Let v denote the intermediate solution
if nn == 1
% First cycle
% Half-step of Linear operator
v = expm(-1i * eps^(-(1+alpha)) *LRI_MATRIX * dt/2)*u;
% Full-step of Nonlinear operator
u = v.*exp(1i * v .* conj(v) * dt);
elseif nn ~= 1 && nn ~= nmax
% Middle cycles
% Full-step of Linear operator
v = expm(-1i * eps^(-(1+alpha)) *LRI_MATRIX * dt)*u;
% Full-step of Nonlinear operator
u = v.*exp(1i * v .* conj(v) * dt);
elseif nn == nmax
% Last cycle
% Half-step of Linear operator
u = expm(-1i * eps^(-(1+alpha)) * LRI_MATRIX * dt/2)*u;
end
% Store the cycle data
udata=[udata u]; tdata=[tdata nn*dt];
end
figure
plot(k, abs( fft( udata(:,6) ) )'.^2 )
xlabel("Spatial frequency"), ylabel('$|\hat{u}|^2$','Interpreter','latex'),
title("Power spectrum")
%Long range interaction on the lattice [-N,N] with periodic BC on n in {-N, ..., N-1}
%p = dim = 2N
function M = LRI_Matrix_periodic_direct(N,a)
p = 2*N;
%% Initialize the coefficient matrix for the LRI operator
M = zeros(p,p);
%% Populate the diagonal
M_DIAG = 2*zeta(1+a)*(1 - 1/(2*N)^(1+a))*eye(p);
%% Populate the dense part of the matrix
M_DENSE = zeros(p);
for i = 1:p
% Go through each row
for j = 1:p
% Go through each column
if j~=i
M_DENSE(i,j) = -cnr(i-(N+1),j-(N+1),a,N);
end
end
end
M = M_DIAG + M_DENSE;
end
function off_diag = cnr(n,r,a,N)
off_diag = 1/abs(n-r)^(1+a) + 1/(2*N)^(1+a) * (hurwitzZeta(1+a, (r-n)/(2*N)) + hurwitzZeta(1+a, -(r-n)/(2*N)) - abs((r-n)/(2*N))^(-(1+a)) * (1 + exp(-1i * (1+a) * pi)));
end