function [] = linear_advection()

Nx = 50;
dx = 1/Nx;
x = [1:Nx]'*dx;

u0 = sin(2*pi*x);


T = 1;
Nt = 50;
dt = T/Nt;

u = u0;

for nt = 1:Nt
    % centered differencing
    dudx = ([u(2:end);u(1)] - [u(end);u(1:end-1)])/2/dx;
    
    % downwind
    % dudx = ([u(2:end);u(1)]-u)/dx;
    
    % upwind
    %dudx = (u - [u(end);u(1:end-1)])/dx;
    
    % forward Euler update
    u = u - dt*dudx;
    
    figure(1)
    plot(x,u,'b',x,exact(nt*dt,x),'r:');
    title(['t = ',num2str(nt*dt)]);
    legend('approx','exact',...
           'location','EastOutside');
    axis([0 1 -1.5 1.5])
    xlabel('x'); ylabel('u');
    pause(0.1);
end

function [sol] = exact(t,x);
sol = sin(2*pi*(x-t));
