% Inputs for the linear convection problem
L = 1;
n = 20,40,80,160;
c = 1;
dt = 1e-3;
% Calculations
x = linspace(0,L,n);
dx = x(2)-x(1);
%dx = L/(n-1)
%Define the start and end position for the square wave
% x_start = 0.1
% x_end = 0.3
% n_start = f(x_start)
% n_end = f(x_end)
n_start = 3;
n_stop = 5;
%Initialize arrays and apply boundary conditions
u = 0.1*ones(1,n);
u(n_start:n_stop) = 0.2;
% BC
u(1) = 0.1;
uold = u;
u_initial = u;
nt = 10
% Time loop
for k = 1:nt
% spaceloop
% du/dt = -cdu/dx
% du/dx - FDS,BDS
for i = 2:n
u(i) = uold(i) - (c*dt/dx)*(uold(i) - uold(i-1));
end
% update old velocities
uold = u;
% plotting the velocities
plot(x,u_initial,\'r\')
hold on
plot(x,u,\'b\')
axis([0 1 0 0.2])
pause(0.3)
end