% Script for solving u'' = 1/(1-0.6cos(2*pi*x)) - 5/4, 1-periodic BCs % using Fourier spectral method. % Solution is plotted % for N = 2, 4 8, 16 clf subplot(2,2,1) color = 'rgbk' L = 1; pmax = 4; pintp = 5; for p = 1:pmax N = 2^p; h = L/N; k = [0:(N/2-1) (-N/2):(-1)]*2*pi/L; k(1) = 1e-6; % Redefine k(1) nonzero to prevent 0/0 division later x = (0:(N-1))*h; % These are only the grid points covering [0 1] % Define right hand side over extended domain f = 1./(1-0.6*cos(2*pi*x)) - 5/4; % DFT, solve, and inverse DFT fhat = fft(f); fhat(1) = 0; u = real(ifft(-fhat./k.^2)); uintp = interpft(u,2^pintp); plot(x,u,['x' color(p)],(0:2^pintp - 1)/2^pintp,uintp,['-' color(p)]) if (p==1) xlabel('x') ylabel('u') title('Solution') hold on end end legend('N=2',' ','N=4',' ','N=8',' ','N=16',' ') hold off