I'm trying to solve the following equation $$u_t = u_{xx} + u(1-u^2), u_x(\pm 1) = 0,$$ using the Fourier cosine transform. The nonlinear term gives a convolution which I would rather avoid, which is why I wanted to try a pseudo-spectral method.

In MATLAB I have the following script (simplified as much as possible)

function TestCosineSeries()

Nx = 32;
x = linspace(-1,1,Nx)';
f = tanh(1000*x);

dt = 1e-3;
Nt = 100;

for k = 1 : Nt

    t = k*dt
    [t,y] = ode45(@(t,y)RHS(t,y,L),[0 dt],f);
    f = y(end,:);



function duout = RHS(t,u,L)

N = length(u);

uhat = fft(u);
kappa = fftshift((-N/2:N/2-1)');
DDuhat = -pi^2*kappa.^2.*uhat;

DDu = ifft(DDuhat);

duout = DDu + (1-u.^2).*u;


I imagine I'm doing something wrong in using the cosine series (maybe I can't use FFT). Any help would be appreciated!

  • $\begingroup$ When debugging your own code like this, it's easiest to start with a simpler testcase first, which you can get, for example, by the method of manufactured solutions. It's possible that you made a mistake in how you call fft: have you tried writing out on paper, in full, all the transformations that are you are doing and making sure they are right? What does the output look like, can you please include it for people to see? Does it at least satisfy the boundary conditions, as it should? Make sure your fft can calculate derivatives correctly for arbitrary polynomial $u$ before using it on ODEs. $\endgroup$ – Kirill Jul 20 '17 at 19:55

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.