function U = transport(u_0, J, K)
% central difference scheme 
% u_t + u_x = 0; 0<x<1, 0<t<1
% u(x,0) = u_0(x) 
% J = number of subdivisions in x
% K = number of subdivisions in t

% create anonymous function in matlab workspace:
% u_0 = @(x) sin(2*pi*x)
% then run program, e.g.
% transport(u_0, 20, 20) 


U = zeros(J+1,K+1);
A = zeros(J+1,J+1);
dt = 1/K;
dx = 1/J;
c = dt/(2*dx);

for i = 1:J+1
    if i == 1
        A(i,J) = c;
        A(i,i) = 1;
        A(i,i+1) = -c;
    elseif i == J+1
        A(i,i-1) = c;
        A(i,i) = 1;
        A(i,2) = -c
    else
        A(i,i-1) = c;
        A(i,i) = 1;
        A(i,i+1) = -c;
    end
end

X = 0:dx:1;
U(:,1) = u_0(X);

for k = 2:K+1
    U(:,k) = A*U(:,k-1);
end

%plot the first few iterations
plot(X, U(:,1), X, U(:,2), X, U(:,3))
