-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83ca454
commit 096b52d
Showing
3 changed files
with
4 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"cells": [{"cell_type": "markdown", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": []}, "source": "### Computing for Mathematics - 2023/2024 individual coursework\n\n**Important** Do not delete the cells containing: \n\n```\n### BEGIN SOLUTION\n\n\n### END SOLUTION\n```\n\nwrite your solution attempts in those cells.\n\nTo submit this notebook:\n\n- Change the name of the notebook from `main` to: `<student_number>`. For example, if your student number is `c1234567` then change the name of the notebook to `c1234567`.\n- **Write all your solution attempts in the correct locations**;\n- **Do not delete** any code that is already in the cells;\n- Save the notebook (`File>Save As`);\n- Follow the instructions given to submit."}, {"cell_type": "markdown", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": []}, "source": "#### Question 1 (30 marks)\n\n(__Hint__: This question is similar to the [this section of the python for mathematics text](https://vknight.org/pfm/tools-for-mathematics/09-differential-equations/how/main.html#how-to-obtain-the-general-solution-of-a-differential-equation).)\n\nFor each of the differential equations output the general solution(s).\n\na. \\\\(\\frac{dy}{dx}=x\\\\)\n\nAvailable marks: 6"}, {"cell_type": "code", "execution_count": 1, "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": ["answer:q1-a"]}, "outputs": [{"data": {"text/latex": "$\\displaystyle y{\\left(x \\right)} = C_{1} + \\frac{x^{2}}{2}$", "text/plain": "Eq(y(x), C1 + x**2/2)"}, "execution_count": 1, "metadata": {}, "output_type": "execute_result"}], "source": "import sympy as sym\n\n### BEGIN SOLUTION\ny = sym.Function(\"y\")\nx = sym.Symbol(\"x\")\n\nlhs = sym.diff(y(x), x)\nrhs = x\nequation = sym.Eq(lhs, rhs)\nsym.dsolve(equation, y(x))\n### END SOLUTION"}, {"cell_type": "markdown", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": []}, "source": "b. \\\\(\\frac{dy}{dx}=y\\\\)\n\nAvailable marks: 6"}, {"cell_type": "code", "execution_count": 5, "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": ["answer:q1-b"]}, "outputs": [{"data": {"text/latex": "$\\displaystyle y{\\left(x \\right)} = C_{1} e^{x}$", "text/plain": "Eq(y(x), C1*exp(x))"}, "execution_count": 5, "metadata": {}, "output_type": "execute_result"}], "source": "### BEGIN SOLUTION\nlhs = sym.diff(y(x), x)\nrhs = y(x)\nequation = sym.Eq(lhs, rhs)\nsym.dsolve(equation, y(x))\n### END SOLUTION"}, {"cell_type": "markdown", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": []}, "source": "c. \\\\(\\frac{dy}{dx}=\\cos(y) \\sin(x)\\\\)\n\nAvailable marks: 6"}, {"cell_type": "code", "execution_count": 9, "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": ["answer:q1-c"]}, "outputs": [{"data": {"text/plain": "[Eq(y(x), pi - asin((C1 + exp(2*cos(x)))/(C1 - exp(2*cos(x))))),\n Eq(y(x), asin((C1 + exp(2*cos(x)))/(C1 - exp(2*cos(x)))))]"}, "execution_count": 9, "metadata": {}, "output_type": "execute_result"}], "source": "### BEGIN SOLUTION\nlhs = sym.diff(y(x), x)\nrhs = sym.cos(y(x)) * sym.sin(x)\nequation = sym.Eq(lhs, rhs)\nsym.dsolve(equation, y(x))\n### END SOLUTION"}, {"cell_type": "markdown", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": []}, "source": "d. \\\\(\\frac{dy}{dx}=\\frac{e^{y}}{x}\\\\)\n\nAvailable marks: 6"}, {"cell_type": "code", "execution_count": 13, "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": ["answer:q1-d"]}, "outputs": [{"data": {"text/latex": "$\\displaystyle y{\\left(x \\right)} = \\log{\\left(- \\frac{1}{C_{1} + \\log{\\left(x \\right)}} \\right)}$", "text/plain": "Eq(y(x), log(-1/(C1 + log(x))))"}, "execution_count": 13, "metadata": {}, "output_type": "execute_result"}], "source": "### BEGIN SOLUTION\nlhs = sym.diff(y(x), x)\nrhs = sym.exp(y(x)) / x\nequation = sym.Eq(lhs, rhs)\nsym.dsolve(equation, y(x))\n### END SOLUTION"}, {"cell_type": "markdown", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": []}, "source": "e. \\\\(\\frac{dy}{dx}=x ^ 3 - \\cos(x) + \\tan(x)\\\\)\n\nAvailable marks: 6"}, {"cell_type": "code", "execution_count": 17, "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": ["answer:q1-e"]}, "outputs": [{"data": {"text/latex": "$\\displaystyle y{\\left(x \\right)} = C_{1} + \\frac{x^{4}}{4} - \\log{\\left(\\cos{\\left(x \\right)} \\right)} - \\sin{\\left(x \\right)}$", "text/plain": "Eq(y(x), C1 + x**4/4 - log(cos(x)) - sin(x))"}, "execution_count": 17, "metadata": {}, "output_type": "execute_result"}], "source": "### BEGIN SOLUTION\nlhs = sym.diff(y(x), x)\nrhs = x ** 3 - sym.cos(x) + sym.tan(x)\nequation = sym.Eq(lhs, rhs)\nsym.dsolve(equation, y(x))\n### END SOLUTION"}, {"cell_type": "markdown", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": []}, "source": "### Question 2 (18 marks)\n\n\n(__Hint__: This question is similar to the [third exercise of the Algebra chapter of Python for mathematics](https://vknight.org/pfm/tools-for-mathematics/02-algebra/solutions/main.html#question-3))\n\nConsider the equation $\\alpha ^ 2 - \\beta / \\alpha = 5 + \\alpha$\n\na. Create a variable `general_solution` which has value the set of solutions to this equation for $\\alpha$ (in other words: what values can $\\alpha$ take as function of $\\beta$).\n\navailable marks: 12"}, {"cell_type": "code", "execution_count": 21, "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": ["answer:q2-a"]}, "outputs": [], "source": "alpha = sym.Symbol(\"alpha\")\nbeta = sym.Symbol(\"beta\")\n\n### BEGIN SOLUTION\nlhs = alpha ** 2 - beta / alpha\nrhs = 5 + alpha\nequation = sym.Eq(lhs=lhs, rhs=rhs)\ngeneral_solution = sym.solveset(equation, alpha)\n### END SOLUTION"}, {"cell_type": "markdown", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": []}, "source": "b. Output the set of solution for the case when $\\beta=0$.\n\navailable marks: 6"}, {"cell_type": "code", "execution_count": 25, "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": ["answer:q2-b"]}, "outputs": [{"data": {"text/latex": "$\\displaystyle \\left\\{\\frac{1}{3} - \\frac{16}{3 \\sqrt[3]{- \\frac{47}{2} + \\frac{45 \\sqrt{7} i}{2}} \\left(- \\frac{1}{2} - \\frac{\\sqrt{3} i}{2}\\right)} - \\frac{\\sqrt[3]{- \\frac{47}{2} + \\frac{45 \\sqrt{7} i}{2}} \\left(- \\frac{1}{2} - \\frac{\\sqrt{3} i}{2}\\right)}{3}, \\frac{1}{3} - \\frac{\\sqrt[3]{- \\frac{47}{2} + \\frac{45 \\sqrt{7} i}{2}} \\left(- \\frac{1}{2} + \\frac{\\sqrt{3} i}{2}\\right)}{3} - \\frac{16}{3 \\sqrt[3]{- \\frac{47}{2} + \\frac{45 \\sqrt{7} i}{2}} \\left(- \\frac{1}{2} + \\frac{\\sqrt{3} i}{2}\\right)}, \\frac{1}{3} - \\frac{\\sqrt[3]{- \\frac{47}{2} + \\frac{45 \\sqrt{7} i}{2}}}{3} - \\frac{16}{3 \\sqrt[3]{- \\frac{47}{2} + \\frac{45 \\sqrt{7} i}{2}}}\\right\\} \\setminus \\left\\{0\\right\\}$", "text/plain": "Complement({1/3 - 16/(3*(-47/2 + 45*sqrt(7)*I/2)**(1/3)*(-1/2 - sqrt(3)*I/2)) - (-47/2 + 45*sqrt(7)*I/2)**(1/3)*(-1/2 - sqrt(3)*I/2)/3, 1/3 - (-47/2 + 45*sqrt(7)*I/2)**(1/3)*(-1/2 + sqrt(3)*I/2)/3 - 16/(3*(-47/2 + 45*sqrt(7)*I/2)**(1/3)*(-1/2 + sqrt(3)*I/2)), 1/3 - (-47/2 + 45*sqrt(7)*I/2)**(1/3)/3 - 16/(3*(-47/2 + 45*sqrt(7)*I/2)**(1/3))}, {0})"}, "execution_count": 25, "metadata": {}, "output_type": "execute_result"}], "source": "### BEGIN SOLUTION\ngeneral_solution.subs({beta: 0})\n### END SOLUTION"}, {"cell_type": "markdown", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": []}, "source": "### Question 3 (24 marks)\n\n(__Hint__: This question is similar to the [fourth exercise of the Sequences chapter of Python for mathematics](https://vknight.org/pfm/tools-for-mathematics/07-sequences/solutions/main.html#question-4).)\n\na. Create a function `generate_sequence` which returns the $n$ term of the following sequence:\n\n\n$$\na_n =\n\\begin{cases}\n n &n \\in \\{1, 2, 3\\}\\\\\n a_{n-1} - a_{n-3}&\\text{if }n\\text{ odd}\\\\\n p a_{n-2}&\\text{otherwise}\n\\end{cases}\n$$\n\nFor some parameter $p$.\n\n_Available marks: 7_"}, {"cell_type": "code", "execution_count": 27, "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": ["answer:q3-a"]}, "outputs": [], "source": "p = sym.Symbol(\"p\")\n\ndef generate_sequence(n, p=p):\n### BEGIN SOLUTION\n \"\"\"\n Returns the sequence corresponding to question 4.\n \"\"\"\n if n in {1, 2, 3}:\n return n\n if n % 2 == 1:\n return generate_sequence(n=n - 1, p=p) - generate_sequence(n=n - 3, p=p)\n return p * generate_sequence(n=n - 2, p=p)\n### END SOLUTION"}, {"cell_type": "markdown", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": []}, "source": "b. Output $a_5$ in terms of $p$\n\n_Available marks: 2_"}, {"cell_type": "code", "execution_count": 31, "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": ["answer:q3-b"]}, "outputs": [{"data": {"text/latex": "$\\displaystyle 2 p - 2$", "text/plain": "2*p - 2"}, "execution_count": 31, "metadata": {}, "output_type": "execute_result"}], "source": "### BEGIN SOLUTION\ngenerate_sequence(n=5, p=p)\n### END SOLUTION"}, {"cell_type": "markdown", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": []}, "source": "c. Output $a_6$ for $p=\\frac{1}{2}$\n\n_Available marks: 6_"}, {"cell_type": "code", "execution_count": 33, "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": ["answer:q3-c"]}, "outputs": [{"data": {"text/latex": "$\\displaystyle \\frac{1}{2}$", "text/plain": "1/2"}, "execution_count": 33, "metadata": {}, "output_type": "execute_result"}], "source": "### BEGIN SOLUTION\ngenerate_sequence(n=6, p=sym.S(1) / 2)\n### END SOLUTION"}, {"cell_type": "markdown", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": ["description:3c-value-is-approximately-correct"]}, "source": "d. Output the set of values that $p$ can take which ensures that $a_{10}=a_{11}$.\n\n_Available marks: 9_"}, {"cell_type": "code", "execution_count": 36, "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": ["answer:q3-d"]}, "outputs": [{"data": {"text/latex": "$\\displaystyle \\left\\{0\\right\\}$", "text/plain": "{0}"}, "execution_count": 36, "metadata": {}, "output_type": "execute_result"}], "source": "### BEGIN SOLUTION\nlhs = generate_sequence(n=10, p=p)\nrhs = generate_sequence(n=11, p=p)\nequation = sym.Eq(lhs, rhs)\nsym.solveset(equation, p)\n### END SOLUTION"}, {"cell_type": "markdown", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": []}, "source": "### Question 4 (28 marks)\n\n(__Hint__: This question uses aspects from [the Matrices chapter of Python for mathematics](https://vknight.org/pfm/tools-for-mathematics/04-matrices/) as well as [the statistics chapter of Python for mathematics](https://vknight.org/pfm/tools-for-mathematics/08-statistics/))\n\na. Create variable `determinants` which has value a list with the determinants of each of the matrices in the tuple `matrices`:\n\n_Available marks: 10_"}, {"cell_type": "code", "execution_count": 39, "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": ["answer:q4-a"]}, "outputs": [], "source": "matrices = (\n sym.Matrix((1,)),\n sym.Matrix(((1, 2), (2, 1))),\n sym.Matrix(((3, 0), (0, 1))),\n sym.Matrix(((2, 1, 1), (3, 2, 4), (6, 1, -1))),\n sym.Matrix(((2, 1, 3), (3, 2, 4), (6, 1, -1))),\n sym.Matrix(((6, 2, 3), (3, -2, -4), (6, 1, -1))),\n sym.Matrix(((1, 2, 3, 1), (1, 3, -2, -4), (5, 6, 1, -1), (1, 2, -1, -4))),\n sym.Matrix(((-4, 3), (1, 1))),\n sym.Matrix(((3, 12), (0, 1))),\n)\n\n### BEGIN SOLUTION\ndeterminants = [matrix.det() for matrix in matrices]\n### END SOLUTION"}, {"cell_type": "markdown", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": []}, "source": "b. Create variable `number_of_rows` which has value a list with the number of rows of each of the matrices in the tuple `matrices`:\n\n_Available marks: 12_"}, {"cell_type": "code", "execution_count": 43, "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": ["answer:q4-b"]}, "outputs": [], "source": "### BEGIN SOLUTION\nnumber_of_rows = [matrix.rows for matrix in matrices]\n### END SOLUTION"}, {"cell_type": "markdown", "metadata": {"editable": true, "raw_mimetype": "", "slideshow": {"slide_type": ""}, "tags": []}, "source": "c. Output the pearson correlation coefficient between the number of rows and the determinants.\n\n_Available marks: 3_"}, {"cell_type": "code", "execution_count": 47, "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": ["answer:q4-c"]}, "outputs": [{"data": {"text/plain": "0.6804510993672779"}, "execution_count": 47, "metadata": {}, "output_type": "execute_result"}], "source": "import statistics as st\n\n### BEGIN SOLUTION\nst.correlation(number_of_rows, determinants)\n### END SOLUTION"}, {"cell_type": "markdown", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": []}, "source": "d. Create two variables: `slope` and `intercept` which have values the slope ($a$) and the intercept ($b$) for a line of best fit estimating the determinants as a function of the number of rows\n\n_Available marks: 3_"}, {"cell_type": "code", "execution_count": 51, "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": ["answer:q4-d"]}, "outputs": [], "source": "### BEGIN SOLUTION\nslope, intercept = st.linear_regression(number_of_rows, determinants)\n### END SOLUTION"}], "metadata": {"celltoolbar": "Tags", "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.11.4"}}, "nbformat": 4, "nbformat_minor": 4} |