# 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 :

```bash
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

```

```bash
fig = plt.figure(figsize=(8,8))

ax = fig.add_subplot(111, projection='3d')

ax.plot(r[:,0], r[:,1], r[:,2])

ax.set_xlabel("$x$")
ax.set_ylabel("$y$")
ax.set_zlabel("$z$")
plt.show()
```

![SVG Image](https://firebasestorage.googleapis.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LL-TjWvtGhAa4RFZygl%2Fuploads%2FaPK6uMOPVkXbrD7PGtwh%2Ffile.svg?alt=media)

##
