clear
clc


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

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

Amp = zeros(1,n);     %  initial amplitude is zero 
OldAmp = zeros(1,n);  %  old amplitude is also zero

for t=0:dt:tmax
    
    NewAmp(1) = sin(2*t);
    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) = sin(3*t);
    
    OldAmp = Amp;
    Amp = NewAmp;
    
    if mod(t/dt, 15)==0
        plot((0:dx:L),Amp,'-o')
        title('oscilating','fontsize',20);
        axis([0 L -8 8])
        pause(0.001)
    end
end
        
        