import numpy as np
%pip install matplotlib
import matplotlib.pyplot as plt
Run to view results
#define magntiude in N/C or V/m
E_field_mag = 40e3
#now define the vector
E_field_vec = np.array([0.,-E_field_mag,0.])
Run to view results
#define magntiude in N/C or V/m
E_field_mag = 40e3
#now define the vector
E_field_vec = E_field_mag*np.array([0.,-1.,0.])
Run to view results
##### Begin Editing #####
#define magnitude in T
B_field_mag = 1 * 10 ** -3
#now define the vector
B_field_vec = np.array([0.,0,-B_field_mag])
#define magnitude in #m/s
velocity_mag = 3 * 10 ** 7
#now define the vector
velocity_vec = np.array([velocity_mag,0.,0.])
#define electron charge in C
q_e = -1.6e-19
#define Lorentz force due to the magnetic field
F_B = np.cross(B_field_vec, velocity_vec)
##### End Editing #####
Run to view results
#define electron charge in C
q_e = -1.6e-19
#define the mass of the electron in kg
m_e = 9.11e-31
Run to view results
#Note here we are defining the total Loretnz force, not just the contributions from the B_field
def get_lorentz_force(charge,velocity,Efield,Bfield):
##### Begin Editing #####
F = charge * (Efield + np.cross(velocity, Bfield))
##### End Editing #####
return F
Run to view results
#10cm/v_e characteristic time into
tc = 0.1/3e7
#number of cycles, a larger number divides the time
#smaller steps, which are more accurate but take longer
ncycles = 1e4
#delta_t fom characteristic time and number of cycles
delta_t = tc/ncycles
#intialize time
t=0
##### Begin Editing #####
# use your solution from Prelab Problem 7.1 to fill in these values
myVel = np.array([velocity_mag,0.,0.])
myEfield = E_field_mag*np.array([0.,-1.,0.])
myBfield = np.array([0.,0,-B_field_mag])
##### End Editing #####
#also define the starting position at (0,0,0)
myPos = np.array([0.,0.,0.])
#finally calculate initial force
myForce = get_lorentz_force(q_e,myVel,myEfield,myBfield)
Run to view results
listVel=[]
listPos=[]
listForce=[]
listTime=[]
Run to view results
#add initial values to data container
listVel.append(myVel)
listPos.append(myPos)
listForce.append(myForce)
listTime.append(t)
while t<tc:
t= t+delta_t
#update velocity using acceleration
newVel = myVel+(myForce/m_e)*delta_t
##### Begin Editing #####
#update position using velocity
newPos = myPos + newVel * delta_t
##### End Editing #####
#find new Force based on update velocity
newForce = get_lorentz_force(q_e,newVel,myEfield,myBfield)
#add new values to lists
listVel.append(newVel)
listPos.append(newPos)
listForce.append(newForce)
listTime.append(t)
#finally, increase time step and
#set new values to 'my' values for next loop
myVel = newVel
myPos = newPos
myForce = newForce
Run to view results
#initialize the figure to plot
plt.figure(figsize=[20,20])
xList=[]
yList=[]
for vector in listPos:
xList.append(vector[0])
yList.append(vector[1])
plt.subplot(221)
plt.plot(xList,listTime,'b-') # Time is plotted along the y-axis
plt.plot(yList,listTime,'r.')
plt.title('Position vs Time')
plt.xlabel('Position (m)')
plt.ylabel('Time (s)')
vxList=[]
vyList=[]
for vector in listVel:
vxList.append(vector[0])
vyList.append(vector[1])
plt.subplot(222)
plt.plot(vxList,listTime,'b-')
plt.plot(vyList,listTime,'r.')
plt.title('Velocity vs Time')
plt.xlabel('Velocity (m/s)')
plt.ylabel('Time (s)')
FxList=[]
FyList=[]
for vector in listForce:
FxList.append(vector[0])
FyList.append(vector[1])
plt.subplot(223)
plt.plot(FxList,listTime,'b-')
plt.plot(FyList,listTime,'r.')
plt.title('Force vs Time')
plt.xlabel('Force (N)')
plt.ylabel('Time (s)')
plt.subplot(224)
plt.plot(xList,yList,'k-.')
plt.title('Trajectory of the Particle')
plt.xlabel('X Position (m)')
plt.ylabel('Y Position (m)')
plt.show()
Run to view results