{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PHYS 216 - Problem 5: Proton deflection between charged plates\n",
    "\n",
    "A proton enters a uniform electric field with horizontal speed $v_x=1.5\\times10^7\\,\\mathrm{m/s}$. The field magnitude is $E=4.0\\times10^5\\,\\mathrm{N/C}$, and the plates have length $L=12.0\\,\\mathrm{cm}$. We calculate the proton's downward deflection when it leaves the plates.\n",
    "\n",
    "The upper plate is positive and the lower plate is negative, so $\\vec E$ points downward. Because a proton has positive charge, $\\vec F=q\\vec E$ also points downward."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Physics model\n",
    "\n",
    "Choose $+x$ to the right and $+y$ upward. The electric force has no $x$-component, so the horizontal speed stays constant:\n",
    "\n",
    "$$t=\\frac{L}{v_x}.$$\n",
    "\n",
    "For the vertical motion,\n",
    "\n",
    "$$F_y=qE_y=-eE, \\qquad a_y=\\frac{F_y}{m_p}=-\\frac{eE}{m_p}.$$\n",
    "\n",
    "The proton enters horizontally, so $v_{y0}=0$. Its vertical displacement is therefore\n",
    "\n",
    "$$\\Delta y=\\frac{1}{2}a_y t^2.$$\n",
    "\n",
    "The requested downward distance is $d=-\\Delta y$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Physical constants\n",
    "e = 1.602176634e-19       # proton charge magnitude (C)\n",
    "m_p = 1.67262192369e-27  # proton mass (kg)\n",
    "\n",
    "# Given values\n",
    "E = 4.0e5       # electric-field magnitude (N/C), directed downward\n",
    "v_x = 1.5e7     # initial horizontal speed (m/s)\n",
    "L = 12.0e-2     # plate length (m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Time inside the electric field\n",
    "t_exit = L / v_x\n",
    "\n",
    "# The minus signs indicate the downward (-y) direction\n",
    "F_y = -e * E\n",
    "a_y = F_y / m_p\n",
    "\n",
    "# Vertical displacement and downward deflection distance\n",
    "delta_y = 0.5 * a_y * t_exit**2\n",
    "d = -delta_y\n",
    "\n",
    "print(f\"Time between the plates: {t_exit:.3e} s\")\n",
    "print(f\"Electric force:          {F_y:.3e} N (downward)\")\n",
    "print(f\"Vertical acceleration:   {a_y:.3e} m/s^2 (downward)\")\n",
    "print(f\"Vertical displacement:   {delta_y:.3e} m\")\n",
    "print(f\"Downward deflection:      {d:.3e} m = {d*1e3:.3f} mm\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Trajectory\n",
    "\n",
    "At horizontal position $x$, the elapsed time is $t=x/v_x$. Thus\n",
    "\n",
    "$$y(x)=\\frac{1}{2}a_y\\left(\\frac{x}{v_x}\\right)^2\n",
    "=-\\frac{eE}{2m_pv_x^2}x^2.$$\n",
    "\n",
    "The trajectory is a parabola. The following graph displays the downward distance $d(x)=-y(x)$ as a positive quantity."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.linspace(0, L, 300)\n",
    "time = x / v_x\n",
    "y = 0.5 * a_y * time**2\n",
    "downward_deflection = -y\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(8, 4.8))\n",
    "ax.plot(x * 100, downward_deflection * 1e3, linewidth=2.5, color=\"tab:blue\")\n",
    "ax.scatter([L * 100], [d * 1e3], color=\"tab:red\", zorder=3,\n",
    "           label=f\"Exit: d = {d*1e3:.2f} mm\")\n",
    "ax.set_xlabel(\"Horizontal position x (cm)\")\n",
    "ax.set_ylabel(\"Downward deflection d (mm)\")\n",
    "ax.set_title(\"Proton trajectory in a uniform electric field\")\n",
    "ax.grid(True, alpha=0.3)\n",
    "ax.legend()\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Optional: velocity as the proton leaves the plates\n",
    "v_y_exit = a_y * t_exit\n",
    "v_exit = np.hypot(v_x, v_y_exit)\n",
    "angle_below_horizontal = np.degrees(np.arctan2(-v_y_exit, v_x))\n",
    "\n",
    "print(f\"Exit vertical velocity: {v_y_exit:.3e} m/s\")\n",
    "print(f\"Exit speed:             {v_exit:.3e} m/s\")\n",
    "print(f\"Exit direction:         {angle_below_horizontal:.2f} degrees below horizontal\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Final result\n",
    "\n",
    "$$\\boxed{d\\approx1.2\\times10^{-3}\\,\\mathrm{m}=1.2\\,\\mathrm{mm}\text{ downward}}$$"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}