(*    Module #14:  Wave Motion     *)
(*    An Initial Condition for the System of 50 masses  
      This Initial Condition Program must be run before 
      the Simulation Program below.                                   *)

Clear[x,v,a]
Num=50.;                        
Do[x[n]=N[0.1*Sin[2*Pi*(n/(Num+1))]];
   v[n]=0.0,{n,1.,Num}]         
data=Table[{n,x[n]},{n,1.,Num}];
ListPlot[data]

(*     Simulation Program of Wave Motion     *)

k=10.; m=1.0; delta=0.25; t=0.0; Steps=56.; z=0.; 
Do[ a[1.]=-(k/m)*(x[1.]-x[2.])-(k/m)*x[1.]; 
    a[Num]=-(k/m)*x[Num]-(k/m)*(x[Num]-x[Num-1]);
    Do[a[n]=-(k/m)*(x[n]-x[n+1])-(k/m)*(x[n]-x[n-1]), 
       {n,2.,Num-1}];             
    Do[v[n]=v[n]+a[n]*delta;  
       x[n]=x[n]+v[n]*delta,
       {n,1.,Num}];
          If[z<4,z=z+1,data=Table[{n,x[n]},{n,1.,Num}];
          ListPlot[data,PlotRange->{-0.1,0.1}];z=0],
{p,0.,Steps*delta,delta}]

(*   Initial Condition for Exercise #1     *)
Clear[x,v,a]
Num=50.;                        
Do[x[n]=N[0.1*Sin[4*Pi*(n/(Num+1))]];
   v[n]=0.0,{n,1.,Num}]         
data=Table[{n,x[n]},{n,1.,Num}];
ListPlot[data]

(*     Pulse Initial Condition for Exercise #2     *)
Clear[x,v,a]
Num=50.;                        
Do[x[n]=1.0;v[n]=0.0,{n,0.,5.}]      
   Do[x[n]=0.;v[n]=0.0,{n,6.,Num}]      
data=Table[{n,x[n]},{n,1.,Num}];
ListPlot[data,PlotRange->{-1.0,1.0}]

(*     Simulation Program for Pulse Initial Condition *)
k=1.;m=0.1;delta=1.; t=0.0; Steps=30.;
Do[a[1.]=-k*(x[1.]-x[2.])-k*x[1.]; 
   a[Num]=-k*x[Num]-k*(x[Num]-x[Num-1]);
    Do[a[n]=-k*(x[n]-x[n+1])-k*(x[n]-x[n-1]), 
      {n,2.,Num-1}];      
    Do[v[q]=v[q]+a[q]*delta;
       x[q]=x[q]+v[q]*delta,{q,1.,Num}];
    data=Table[{r,x[r]},{r,1.,Num}];
    ListPlot[data,PlotRange->{-1.,1.}],
    {p, 0., Steps*delta, delta}]