{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2411ab62",
   "metadata": {},
   "source": [
    "# Part A - Repeated measurements\n",
    "- a representative value, using the samples mean \n",
    "- the measurements variability, using the sample standard deviation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "79441225",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4.98, 5.01, 5.  , 5.03, 4.99, 5.02, 4.97, 5.01])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np \n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "vSamples = np.array([4.98, 5.01, 5.00, 5.03, 4.99, 5.02, 4.97, 5.01])\n",
    "vSamples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f4fb3a51",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mean voltage is = 5.0013 V\n",
      "Sample standard deviation is = 0.0203 V\n"
     ]
    }
   ],
   "source": [
    "vMean = vSamples.mean() # np.mean(vSample)\n",
    "vSTD = vSamples.std(ddof=1) #sample standar deviation\n",
    "\n",
    "print(f\"Mean voltage is = {vMean:.4f} V\")\n",
    "print(f\"Sample standard deviation is = {vSTD:.4f} V\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "26b4bdf2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 27432 characters]",
      "text/plain": [
       "<Figure size 800x400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(8,4))\n",
    "plt.plot(vSamples, 'ok', label=\"Samples\")\n",
    "plt.axhline(vMean, linestyle=\"--\", linewidth=2, label=f\"Mean = {vMean:.4f}\")\n",
    "plt.xlabel(\"Measurements\")\n",
    "plt.ylabel(\"Voltage [V]\")\n",
    "plt.ylim([4.9,5.1])\n",
    "plt.grid(alpha=0.3)\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8893bdc0",
   "metadata": {},
   "source": [
    "1. What is the estimated voltage?\n",
    "2. What does the sample standard deviation tell us?\n",
    "3. Are the measurements relatively precise or widely scattered?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3482af0e",
   "metadata": {},
   "source": [
    "# Part B - Propagation of uncertainty of one variable\n",
    "Example area of a cricle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "2c2306b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Radius: r = 10.0000 +/- 0.2000 mm\n",
      "Area: A = 314.1593 +/- 12.5664 mm^2\n"
     ]
    }
   ],
   "source": [
    "r = 10.0\n",
    "sigmar = 0.2\n",
    "Area = np.pi*r**2\n",
    "sigmaA = sigmar*(2*np.pi*r)\n",
    "\n",
    "print(f\"Radius: r = {r:.4f} +/- {sigmar:.4f} mm\")\n",
    "print(f\"Area: A = {Area:.4f} +/- {sigmaA:.4f} mm^2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "1c60ddad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 23600 characters]",
      "text/plain": [
       "<Figure size 600x400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sigmarValues = np.linspace(0.01, 0.5, 10)\n",
    "sigmaAValues = (np.pi*2*r)*sigmarValues\n",
    "\n",
    "plt.figure(figsize=(6,4))\n",
    "plt.plot(sigmarValues, sigmaAValues, 'ok')\n",
    "plt.title(\"Propagation of uncertainty\")\n",
    "plt.xlabel(\"Uncertainty in radius [mm]\")\n",
    "plt.ylabel(\"Uncertainty in area [mm^2]\")\n",
    "plt.grid(alpha=0.3)\n",
    "plt.savefig(\"uncertainty.png\", dpi=300, bbox_inches=\"tight\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56766593",
   "metadata": {},
   "source": [
    "# Part c - Propagation of uncertainty for several variables\n",
    "Example: Resistance from voltage and current.\n",
    "- $V=12.0 \\pm 0.2$\n",
    "- $I=2.0 \\pm 0.05$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "a6583ccf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Resistance: R = 6.0000 +/- 0.1803 ohms\n"
     ]
    }
   ],
   "source": [
    "V = 12.0\n",
    "sigmaV = 0.2\n",
    "I = 2.0 \n",
    "sigmaI = 0.05\n",
    "\n",
    "R = V/I\n",
    "sigmaR = np.sqrt((sigmaV/I)**2 + (sigmaI*V/I**2)**2)\n",
    "print(f\"Resistance: R = {R:.4f} +/- {sigmaR:.4f} ohms\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "d57fa95d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Contribution from voltage uncertainty: 30.7692%\n",
      "Contribution from current uncertainty: 69.2308%\n"
     ]
    }
   ],
   "source": [
    "termV = (sigmaV / I)**2\n",
    "termI = ((V * sigmaI) / (I**2))**2\n",
    "total = termV + termI\n",
    "\n",
    "percent_V = 100 * termV / total\n",
    "percent_I = 100 * termI / total\n",
    "\n",
    "print(f\"Contribution from voltage uncertainty: {percent_V:.4f}%\")\n",
    "print(f\"Contribution from current uncertainty: {percent_I:.4f}%\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv (3.14.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.14.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
