% Domain Definition
N = 128;
PhysicalDomain = [0, 2*pi];                     % Physical Domain scaled to a 2*pi interval
h = (PhysicalDomain(2) - PhysicalDomain(1))/N;

x = linspace(h,2*pi,N);
k = [0:N/2-1 0 -N/2+1:-1];

dt = 0.001;
tf = 5;
t = 0:dt:tf;

% System Definition
% int the form u_t = G(u)
nu = 2;
uinicial = exp(-0.5*(x-pi).^2); 
u = zeros(length(t),length(x));
u(1,:) = uinicial;

% Fator integrante
v = @(t) exp(nu.*(k.^2).*t);    
U = fft(u);
U(1,:) = U(1,:)./v(0);

G = @(t,U) -0.5i.*k.*exp(nu.*(k.^2).*t).*fft(real(ifft(exp(-nu.*t.*(k.^2).*U)).^2));


for i = 1:N-2
    
    k1 = G(t(i),U(i,:));

    U(i+1,:) = U(i,:) + (dt).*(k1);
    u(i+1,:) = real(ifft(v(t(i+1)).*U(i+1,:)));
    
end

waterfall(x,t,u)
xlabel('x')
ylabel('t')
zlabel('u')