{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Opgaver til python A og B kursus" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Opgave 1\n", "\n", "Make a function that can appproximate an integral using mid point integration:\n", "\n", "$$ \\int_a^b f(x) dx \\approx h \\cdot \\sum_{i=0}^{n-1} f(a + 1/2 h + ih)$$\n", "\n", "1. Make a Python function midpointint(f, a, b, n): that performs the mid point integration where f is a scalar function that can be evaluated as f(x).\n", "2. Compute closed form solutions of $\\int_a^b f(x) dx$ for your favorite $f$ e.g. exp, sin, cos\n", "3. Validate you implementation with the closed form solution" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def midpointint(f, a, b, n): \n", " \"\"\" \n", " Approximates int(a to b) f(x) dx using midpoint integration. \n", " \"\"\" \n", " h = (b-a)/n \n", " \n", " # Create a generator and sum it \n", " gen = (f(a + 1/2 * h + i * h) for i in range(0, n-1)) \n", " \n", " return h * sum(gen)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I like $f(x) = e^x$ so that is what we will do. With $a = 0, b = 10$.\n", "\n", "$$\\int_0^{10} e^x dx = e^{10} -1 \\approx 22025$$" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "21806.207938916818" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "f = lambda x: math.exp(x)\n", "midpointint(f, 0, 10, 1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Trying a $f(x) = sin(x)$ with in same interval.\n", "\n", "$$\\int_0^{10} sin(x) dx \\approx 1.8391$$" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.8444773816015885" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = lambda x: math.sin(x)\n", "midpointint(f, 0, 10, 1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Opgave 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make a Python script, which defines and prints the following integer arrays:\n", "\n", "$$ D_1 = \\left[\\begin{matrix}\n", " 1 & 0 & 1 \\\\\n", " 0 & 2 & 0 \\\\\n", " 1 & 0 & 1\n", "\\end{matrix}\\right]$$\n", "\n", "$$ D_2 = \\left[\\begin{matrix}\n", " 1 & 8 & 1 \\\\\n", " 8 & 2 & 8 \\\\\n", " 1 & 8 & 1\n", "\\end{matrix}\\right]$$\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1, 8, 1], [8, 2, 8], [1, 8, 1]]" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "D1 = [\n", " [1, 0, 1],\n", " [0, 2, 0],\n", " [1, 0, 1]\n", "]\n", "D2 = [\n", " [1, 8, 1],\n", " [8, 2, 8],\n", " [1, 8, 1]\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the script further compute the following:\n", "\n", "1. Make a list of tuples containing indices to matrix elements $(D_2)_{i,j}$ where $(D_2)_{i,j} > 1$. Print the list and validate that it is correct." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(0, 1), (1, 0), (1, 1), (1, 2), (2, 1)]" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# This can be done using list comprehention\n", "[(i, j) for (i, r) in enumerate(D2) for (j, c) in enumerate(r) if c > 1]" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(0, 1), (1, 0), (1, 1), (1, 2), (2, 1)]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Or generators\n", "def gen():\n", " for (i, r) in enumerate(D2):\n", " for (j, c) in enumerate(r):\n", " if c > 1:\n", " yield (i, j)\n", " \n", "list(gen())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Make a new matrix as:\n", "$$ F = \\left[\\begin{matrix}\n", "D_2 & D_2 \\\\\n", "D_2 & D_2\n", "\\end{matrix}\\right]$$\n", "Print **F** and the shape of **F** as a tuple.\n" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(6, 6)\n" ] }, { "data": { "text/plain": [ "[[1, 8, 1, 1, 8, 1],\n", " [8, 2, 8, 8, 2, 8],\n", " [1, 8, 1, 1, 8, 1],\n", " [1, 8, 1, 1, 8, 1],\n", " [8, 2, 8, 8, 2, 8],\n", " [1, 8, 1, 1, 8, 1]]" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Multiplying list concatinates it with itself.\n", "\n", "F = [2*r for r in D2] * 2\n", "\n", "# outer inner\n", "print((len(F), len(F[0])))\n", "F" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Compute and print the sum of all elements of the **F** matrix." ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "152" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum((c for r in F for c in r))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. Use Python to determine and print the number of ‘1’, ‘2’ and ‘8’ values in **F**." ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1: 16, 2: 4, 8: 16\n" ] } ], "source": [ "# Not very efficient because we loop multiple times.\n", "count = lambda n: sum((1 for r in F for c in r if c == n))\n", "\n", "print(f\"1: {count(1)}, 2: {count(2)}, 8: {count(8)}\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 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.8.1" } }, "nbformat": 4, "nbformat_minor": 2 }