% Number of cells and timesteps
nx = 100;
nt = 5;

% Size of total domain
width = 100;

% Size of each cell
dx = width / nx;

% Wave speed
c = 1.0;

% Initial heat distribution
u0 = zeros(1, nx);
u0(50) = 1;
u0(51) = 0.5;
u0(49) = 0.5;
u1 = u0;
u2 = u0;

% (Center) position of each cell
x = linspace(0.5*dx, width-0.5*dx, nx);

% Maximum size of timestep (according to CFL)
cfl = 0.8;
dt = cfl*dx*dx/(2*c);

% Plotting help
figure('units','normalized','outerposition',[0 0 1 1])
simulated_graph = plot(x, u2, '.:');
simulated_graph.YDataSource = 'u2';
axis([0, width, -max(u0), max(u0)]);
legend('Pressure');
title('Linear wave equation in 1D');

% Loop over time for nt timesteps
for j=0:nt
    
    % Loop over all the internal cells
    for i=2:nx-1
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % Dette er linjen du må endre på %
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        u2(i) = u1(i);
    end
    
    % Set reflective boundary conditions
    u2(1) = u2(2);
    u2(end) = u2(end-1);
    
    % Rotate / swap the data
    u0 = u1;
    u1 = u2;
    
    % Update plot
    refreshdata;
    drawnow;
    pause(0.01);
end



