(*     Module #9:     Harmonic Motion of a Mass on a Spring *)

(*  Graph of Hooke's law:  data points    *)
ListPlot[{{0,0},{0.5,9.8},{1.0,19.6},{1.5,29.4},{2.0,39.2}},
    AxesLabel->{"x","Weight"},AxesOrigin->{0,0}]

(*  Graph of Hooke's law: line graph    *)
ListPlot[{{0,0},{0.5,9.8},{1.0,19.6},{1.5,29.4},{2.0,39.2}},
    AxesLabel->{"x","Weight"},AxesOrigin->{0,0},PlotJoined->True]

(*    Sample calculation of the spring constant   *)
k=(19.6-9.8)/(1.0-0.5)

(*     Graph of Spring force law     *)
k=19.6;
Plot[F=-k*x,{x,-2,2},AxesLabel->{"x","Spring Force"}]

(*     Program for the calculation of the motion of the
       harmonic oscillator     *)
m = 2.; k=19.6;
ti=0.;tf=6.0; delta=0.1; 
x[ti]=0.1; v[ti]=0.;
Do[ F[t] = -k*x[t];
    v[t+delta] = v[t] + (F[t]/m)*delta;
    x[t+delta] = x[t] + v[t+delta]*delta,
       {t,ti,tf,delta}]

(*     Make graph of position versus time    *)
xdata = Table[{t,x[t]},{t,ti,tf,delta}];  
xplot = ListPlot[xdata,AxesLabel->{"t","x[t]"}]

(*     Make a graph of the velocity versus time   *)
vdata = Table[{t,v[t]},{t,ti,tf,delta}];  
vplot=ListPlot[vdata,AxesLabel->{"t","v[t]"},
                   PlotJoined->True]

(* Make combined graph of x vs. time and v versus time *)
Show[xplot,vplot]

(*    Program for exercise #1     *)
m = 2.; k=19.6; x[0.]=0.1; v[0.]=0.;
delta=0.05; TotalTime = 2.1;
Do[ F[t] = -k*x[t];
    v[t+delta] = v[t] + (F[t]/m)*delta;
    x[t+delta] = x[t] + v[t+delta]*delta;
    Print[{t,x[t]}],
       {t,0.,TotalTime,delta}]

(*     Program for exercise #6   *)
m = 2.; k=19.6; g=9.8;
ti=0.;tf=2.1;delta=0.05;
x[ti]=0.1; v[ti]=0.;
Do[ F[t] = -k*x[t]+m*g;
    v[t+delta] = v[t] + (F[t]/m)*delta;
    x[t+delta] = x[t] + v[t+delta]*delta,
       {t,ti,tf,delta}]