Python Tutorials II

Motion of a charge particle in a slowly changing magnetic field

In this tutorial we are going to see how to ray-trace a particle moving in a slowly changing magnetic field :

from mpl_toolkits.mplot3d import Axes3D

q = 1.0
m = 10.0
dt = 1e-3
t0 = 0
t1 = 10
t2 = 20


t = np.linspace(t0, t2, int((t2 - t0)/dt))
n = len(t)

r = np.zeros((n,3))
v = np.zeros((n,3))

#Initial conditions

r[0] = [0.0, 0.0, 0.0]
v[0] = [2.0, 0.0, 3.0]

#B = array([0.0, 0.0, 5.0])

B = np.zeros((n,3))
B[0] = np.array([0.0, 0.0, 4.0])
dB = np.array([0.0, 0.0, 5e-3])
for i in range(n-1):
    a   = q/m* np.cross(v[i],B[i]) 
    v[i+1] = v[i] + a*dt
    r[i+1] = r[i] + v[i+1]*dt
    B[i+1] = B[i] + dB
SVG Image

Last updated