{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5b401620",
   "metadata": {},
   "source": [
    "# PHYS 216 — Assignment 1, Problem 3\n",
    "\n",
    "## Uniformly charged circular arc\n",
    "\n",
    "An arc of radius $R$ lies in the $xy$-plane, carries total charge $Q$, and runs from $\\phi=\\pi/4$ to $3\\pi/4$. Its angular width is $\\Delta\\phi=\\pi/2$, so\n",
    "\n",
    "$$\\lambda=\\frac{Q}{R\\Delta\\phi}=\\frac{2Q}{\\pi R},\\qquad dq=\\lambda R\\,d\\phi=\\frac{2Q}{\\pi}d\\phi.$$\n",
    "\n",
    "At the point $(0,0,z)$, a source element is at $(R\\cos\\phi,R\\sin\\phi,0)$ and\n",
    "\n",
    "$$d\\mathbf E=k\\frac{dq}{(R^2+z^2)^{3/2}}[-R\\cos\\phi\\hat{\\mathbf i}-R\\sin\\phi\\hat{\\mathbf j}+z\\hat{\\mathbf k}].$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b425916",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-19T00:51:11.931398Z",
     "iopub.status.busy": "2026-09-19T00:51:11.931398Z",
     "iopub.status.idle": "2026-09-19T00:51:12.452283Z",
     "shell.execute_reply": "2026-09-19T00:51:12.452283Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "k = 8.9875517923e9  # N m^2 / C^2\n",
    "\n",
    "def analytic_axis_field(Q, R, z):\n",
    "    denom = (R**2 + z**2)**1.5\n",
    "    return np.array([\n",
    "        0.0,\n",
    "        -(2*np.sqrt(2)/np.pi) * k*Q*R / denom,\n",
    "        k*Q*z / denom,\n",
    "    ])\n",
    "\n",
    "def numerical_axis_field(Q, R, z, n=200_001):\n",
    "    phi = np.linspace(np.pi/4, 3*np.pi/4, n)\n",
    "    dq_dphi = 2*Q/np.pi\n",
    "    denom = (R**2 + z**2)**1.5\n",
    "    integrand = np.vstack((\n",
    "        -R*np.cos(phi),\n",
    "        -R*np.sin(phi),\n",
    "        np.full_like(phi, z),\n",
    "    )) * (k*dq_dphi/denom)\n",
    "    return np.trapezoid(integrand, phi, axis=1)\n",
    "\n",
    "Q, R, z = 1.0e-9, 0.25, 0.15\n",
    "Ea = analytic_axis_field(Q, R, z)\n",
    "En = numerical_axis_field(Q, R, z)\n",
    "print('Analytic E [Ex, Ey, Ez] =', Ea, 'N/C')\n",
    "print('Numeric  E [Ex, Ey, Ez] =', En, 'N/C')\n",
    "print(f\"Relative difference = {np.linalg.norm(Ea-En)/np.linalg.norm(Ea):.3e}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01bf6e76",
   "metadata": {},
   "source": [
    "## Closed-form result\n",
    "\n",
    "The $x$-components cancel pairwise because the arc is symmetric about the $y$-axis. Every source element lies in the $xy$-plane, so at the centre ($z=0$) there is no $z$-component.\n",
    "\n",
    "At the centre,\n",
    "\n",
    "$$\\mathbf E(0,0,0)=-\\frac{2\\sqrt2}{\\pi}\\frac{kQ}{R^2}\\hat{\\mathbf j}.$$\n",
    "\n",
    "Along the axis,\n",
    "\n",
    "$$\\boxed{\\mathbf E(0,0,z)=\\frac{kQ}{(R^2+z^2)^{3/2}}\\left(-\\frac{2\\sqrt2R}{\\pi}\\hat{\\mathbf j}+z\\hat{\\mathbf k}\\right)}.$$\n",
    "\n",
    "The negative $y$ component points away from the positively charged arc, while the positive $z$ component points toward the observation point above the page."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48380e2e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-19T00:51:12.455290Z",
     "iopub.status.busy": "2026-09-19T00:51:12.454298Z",
     "iopub.status.idle": "2026-09-19T00:51:12.720617Z",
     "shell.execute_reply": "2026-09-19T00:51:12.720617Z"
    }
   },
   "outputs": [],
   "source": [
    "u = np.linspace(0, 5, 500)  # u = z/R\n",
    "Ey = -(2*np.sqrt(2)/np.pi)/(1+u**2)**1.5\n",
    "Ez = u/(1+u**2)**1.5\n",
    "Emag = np.hypot(Ey, Ez)\n",
    "\n",
    "plt.figure(figsize=(8, 5))\n",
    "plt.plot(u, Ey, lw=3, color='#E50024', label=r'$E_y/(kQ/R^2)$')\n",
    "plt.plot(u, Ez, lw=3, color='#005FBE', label=r'$E_z/(kQ/R^2)$')\n",
    "plt.plot(u, Emag, '--', lw=2.5, color='#009B77', label=r'$|E|/(kQ/R^2)$')\n",
    "plt.axhline(0, color='black', lw=0.8)\n",
    "plt.xlabel('z/R')\n",
    "plt.ylabel('normalized component')\n",
    "plt.title('Charged arc: field components on the z-axis')\n",
    "plt.grid(alpha=0.3)\n",
    "plt.legend(frameon=False)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd11c2b4",
   "metadata": {},
   "source": [
    "## Direction of the field\n",
    "\n",
    "The field begins entirely in the $-y$ direction at the centre. Moving above the page creates a positive $z$ component. Very far away, the whole arc looks increasingly like a point charge and the field points mostly along $+z$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07470488",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-19T00:51:12.723012Z",
     "iopub.status.busy": "2026-09-19T00:51:12.722013Z",
     "iopub.status.idle": "2026-09-19T00:51:12.803965Z",
     "shell.execute_reply": "2026-09-19T00:51:12.802957Z"
    }
   },
   "outputs": [],
   "source": [
    "# Direction angle measured from +z toward -y\n",
    "angle_deg = np.degrees(np.arctan2(np.abs(Ey), np.maximum(Ez, 1e-15)))\n",
    "plt.figure(figsize=(8, 4.5))\n",
    "plt.plot(u, angle_deg, color='#002F60', lw=3)\n",
    "plt.xlabel('z/R')\n",
    "plt.ylabel('tilt toward −y (degrees)')\n",
    "plt.title('The field rotates toward +z as the observer moves away')\n",
    "plt.ylim(0, 95); plt.grid(alpha=0.3)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}