clear
clc


dx = 0.01;     %points along string
dt = 0.001;    % time step

c = 1;          %speed of wave 
L = 10;         %Length of string
tmax = 10;   %duration simulation

k = c*dt/dx; 
n=L/dx + 1;

Amp = zeros(1,n);
Amp(3/dx:4/dx) = -.5+.5*cos(linspace(0,2*pi,4/dx-3/dx+1)); % depression in string

OldAmp = Amp;

for t=0:dt:tmax
    
    NewAmp(1) = 2*k^2*Amp(2) + 2*(1-k^2)*Amp(1) - OldAmp(1);
    NewAmp(2:n-1) = k^2*(Amp(1:n-2)+Amp(3:n)) + 2*(1-k^2)*Amp(2:n-1)-OldAmp(2:n-1);
    NewAmp(n) = 2*k^2*Amp(n-1) + 2*(1-k^2)*Amp(n) - OldAmp(n);
    
    OldAmp = Amp;
    Amp = NewAmp;
    
    if mod(t/dt, 15)==0
        plot((0:dx:L),Amp,'-o') 
       
        title('reflected wave','fontsize',20);
        axis([0 L -1 1])
        pause(0.001)
    end
end
        
        