(*     Module #23:   Inductor, Capacitor, and Resistor
       Circuits     *)
       
L = (10^-3); R=4.; Cap=0.1*(10^-6); V=2.0;
omega=1/Sqrt[L*Cap];f=omega/(2*N[Pi]); period=1/f;
Q[0]= Cap*V; i[0]=0.; delta=period/10.; Steps = 100.;
 Do[i[n+1]=i[n]-(R/L)*i[n]*delta-(Q[n]/(Cap*L))*delta;
    Q[n+1]=Q[n]+i[n+1]*delta
   ,{n,0,Steps}]

(*     Make graph of current versus time    *)

data=Table[{n*delta,i[n]},{n,0,Steps}];
currentplot=ListPlot[data,AxesLabel->{"time","i[t]"},
                          PlotJoined->True]

(*     Period of Oscillation     *)

L=(10^-3); Cap=0.1*(10^-6);
T = 2*N[Pi]*Sqrt[L*Cap]

(*   Make graph of charge versus time     *)
chargedata=Table[{n*delta,Q[n]},{n,0,Steps}];
chargeplot=ListPlot[chargedata,AxesLabel->{"time","Q[t]"},
                          PlotJoined->True]

(*   Initial charge on the capacitor     *)

Cap=0.1*(10^-6); V=2.0;
Print["Initial Charge =", Cap*V]

(*     Program with a signal generator      *)

L = (10^-3); R=4.0; Cap=0.1*(10^-6);V0=2.0;
Q[0]=0.; i[0]=0.; omega=1.0*(10^5); period=(2*N[Pi])/omega; 
delta=period/10.; Steps = 150.;  
 Do[i[n+1]=i[n]+
        (V0*Sin[omega*n*delta]-R*i[n]-Q[n]/Cap)*(delta/L);
    Q[n+1]=Q[n]+i[n+1]*delta
   ,{n,0,Steps}]

(*   Make graph of current versus time     *)

idata=Table[{n*delta,i[n]},{n,0,Steps}];
currentplot=ListPlot[idata,PlotJoined->True,
                     AxesLabel->{"time","i[t]"}]

(*     Make graph of voltage versus time     *)
Vdata=Table[{n*delta,i[n]*R},{n,0,Steps}];
currentplot=ListPlot[Vdata,PlotJoined->True,
                     AxesLabel->{"time","v[t]"}]