{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "interactive-link",
   "metadata": {},
   "source": [
    "> **This is a saved, working copy.** Every cell below runs exactly as\n",
    "> shown, just like it did in the browser.\n",
    "> For the live browser version of this practice (handy if you want to\n",
    "> tinker), see [Homework 1 practice — interactive](https://math4120.pages.dev/wasm/hw1_practice.html).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ipython-import",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-13T23:26:30.269927Z",
     "iopub.status.busy": "2026-11-13T23:26:30.269684Z",
     "iopub.status.idle": "2026-11-13T23:26:30.273603Z",
     "shell.execute_reply": "2026-11-13T23:26:30.272938Z"
    }
   },
   "outputs": [],
   "source": [
    "from IPython.display import Markdown, HTML\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "Hbol",
   "metadata": {
    "marimo": {
     "config": {
      "hide_code": true
     },
     "md_prefix": "r"
    }
   },
   "source": [
    "# Computational Quiz — Practice\n",
    "\n",
    "**MATH4120 — Mathematical Modelling and Programming** Lancaster University, 2026–27\n",
    "\n",
    "---\n",
    "\n",
    "**This is scratch space, not the submission.** Write your `riemann_left_sum` and `trapezoidal_rule` here, run the check below, and once both say \"matches\", copy your two function definitions into the Moodle quiz — that's the actual submission, not this notebook."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "MJUe",
   "metadata": {
    "marimo": {
     "config": {
      "hide_code": true
     },
     "md_prefix": "r"
    }
   },
   "source": [
    "## Write your functions here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "vblA",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-13T23:26:30.275376Z",
     "iopub.status.busy": "2026-11-13T23:26:30.275184Z",
     "iopub.status.idle": "2026-11-13T23:26:30.278452Z",
     "shell.execute_reply": "2026-11-13T23:26:30.277529Z"
    }
   },
   "outputs": [],
   "source": [
    "def riemann_left_sum(f, a, b, n):\n",
    "    return 0.0    # ← write your implementation here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bkHC",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-13T23:26:30.280080Z",
     "iopub.status.busy": "2026-11-13T23:26:30.279921Z",
     "iopub.status.idle": "2026-11-13T23:26:30.282777Z",
     "shell.execute_reply": "2026-11-13T23:26:30.281947Z"
    }
   },
   "outputs": [],
   "source": [
    "def trapezoidal_rule(f, a, b, n):\n",
    "    return 0.0    # ← write your implementation here"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "lEQa",
   "metadata": {
    "marimo": {
     "config": {
      "hide_code": true
     },
     "md_prefix": "r"
    }
   },
   "source": [
    "## Check against the worked example\n",
    "\n",
    "This is the same check from the instructions: for $f(x) = x^2$ on $[0, 1]$ with $n = 4$, a correct implementation gives $R_4 = 0.21875$ and $T_4 = 0.34375$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "PKri",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-13T23:26:30.284272Z",
     "iopub.status.busy": "2026-11-13T23:26:30.284140Z",
     "iopub.status.idle": "2026-11-13T23:26:30.286547Z",
     "shell.execute_reply": "2026-11-13T23:26:30.285896Z"
    }
   },
   "outputs": [],
   "source": [
    "def f_check(x):\n",
    "    return x ** 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "Xref",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-13T23:26:30.288435Z",
     "iopub.status.busy": "2026-11-13T23:26:30.288204Z",
     "iopub.status.idle": "2026-11-13T23:26:30.292002Z",
     "shell.execute_reply": "2026-11-13T23:26:30.291231Z"
    }
   },
   "outputs": [],
   "source": [
    "riemann_expected = 0.21875\n",
    "trapezoidal_expected = 0.34375\n",
    "\n",
    "riemann_got = riemann_left_sum(f_check, 0.0, 1.0, 4)\n",
    "trapezoidal_got = trapezoidal_rule(f_check, 0.0, 1.0, 4)\n",
    "\n",
    "riemann_ok = abs(riemann_got - riemann_expected) < 1e-9\n",
    "trapezoidal_ok = abs(trapezoidal_got - trapezoidal_expected) < 1e-9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "SFPL",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-13T23:26:30.293589Z",
     "iopub.status.busy": "2026-11-13T23:26:30.293437Z",
     "iopub.status.idle": "2026-11-13T23:26:30.299369Z",
     "shell.execute_reply": "2026-11-13T23:26:30.298612Z"
    },
    "jupyter": {
     "source_hidden": true
    },
    "marimo": {
     "config": {
      "hide_code": true
     }
    },
    "tags": [
     "remove-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "\n",
       "```\n",
       "riemann_left_sum: got 0.0 — does not match yet ✗\n",
       "trapezoidal_rule: got 0.0 — does not match yet ✗\n",
       "```\n"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "riemann_line = f\"riemann_left_sum: got {riemann_got} — {'matches ✓' if riemann_ok else 'does not match yet ✗'}\"\n",
    "trapezoidal_line = f\"trapezoidal_rule: got {trapezoidal_got} — {'matches ✓' if trapezoidal_ok else 'does not match yet ✗'}\"\n",
    "Markdown(f\"\"\"\n",
    "```\n",
    "{riemann_line}\n",
    "{trapezoidal_line}\n",
    "```\n",
    "\"\"\")"
   ]
  }
 ],
 "metadata": {
  "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.13.3"
  },
  "marimo": {
   "app_config": {
    "app_title": "Computational Quiz — Practice",
    "width": "medium"
   },
   "marimo_version": "0.25.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
