dx = .01 ; % Spacing o f p o int s on s t r i n g dt = .001 ; % Si z e o f t ime s t e p c = 5 ; % Speed of wave propagation L = 10 ; % Length o f string stopTime = 10; % Time to run the simulation r = c*dt/dx ; n = L/dx + 1 ; % Set current and past to the graph of a plucked string current = .5-.5*cos (2*pi /L * [0:dx:L] ); past = current ; for t=0:dt:stopTime % Calculate the future position of the string future( 1 ) = 0 ; future( 2 : n-1) = r^2*( current(1:n-2)+current(3:n)) + 2*(1-r^2)* current( 2:n-1) - past(2:n-1); future(n) = 0 ; % Set things up for the next time step past = current; current = future; % Plot the graph after every 10 th frame if mod(t/dt ,10) == 0 plot([ 0 : dx :L] , current,'r','linewidth',4 ) axis ([0 L -2 2]) pause ( .001 ) end end