clear
clc


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

c = 5;          %speed of wave 
L = 10;         %Length of string
tmax = 10;   %duration simulation
k = c*dt/dx; 
n=L/dx + 1;

Amp = 0.5-0.5*cos(2*pi/L*[0:dx:L]);  %initial amplitude
AmpOld = Amp;                        %updating amplitude each duration

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