Solving differential equation using FFT: dealing with zero frequencies

1.3k Views Asked by At

To get started using spectral methods to solve differential equations I am currently using Matlab and its FFT library.

I have successfully approximated a first derivative of a function using the following Matlab code(based on this):

a = 0; %left end of the domain
b = 1; %right end of the domain
L = b-a; %length of the domain
N = 8; %Number of points
dx = L/N;
x = a + dx*(0:N-1);
k = 2*pi/L*[0:N/2-1, 0, -N/2+1:-1];

%% Exact solution
u = -4 * x.^2 + 4 * x;
du = -8 * x + 4;

%% Approximate solution
fftu = fft(u);
dffft = 1i*k.*fftu;
du_appr = ifft(dffft);

%% Plot
plot(x, du_appr)
hold on
plot(x, du)
legend('Approximate solution', 'Exact solution')

This works well for increasing N.

However, now imagine I would like to solve the differential equation:

$u_x = f(x)$

Where:

$f(x) = -8x + 4$

I would expect we can use the same idea but now inverted:

fftdu = fft(du);
u_h = fftdu./(1i*k);
u_appr = ifft(u_h);

As the frequency vector $k$ contains zeros this does not lead to a valid solution.

I am aware that normally the $0^{th}$ frequency bin represents the average of the signal, but I don't know how I could use that to solve my problem.

My question is therefore: How does one solve a simple differential equation using FFT?

All help is greatly appreciated.