# Solving equations of motion for a particle in magnetic and electric field

In the following we discuss the solutions for the equation of motion for a particle in a magnetic and electric field

\begin{equation}
  m\mathbf{\ddot{r} } = q\mathbf{E} + \frac{q}{c} \mathbf{\dot{r}} \times \mathbf{B}
\end{equation}

governed by the Lorentz Force. $\mathbf{B} = (0, 0, B_z)$, $\mathbf{E} = (0, E_y, E_z)$ and the initial conditions $\mathbf{r} = (x_0, y_0, z_0)$, $\mathbf{v_0} = (v_{0x}, v_{0y}, v_{0z})$.

## Import libraries


```python
import numpy as np
import matplotlib.pyplot as plt
```

## Define the solutions of the problem

The following solution for the movement in the x-y plane can be derived by solving above differential equation:

\begin{split}
  x(t) &= x_0 + \frac{E_z c}{B_z}t + \left(v_{0x} - \frac{E_z c}{Bz}\right) \frac{cm}{qB_z}\sin{\frac{qB_z}{cm}t} \\
  y(t) &= y_0 + \left(v_{0x} - \frac{E_z c}{Bz}\right) \frac{cm}{qB_z} \left(\cos{\frac{qB_z}{cm}t} - 1\right) \\
  z(t) &= 0
\end{split}


```python
def x_t(t, x_0, v_0x, E_y, B_z, q, c, m):
    return x_0 + ((E_y * c) / B_z) * t + (v_0x - (E_y * c) / B_z) * ((c * m) / (q * B_z)) * np.sin(((q * B_z) / (c * m)) * t)
```


```python
def y_t(t, y_0, v_0x, E_y, B_z, q, c, m):
    return y_0 + ((v_0x - (E_y * c) / B_z) * ((c * m) / (q * B_z))) * (np.cos(((q * B_z) / (c * m)) * t) - 1)
```

### Define initial conditions


```python
x_0 = 0
y_0 = 0

v_0x = 0

E_y = 1 
B_z = 1 

c = 1# 299792458
m = 1# np.exp(-31)
q = 1#1.6 * np.exp(-19)

t = 0
```

### Solve and plot


```python
t_start = 0
t_end = 200
t = np.linspace(0, t_end, 1000)

x = x_t(t, x_0, v_0x, E_y, B_z, q, c, m)
y = y_t(t, y_0, v_0x, E_y, B_z, q, c, m)
```


```python
plt.figure(figsize=(15,10))

plt.subplot(211)

plt.title('Solutions of the x(t) and y(t) coordinates')

plt.ylabel('$x(t)$ in m')
plt.xlim(xmin=t_start, xmax=t_end)
plt.plot(t, x)

plt.subplot(212)

plt.ylabel('$y(t)$ in m')
plt.xlabel('$t$ in s')
plt.xlim(xmin=t_start, xmax=t_end)
plt.plot(t, y)
```

37bfbf5c94960268321375e1369c4681dd18a709
ipynb
Jupyter Notebook
DynamicSystems/MotionEquations_MagneticElectricField/MotionEquations_MagneticElectricField.ipynb
Progklui/physicsProjects
158481bd38c046a1ca6217dab65f9e80763ead10
MIT
DynamicSystems/MotionEquations_MagneticElectricField/MotionEquations_MagneticElectricField.ipynb
Progklui/physicsProjects
158481bd38c046a1ca6217dab65f9e80763ead10
MIT
DynamicSystems/MotionEquations_MagneticElectricField/MotionEquations_MagneticElectricField.ipynb
Progklui/physicsProjects
158481bd38c046a1ca6217dab65f9e80763ead10
MIT
Qwen/Qwen-72B
1. YES
2. YES