(*  Module #19:  Motion of Charges in Electric Fields  *)

(*   Motion of a Charge in a Region of Constant Electric
      Field, for example in a plane capacitor          *)

Clear[Ex,Ey,x,Vx,y,Vy];
x[0]=0.; Vx[0]=3*(10^6); y[0]=0.; Vy[0]=0.; 
Delta=1.0*(10^-9); Steps = 10;
q=1.6*(10^-19);m=9.1*(10^-31); 
Ex=0.; Ey=1000.;               
Do[Fx = q*Ex; Fy = q*Ey;
   Vx[n+1] = Vx[n] + (Fx/m)*Delta;
   Vy[n+1] = Vy[n] + (Fy/m)*Delta; 
   x[n+1] = x[n] + Vx[n+1]*Delta;
   y[n+1] = y[n] + Vy[n+1]*Delta,
   {n,0,Steps}]

(*   Graph of Trajectory of Charged Particle in the
     Capacitor   *)

data = Table[{x[n],y[n]}, {n,0,Steps}];
plot1=ListPlot[data, AxesLabel->{"x","y"}]

(*     Motion of a charge with and without an electric
       field     *)
Clear[Ex,Ey,x,Vx,y,Vy];
x[0]=0.; Vx[0]=3*(10^6); y[0]=0.; Vy[0]=0.; 
Delta=1.0*(10^-9); Steps = 17;
q=1.6*(10^-19);m=9.1*(10^-31); 
Ex=0.; Ey=1000.;               
Do[If[n<=10,Fx = q*Ex; Fy = q*Ey,Fx=0.;Fy=0.];
   Vx[n+1] = Vx[n] + (Fx/m)*Delta;
   Vy[n+1] = Vy[n] + (Fy/m)*Delta; 
   x[n+1] = x[n] + Vx[n+1]*Delta;
   y[n+1] = y[n] + Vy[n+1]*Delta,
   {n,0,Steps}]

(*     Graph of the charge trajectory                *)

data = Table[{x[n],y[n]}, {n,0,Steps}];
plot2=ListPlot[data, AxesLabel->{"x","y"}]

(*    A Non-constant Electric Field                  *)

Clear[Ex,Ey,x,Vx,y,Vy];
Eo=1000.;DeltaX=0.03/10;Steps=10.;
Do[Ey[n]=Eo*(1-n/Steps);
    x[n]=n*DeltaX,
    {n,0,Steps}]
FieldData = Table[{x[n],Ey[n]}, {n,0,Steps}];
ListPlot[FieldData, AxesLabel->{"x","Ey"}]

(*  Motion of a charge in a region with a constant 
    electric field-You must run the previous program 
    to set up the electric field first               *)
    
Clear[Ex,Ey,x,Vx,y,Vy];
x[0]=0.; Vx[0]=3*(10^6); y[0]=0.; Vy[0]=0.; 
Delta=1.0*(10^-9); Steps = 10;
q=1.6*(10^-19);m=9.1*(10^-31); Eo=1000.;                       
Do[Ey[n]=Eo*(1-n/Steps);      
   Fy = q*Ey[n];
   Vx[n+1] = Vx[n];            
   Vy[n+1] = Vy[n] + (Fy/m)*Delta; 
   x[n+1] = x[n] + Vx[n+1]*Delta;
   y[n+1] = y[n] + Vy[n+1]*Delta,
   {n,0,Steps}]

data = Table[{x[n],y[n]}, {n,0,Steps}];
plot3=ListPlot[data, AxesLabel->{"x","y"},PlotJoined->True]