{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "1dd847b1-0ad5-4f0f-b22b-8c68142dcbe3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.optimize import curve_fit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "657cc2c7-920b-46e0-a7a8-ef65146ddf4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "I_names = np.array([13, 24, 35, 46, 57, 68, 79, 90, 101])\n",
    "I_values = I_names * 1.5 / 155# ток в мА"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "61022dfd-06b7-43cb-ac1f-2128f46ff7ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "colors = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cab0ae39-aeb0-425a-a38a-8491552135fe",
   "metadata": {
    "tags": []
   },
   "source": [
    "### График зависимости индукции от силы тока в обмотках магнита $B = f(I_\\text{М})$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "4c853de8-c61e-4ce0-b796-ffaa6e697b12",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df = pd.read_csv(rf'data/induction_on_amperage.csv')\n",
    "\n",
    "B = np.array(df['Ind[mT]'])\n",
    "I_m = np.array(df['I[mA]'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "576974ec-5d4d-42c1-9280-7a7f9b74a6be",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 68484 characters]\n",
      "text/plain": [
       "<Figure size 1800x1200 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(6, 4), dpi=300)\n",
    "\n",
    "# plt.minorticks_on()\n",
    "# plt.grid(which='minor', linestyle=':', color='0.9')\n",
    "plt.grid(which='major', linestyle='-', color='0.8', lw=0.3)\n",
    "\n",
    "plt.errorbar(I_m, B, xerr=5+0.003*I_m, yerr=0.5+0.003*B, lw=1, ls='', marker='.', markersize=8, label=r\"$B = f(I_М)$\", ds='default')\n",
    "    \n",
    "plt.xlabel(r\"$I_М$, мА\")\n",
    "plt.ylabel(r\"$B$, мТл\")\n",
    "\n",
    "plt.legend()\n",
    "plt.savefig(rf\"images/BonI.png\", facecolor=\"white\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9e18b07-8656-49b4-bf8e-1c2371bee3e5",
   "metadata": {},
   "source": [
    "### Семейство характеристик $\\varepsilon_\\text{х} = f(B)$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "fadcfd44-216a-4449-9e75-398c8d3c740c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def B_approx(x):\n",
    "    for i, xi in enumerate(I_m):\n",
    "        if xi >= x:\n",
    "            x1 = I_m[i - 1]\n",
    "            x2 = I_m[i]\n",
    "            y1 = B[i - 1]\n",
    "            y2 = B[i]\n",
    "            return y1 + (y2-y1)/(x2-x1)*(x-x1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "e6741806-03fb-487f-8366-bbb6c825c49b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def line(x, arg1, arg2):\n",
    "    return x * arg1 + arg2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "id": "08c7f31f-1c40-4904-b619-91c227f7a6b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 291468 characters]\n",
      "text/plain": [
       "<Figure size 1800x1200 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(6, 4), dpi=300)\n",
    "plt.grid(which='major', linestyle='-', color='0.8', lw=0.3)\n",
    "\n",
    "dE_dB = []\n",
    "\n",
    "for i, I_name in enumerate(I_names):\n",
    "    df = pd.read_csv(rf\"data/VoA_{I_name}.csv\")\n",
    "    \n",
    "    U_ = df[\"U_34[mV]\"]\n",
    "    U0 = U_[0]\n",
    "    U = 1000*(U0 - U_)\n",
    "    \n",
    "    I_ = df[\"I_m[mA]\"]\n",
    "    B_ = np.array([B_approx(x) for x in I_], dtype=np.float64)\n",
    "    \n",
    "    popt, pcov = curve_fit(line, B_, U)\n",
    "    \n",
    "    # print(f\"k = {popt[0]:.4f} \\t \")\n",
    "    dE_dB.append(popt[0]/1000)\n",
    "    \n",
    "    plt.errorbar(B_, U, xerr=1+0.003*B_, yerr=1+0.003*U, ls='', marker='.', markersize=4, label=r\"$I_{||} = $\"+rf\"${I_values[i]:.2f}\\pm{1.5/155:.2f}$ мА\", ds='default', color=colors[i])\n",
    "    plt.plot(B_, line(B_, popt[0], popt[1]), color=colors[i], lw=1)\n",
    "\n",
    "dE_dB = np.array(dE_dB) * 1000\n",
    "    \n",
    "plt.xlabel(r\"$B$, мТл\")\n",
    "plt.ylabel(r\"$\\varepsilon_x$, мкВ\")\n",
    "\n",
    "plt.legend()\n",
    "plt.savefig(rf\"images/UonB.png\", facecolor=\"white\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed4c8024-e86f-4672-bd64-1d5d8e596592",
   "metadata": {},
   "source": [
    "### Зависимость $k = f(I)$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "id": "0d76d070-47ce-4f4f-9342-566a1107abab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 103668 characters]\n",
      "text/plain": [
       "<Figure size 1800x1200 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(6, 4), dpi=300)\n",
    "\n",
    "plt.grid(which='major', linestyle='-', color='0.8', lw=0.3)\n",
    "\n",
    "popt, pcov = curve_fit(line, I_values, dE_dB)\n",
    "\n",
    "k = popt[0]\n",
    "\n",
    "plt.errorbar(I_values, dE_dB, xerr=0.01, yerr=3.15e-03, color=colors[0], lw=1, ls='', marker='.', markersize=4, label=r\"$\\dfrac{d\\varepsilon_x}{dB} = f(I_{||})$\", ds='default')\n",
    "plt.plot(I_values, line(I_values, popt[0], popt[1]), color=colors[0], lw=1)\n",
    "\n",
    "plt.xlabel(r\"$I_М$, мА\")\n",
    "plt.ylabel(r\"$d\\varepsilon_x/dB$, мВ/Тл\")\n",
    "\n",
    "plt.legend()\n",
    "plt.savefig(rf\"images/KonI.png\", facecolor=\"white\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "id": "2d140d8b-b9fb-48b7-88fd-ccd862fc794f",
   "metadata": {},
   "outputs": [],
   "source": [
    "h = 2.2e-3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "id": "deb90002-f303-4b6b-ad90-e3c83d3ab41b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0004318927697950297"
      ]
     },
     "execution_count": 189,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "R_h = k*h\n",
    "R_h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 190,
   "id": "8731ed11-0b5c-4304-9ab3-eb563dbe33e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "q = 1.6e-19"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "id": "048587bd-6a59-449c-a65c-86f810f3ce4c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.44711846020626e+22"
      ]
     },
     "execution_count": 191,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = 1 / (R_h * q)\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 204,
   "id": "8f61c6e8-356b-4a4c-b797-dd5e8d0331eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 2.2e-3\n",
    "L35 = 3e-3\n",
    "l = 2.5e-3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 207,
   "id": "1f153f00-d44c-41e5-8bab-ad09bd8e5896",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "274.0977615349475"
      ]
     },
     "execution_count": 207,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sigma = 1 * L35/(1.99 * a * l)\n",
    "sigma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "id": "cf5b7366-16f9-434f-969f-74b61fed23ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1183.8084142394603"
      ]
     },
     "execution_count": 208,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = sigma * R_h\n",
    "b*10000"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
