u=zeros(1,100); % u is the wave displacement vu=zeros(1,100); % vu is the velocity of the string % here would be the initial conditions and initial velocities,,,, dx=1; % each x step is 1 meter v=10; % wave velocity is 10 m/s dt=0.05; % timestep is 0.01 seconds h=plot(u) axis([1,100,-1,1]) t=0; while 1 % do it until someone hits cntrl-c t=t+dt au=zeros(1,100); % au will be the acceleration vector oldu=u; % save the previous value of u % here's where we would add any forcing, by forcing u(1) to be % a particular value for i=2:99 % step through all of the spatial steps au(i)=v^2/dx^2*(u(i-1)-2*u(i)+u(i+1)); % second spatial derivative end vu=vu+dt*au; % v=v0+a*t u=u+vu*dt+au*dt*dt/2; % s=1/2at^2+vt set(h,'YData',u,'Color',[0,0,1]) % overplot a blue line set(h,'Color',[1,0,0]) % overplot a red line drawnow end end