I am working in Matlab to compute partial derivatives and a Laplacian using Fourier Transforms. I prefer to take spectral derivatives, since it corresponds to multiplication by a wave number, however, I think my code is off since a large bulk of my data after computation gets sent to zero. Here is my code,

K = 16; KT = 2*K;
dx = 2*pi/KT;
x = 0:dx:2*pi-dx;

% build spectral derivatives and laplacian
Dxk = 1i*ones(KT,1)*[0:K -K+1:-1]; 
Dyk = 1i*[0:K -K+1:-1]'*ones(1,KT); 

D2x = Dxk.^2; D2y = Dyk.^2;
Imat = eye(KT,KT);

% creates derivatives using a kronecker product
Dx = kron(Dxk,Imat); Dy = kron(Imat,Dyk);   

% Lap = kron(D2x,Imat) + kron(Imat,D2y);

[xx,yy] = meshgrid(x,x);
f = fft2(sin(xx).*cos(yy));
f_x = Dx.*f(:);
f_x = real(ifft2(reshape(f_x,KT,KT)));


However, my derivatives end up looking like the following, Fourier partial wrt x. Thanks in advance for the help!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.