(*     Module #22:  Resistor and Capacitor Circuits  *)

(*   Case where the charge builds up on a capacitor *)

Q[0]=0.; R=1.0*(10^6); Cap=5.5*(10^-6); Vb=2.0;
Delta=0.2; Steps = 100.;
 Do[Q[n+1]=Q[n]+(Vb/R)*Delta-(Q[n]/(R*Cap))*Delta
   ,{n,0,Steps}]

(*    Make graph of charge versus time   *)

data=Table[{n*Delta,Q[n]},{n,0,Steps,10}];
ListPlot[data,AxesLabel->{"time","Q[t]"}]

(*     Definition of the current   *)

Current[n_] := (Q[n+1]-Q[n])/Delta

(*     Make graph of current versus time   *)

CurrentData=Table[{n*Delta,Current[n]},{n,0,Steps,10}];
ListPlot[CurrentData,AxesLabel->{"time","I[t]"}]

(*    Here's the case you examine in lab, where the charge on a capacitor descreases
       due to current flow through the resistor    *)
       
Q[0]=1.0*(10^-5); R=1.0*(10^6); Cap=5.5*(10^-6);
Delta=0.2; Steps = 100.;
 Do[Q[n+1]=Q[n]-(Q[n]/(R*Cap))*Delta
   ,{n,0,Steps}]

(*     Make graph of charge versus time   *)

data=Table[{n*Delta,Q[n]},{n,0,Steps,10}];
ListPlot[data,AxesLabel->{"time","Q[t]"}]

(*      Program for Exercise #1     *)

Q[0]=0.; R=5.0*(10^6); Cap=5.5*(10^-6); V=2.0;
Delta=2.; Steps = 300.;
 Do[Q[n+1]=Q[n]+(V/R)*Delta-(Q[n]/(R*Cap))*Delta
   ,{n,0,Steps}]
data=Table[{n*Delta,Q[n]},{n,0,Steps,10}];
ListPlot[data,AxesLabel->{"time","Q[t]"}]

(*     Program for Exercise #2      *)

Q[0]=0.; R=5.0*(10^6); Cap=5.5*(10^-5); V=2.0;
Delta=2.; Steps = 300.;
 Do[Q[n+1]=Q[n]+(V/R)*Delta-(Q[n]/(R*Cap))*Delta
   ,{n,0,Steps}]
data=Table[{n*Delta,Q[n]},{n,0,Steps,10}];
ListPlot[data,AxesLabel->{"time","Q[t]"}]