From c3c1dfffaeac9774e9f2a355fd1fb985b6283f06 Mon Sep 17 00:00:00 2001 From: Vince Knight Date: Wed, 13 Nov 2024 09:40:15 +0000 Subject: [PATCH] Add log of intro to sequences 2025 --- .../2024-11-13-introduction-to-sequences.md | 18 + assets/nbs/2024-2025/sequences.ipynb | 481 ++++++++++++++++++ 2 files changed, 499 insertions(+) create mode 100644 _posts/2024-11-13-introduction-to-sequences.md create mode 100644 assets/nbs/2024-2025/sequences.ipynb diff --git a/_posts/2024-11-13-introduction-to-sequences.md b/_posts/2024-11-13-introduction-to-sequences.md new file mode 100644 index 0000000..6ca2108 --- /dev/null +++ b/_posts/2024-11-13-introduction-to-sequences.md @@ -0,0 +1,18 @@ +--- +layout: post +title: "Introduction to Sequences" +tags: + - sequences +--- + +In class yesterday I gave an overview of how to use the recursion +to directly define mathematical sequences. + +You can see a recording of the class [here](https://cardiff.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=51f6f1f4-9c75-4c3d-902f-b22101083fff) + +Here is the notebook I used in class: +[sequences.ipynb]({{site.baseurl}}/assets/nbs/2024-2025/sequences.ipynb) + +An interesting use of recursion when programming is to study +the [Collatz Conjecture](https://en.wikipedia.org/wiki/Collatz_conjecture). If +you attempt this and would like me to take a look let me know. diff --git a/assets/nbs/2024-2025/sequences.ipynb b/assets/nbs/2024-2025/sequences.ipynb new file mode 100644 index 0000000..dcc1f2e --- /dev/null +++ b/assets/nbs/2024-2025/sequences.ipynb @@ -0,0 +1,481 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "152e0df8-fd01-411e-be3a-eec805a3faea", + "metadata": {}, + "source": [ + "$$\n", + "\\left\\{\n", + "\\begin{array}{c}\n", + "4\\\\\n", + "5\\\\\n", + "\\sum_{i=0}^{n}i\n", + "\\end{array}\n", + "\\right.x\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "a7310d3a-024f-42da-b302-58cf5c59fe2f", + "metadata": {}, + "source": [ + "$$\n", + "\\left\\{\n", + "\\begin{array}{lr}\n", + "4& 5\\\\\n", + "5& 50\\\\\n", + "\\sum_{i=0}^{n}i& \\int_0^{\\pi}e^{ix}\n", + "\\end{array}\n", + "\\right.x\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "82bc5e58-0b00-4f0c-b8e3-976401e7811d", + "metadata": {}, + "source": [ + "A sequence $a_1, a_2, a_3, …$ is defined by:\n", + "\n", + "$$\\begin{aligned}\n", + "\\left\\{\n", + "\\begin{array}{l}\n", + " a_1 = k,\\\\\n", + " a_{n + 1} = 3a_n – 4, n \\geq 1,\n", + "\\end{array}\n", + "\\right.\n", + "\\end{aligned}$$\n", + "\n", + "where $k$ is a constant.\n", + "\n", + "1. Write down an expression for $a_2$ in terms of $k$.\n", + "2. Show that $a_3 = 9k - 16$\n", + "3. Given that $\\sum_{r=1}^{12} a_r = 50$ find the value of $k$." + ] + }, + { + "cell_type": "markdown", + "id": "9a7594b3-33c0-430c-bd14-6b3cb1dd4883", + "metadata": {}, + "source": [ + "We will start by writing out the recursive definition for $a_n$ in code **directly**:" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "d036c213-eaff-4199-bdf9-e80929077df1", + "metadata": {}, + "outputs": [], + "source": [ + "def generate_a(k_value, n):\n", + " \"\"\"\n", + " Uses recursion to give the terms of the sequence\n", + " \"\"\"\n", + " if n == 1:\n", + " return k_value\n", + " return 3 * generate_a(k_value=k_value, n=n - 1) - 4" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "cff74ad2-e59c-4d93-8324-3f26032ab9bd", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "generate_a(k_value=5, n=1)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "2a0583a5-f2b1-4a2a-9459-f76950b9e97c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "11" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "generate_a(k_value=5, n=2)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "089ebe5b-6895-46b9-857c-9c215dd0650c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "29" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "generate_a(k_value=5, n=3)" + ] + }, + { + "cell_type": "markdown", + "id": "5ba657dc-e944-4df3-9bab-5efbd8718cb4", + "metadata": {}, + "source": [ + "$$\n", + "a_{n} = 3 a_{n - 1} - 4\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "04afc98a-10f8-4314-a4c4-9f404b0bb72d", + "metadata": {}, + "source": [ + "To write down an expression of $a_2$ as a function of $k$ we need to define a symbolic variable $k$:" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "e5b59c50-0c31-4acf-9eb2-cad703138c98", + "metadata": {}, + "outputs": [], + "source": [ + "import sympy as sym\n", + "\n", + "k = sym.Symbol(\"k\")" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "68c2ad22-19e6-4581-986b-62296d796e03", + "metadata": {}, + "outputs": [ + { + "data": { + "text/latex": [ + "$\\displaystyle 3 k - 4$" + ], + "text/plain": [ + "3*k - 4" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "generate_a(k_value=k, n=2)" + ] + }, + { + "cell_type": "markdown", + "id": "0af1a5d5-91ee-468b-9f05-152b041cbe23", + "metadata": {}, + "source": [ + "To get $a_3$:" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "84f5116d-f380-493c-a0ec-b4cfdc52cc5f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/latex": [ + "$\\displaystyle 9 k - 16$" + ], + "text/plain": [ + "9*k - 16" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "generate_a(k_value=k, n=3)" + ] + }, + { + "cell_type": "markdown", + "id": "9b424cf4-5e2e-4d99-b18c-c5e5b67993a1", + "metadata": {}, + "source": [ + "To solve the final question, let us write down the equation:" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "4ece426d-5cdb-4a60-a3ca-7c335c99e608", + "metadata": {}, + "outputs": [ + { + "data": { + "text/latex": [ + "$\\displaystyle 265720 k - 531416$" + ], + "text/plain": [ + "265720*k - 531416" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lhs = sum(generate_a(k_value=k, n=r) for r in range(1, 13))\n", + "lhs" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "fccaff94-285b-4e3b-8ee5-ba9234514a39", + "metadata": {}, + "outputs": [ + { + "data": { + "text/latex": [ + "$\\displaystyle 265720 k - 531416 = 50$" + ], + "text/plain": [ + "Eq(265720*k - 531416, 50)" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "equation = sym.Eq(lhs=lhs, rhs=50)\n", + "equation" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "60e90acc-8ef9-4b99-9f66-76224d7c2c83", + "metadata": {}, + "outputs": [ + { + "data": { + "text/latex": [ + "$\\displaystyle \\left\\{\\frac{20441}{10220}\\right\\}$" + ], + "text/plain": [ + "{20441/10220}" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sym.solveset(equation, k)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "0d1f6ecf-9371-4ebb-986e-86cfdc9f991f", + "metadata": {}, + "outputs": [], + "source": [ + "import random" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "fe4a01c0-fe0f-47a3-8112-732e7420cdfe", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "random.seed(450682)\n", + "random.randint(1, 6)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "b0924192-f8a2-4f4e-8de8-54fd1de31d20", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "random.randint(1, 6)" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "6cce74d5-dc62-4de6-baba-250da21eaaa4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "random.randint(1, 6)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "7579be60-d3b4-40e0-81c4-b0fdec5c4028", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "random.seed(450682)\n", + "random.randint(1, 6)" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "cb752deb-fb98-404a-a05a-2c7a20a262d5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "random.randint(1, 6)" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "e872204d-ab36-4623-8d02-90b6cc4d5ee5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "random.randint(1, 6)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ceeae406-7084-4378-8ed0-76e03e38f791", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.13.0" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}