From 397a67cf6da345e4a9f999643fc0d87bdf73e797 Mon Sep 17 00:00:00 2001 From: Berend Bouvy <b.n.bouvy@student.tudelft.nl> Date: Sun, 6 Oct 2024 19:56:58 +0200 Subject: [PATCH] I forgot to save --- content/tutorials/Week_1_6/Tutorial.ipynb | 165 ++++-------------- .../Tutorials/Week_1_6/Tutorial.ipynb | 165 ++++-------------- 2 files changed, 64 insertions(+), 266 deletions(-) diff --git a/content/tutorials/Week_1_6/Tutorial.ipynb b/content/tutorials/Week_1_6/Tutorial.ipynb index 7985620c..91a253da 100644 --- a/content/tutorials/Week_1_6/Tutorial.ipynb +++ b/content/tutorials/Week_1_6/Tutorial.ipynb @@ -17,7 +17,7 @@ "<h2 style=\"height: 10px\">\n", "</h2>\n", "\n", - "*[CEGM1000 MUDE](http://mude.citg.tudelft.nl/): Week 1.5. September 30, 2024.*\n", + "*[CEGM1000 MUDE](http://mude.citg.tudelft.nl/): Week 1.6. October 7, 2024.*\n", "\n", "_This notebook was prepared by Berend Bouvy and used in an in-class demonstration on Monday._" ] @@ -31,41 +31,26 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", - "import os" + "from func import *\n", + "import timeit" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "# Objective: Create a variable of each type in Python\n", - "int_var = #TODO: create an integer variable\n", - "float_var = #TODO: create a float variable\n", - "bool_var = #TODO: create a boolean variable\n", - "str_var = #TODO: create a string variable\n", - "list_var = #TODO: create a list variable\n", - "tuple_var = #TODO: create a tuple variable\n", - "dict_var = #TODO: create a dictionary variable\n", - "\n", - "\n", - "# Asserts\n", - "assert type(int_var) == int, f'Expected int but got {type(int_var)}' # This will throw an error if the type is incorrect\n", - "assert type(float_var) == float, f'Expected float but got {type(float_var)}' # This will throw an error if the type is incorrect\n", - "assert type(bool_var) == bool, f'Expected bool but got {type(bool_var)}' # This will throw an error if the type is incorrect\n", - "assert type(str_var) == str, f'Expected str but got {type(str_var)}' # This will throw an error if the type is incorrect\n", - "assert type(list_var) == list, f'Expected list but got {type(list_var)}' # This will throw an error if the type is incorrect\n", - "assert type(tuple_var) == tuple, f'Expected tuple but got {type(tuple_var)}' # This will throw an error if the type is incorrect\n", - "assert type(dict_var) == dict, f'Expected dict but got {type(dict_var)}' # This will throw an error if the type is incorrect\n", - "\n", - "\n" + "# import data \n", + "data = np.loadtxt('data.txt')\n", + "x = data[:,0]\n", + "y = data[:,1]\n" ] }, { @@ -74,124 +59,38 @@ "metadata": {}, "outputs": [], "source": [ - "# relative path vs absolute path\n", - "# relative path: relative to the current working directory\n", - "# absolute path: full path from the root directory\n", - "\n", - "# Create a variable that contains the current working directory using the os module\n", - "cwd = #TODO: get the current working directory\n", - "print(cwd)\n", - "\n", - "# Get all the files in the current working directory\n", - "files = #TODO: get all the files in the current working directory\n", - "print(files)\n", - "\n", - "# find path to data in data folder\n", - "data_dir = #TODO: find the path to the data folder\n", - "print(data_dir)\n", - "\n", - "# read the data using absolute path and relative path\n", - "data_abs = #TODO: read the data using absolute path\n", - "data_rel = #TODO: read the data using relative path\n", - "\n", - "# Asserts\n", - "assert data_abs == data_rel, 'Data read using absolute path and relative path are not the same' # This will throw an error if the data is not the same\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "$$ H_n = \\sum_{k=1}^{n} \\frac{1}{k} $$" + "A = #TODO: create the matrix A\n", + "x_hat, y_hat = #TODO: solve the system of equations\n", + "print(f\"x_hat = {x_hat}\")" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def harmonic_series(n):\n", - " \"\"\"\n", - " This function calculates the harmonic series of n\n", - " \"\"\"\n", - " result = #TODO: calculate the harmonic series of n\n", - " return result\n", - "\n", - "# Plotting\n", - "n = 100\n", - "x = #TODO: create a list of n values from 1 to n\n", - "y = #TODO: calculate the harmonic series of x, using list comprehension\n", - "\n", - "#TODO: plot x and y, with labels and title\n", - "\n", - "# asserts\n", - "assert harmonic_series(1) == 1, f'Expected 1 but got {harmonic_series(1)}'\n", - "assert harmonic_series(2) == 1.5, f'Expected 1.5 but got {harmonic_series(2)}'\n", - "assert harmonic_series(3) == 1.8333333333333333, f'Expected 1.8333333333333333 but got {harmonic_series(3)}'\n", - "\n", - "# save x, y data\n", - "data = #TODO: create a 2D array with x and y using np.column_stack()\n", - "#TODO: save data to a csv file" - ] - }, - { - "cell_type": "markdown", + "execution_count": 9, "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "runtimes: [0.0036198 0.079353 0.0814436 0.0611185]\n" + ] + } + ], "source": [ - "[Wikipedia: Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_sequence)\n", + "# runtimes\n", "\n", + "funcs = [FD_1, FD_2, FD_3, FD_4]\n", "\n", - "$$ F_n = F_{n-1} + F_{n-2} \\quad \\text{for} \\quad n \\geq 2 \\quad \\text{with} \\quad F_0 = 0, \\quad F_1 = 1$$\n" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [], - "source": [ - "def fibonacci(n):\n", - " \"\"\"\n", - " This function will return the n-th numbers of the fibonacci sequence\n", - " \"\"\"\n", - " #TODO: calculate the n-th number of the fibonacci sequence\n", - " return result\n", + "assert np.allclose(funcs[0](x, y), funcs[1](x,y)), \"FD_1 and FD_2 are not equal\"\n", + "assert np.allclose(funcs[0](x, y), funcs[2](x,y)), \"FD_1 and FD_3 are not equal\"\n", + "assert np.allclose(funcs[0](x, y), funcs[3](x,y)), \"FD_1 and FD_4 are not equal\"\n", "\n", - "# Asserts\n", - "assert fibonacci(0) == 0, f'Expected 0 but got {fibonacci(0)}' # This will throw an error if the result is incorrect \n", - "assert fibonacci(1) == 1, f'Expected 1 but got {fibonacci(1)}' # This will throw an error if the result is incorrect\n", - "assert fibonacci(2) == 1, f'Expected 1 but got {fibonacci(2)}' # This will throw an error if the result is incorrect\n", - "assert fibonacci(3) == 2, f'Expected 2 but got {fibonacci(3)}' # This will throw an error if the result is incorrect" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [], - "source": [ - "def fib_sequence(n):\n", - " \"\"\"\n", - " This function will return the first n numbers of the fibonacci sequence\n", - " \"\"\"\n", - " # result = #TODO: calculate the first n numbers of the fibonacci sequence\n", - " result = [0, 1]\n", - " if n == 0:\n", - " return []\n", - " elif n == 1:\n", - " return [0]\n", - " for i in range(2, n):\n", - " result.append(result[-1] + result[-2])\n", - " return result\n", + "runtime = np.zeros(4)\n", + "for i in range(4):\n", + " runtime[i] = timeit.timeit(lambda: funcs[i](x, y), number=1000)\n", "\n", - "# Asserts\n", - "assert fib_sequence(0) == [], f'Expected [] but got {fib_sequence(0)}' # This will throw an error if the result is incorrect\n", - "assert fib_sequence(1) == [0], f'Expected [0] but got {fib_sequence(1)}' # This will throw an error if the result is incorrect\n", - "assert fib_sequence(2) == [0, 1], f'Expected [0, 1] but got {fib_sequence(2)}' # This will throw an error if the result is incorrect\n", - "assert fib_sequence(3) == [0, 1, 1], f'Expected [0, 1, 1] but got {fib_sequence(3)}' # This will throw an error if the result is incorrect\n", - "assert fib_sequence(4) == [0, 1, 1, 2], f'Expected [0, 1, 1, 2] but got {fib_sequence(4)}' # This will throw an error if the result is incorrect\n", - "assert fib_sequence(5) == [0, 1, 1, 2, 3], f'Expected [0, 1, 1, 2, 3] but got {fib_sequence(5)}' # This will throw an error if the result is incorrect\n" + "print(f\"runtimes: {runtime}\")" ] }, { @@ -238,7 +137,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.5" + "version": "3.12.6" } }, "nbformat": 4, diff --git a/src/students/Tutorials/Week_1_6/Tutorial.ipynb b/src/students/Tutorials/Week_1_6/Tutorial.ipynb index 7985620c..91a253da 100644 --- a/src/students/Tutorials/Week_1_6/Tutorial.ipynb +++ b/src/students/Tutorials/Week_1_6/Tutorial.ipynb @@ -17,7 +17,7 @@ "<h2 style=\"height: 10px\">\n", "</h2>\n", "\n", - "*[CEGM1000 MUDE](http://mude.citg.tudelft.nl/): Week 1.5. September 30, 2024.*\n", + "*[CEGM1000 MUDE](http://mude.citg.tudelft.nl/): Week 1.6. October 7, 2024.*\n", "\n", "_This notebook was prepared by Berend Bouvy and used in an in-class demonstration on Monday._" ] @@ -31,41 +31,26 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", - "import os" + "from func import *\n", + "import timeit" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "# Objective: Create a variable of each type in Python\n", - "int_var = #TODO: create an integer variable\n", - "float_var = #TODO: create a float variable\n", - "bool_var = #TODO: create a boolean variable\n", - "str_var = #TODO: create a string variable\n", - "list_var = #TODO: create a list variable\n", - "tuple_var = #TODO: create a tuple variable\n", - "dict_var = #TODO: create a dictionary variable\n", - "\n", - "\n", - "# Asserts\n", - "assert type(int_var) == int, f'Expected int but got {type(int_var)}' # This will throw an error if the type is incorrect\n", - "assert type(float_var) == float, f'Expected float but got {type(float_var)}' # This will throw an error if the type is incorrect\n", - "assert type(bool_var) == bool, f'Expected bool but got {type(bool_var)}' # This will throw an error if the type is incorrect\n", - "assert type(str_var) == str, f'Expected str but got {type(str_var)}' # This will throw an error if the type is incorrect\n", - "assert type(list_var) == list, f'Expected list but got {type(list_var)}' # This will throw an error if the type is incorrect\n", - "assert type(tuple_var) == tuple, f'Expected tuple but got {type(tuple_var)}' # This will throw an error if the type is incorrect\n", - "assert type(dict_var) == dict, f'Expected dict but got {type(dict_var)}' # This will throw an error if the type is incorrect\n", - "\n", - "\n" + "# import data \n", + "data = np.loadtxt('data.txt')\n", + "x = data[:,0]\n", + "y = data[:,1]\n" ] }, { @@ -74,124 +59,38 @@ "metadata": {}, "outputs": [], "source": [ - "# relative path vs absolute path\n", - "# relative path: relative to the current working directory\n", - "# absolute path: full path from the root directory\n", - "\n", - "# Create a variable that contains the current working directory using the os module\n", - "cwd = #TODO: get the current working directory\n", - "print(cwd)\n", - "\n", - "# Get all the files in the current working directory\n", - "files = #TODO: get all the files in the current working directory\n", - "print(files)\n", - "\n", - "# find path to data in data folder\n", - "data_dir = #TODO: find the path to the data folder\n", - "print(data_dir)\n", - "\n", - "# read the data using absolute path and relative path\n", - "data_abs = #TODO: read the data using absolute path\n", - "data_rel = #TODO: read the data using relative path\n", - "\n", - "# Asserts\n", - "assert data_abs == data_rel, 'Data read using absolute path and relative path are not the same' # This will throw an error if the data is not the same\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "$$ H_n = \\sum_{k=1}^{n} \\frac{1}{k} $$" + "A = #TODO: create the matrix A\n", + "x_hat, y_hat = #TODO: solve the system of equations\n", + "print(f\"x_hat = {x_hat}\")" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def harmonic_series(n):\n", - " \"\"\"\n", - " This function calculates the harmonic series of n\n", - " \"\"\"\n", - " result = #TODO: calculate the harmonic series of n\n", - " return result\n", - "\n", - "# Plotting\n", - "n = 100\n", - "x = #TODO: create a list of n values from 1 to n\n", - "y = #TODO: calculate the harmonic series of x, using list comprehension\n", - "\n", - "#TODO: plot x and y, with labels and title\n", - "\n", - "# asserts\n", - "assert harmonic_series(1) == 1, f'Expected 1 but got {harmonic_series(1)}'\n", - "assert harmonic_series(2) == 1.5, f'Expected 1.5 but got {harmonic_series(2)}'\n", - "assert harmonic_series(3) == 1.8333333333333333, f'Expected 1.8333333333333333 but got {harmonic_series(3)}'\n", - "\n", - "# save x, y data\n", - "data = #TODO: create a 2D array with x and y using np.column_stack()\n", - "#TODO: save data to a csv file" - ] - }, - { - "cell_type": "markdown", + "execution_count": 9, "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "runtimes: [0.0036198 0.079353 0.0814436 0.0611185]\n" + ] + } + ], "source": [ - "[Wikipedia: Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_sequence)\n", + "# runtimes\n", "\n", + "funcs = [FD_1, FD_2, FD_3, FD_4]\n", "\n", - "$$ F_n = F_{n-1} + F_{n-2} \\quad \\text{for} \\quad n \\geq 2 \\quad \\text{with} \\quad F_0 = 0, \\quad F_1 = 1$$\n" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [], - "source": [ - "def fibonacci(n):\n", - " \"\"\"\n", - " This function will return the n-th numbers of the fibonacci sequence\n", - " \"\"\"\n", - " #TODO: calculate the n-th number of the fibonacci sequence\n", - " return result\n", + "assert np.allclose(funcs[0](x, y), funcs[1](x,y)), \"FD_1 and FD_2 are not equal\"\n", + "assert np.allclose(funcs[0](x, y), funcs[2](x,y)), \"FD_1 and FD_3 are not equal\"\n", + "assert np.allclose(funcs[0](x, y), funcs[3](x,y)), \"FD_1 and FD_4 are not equal\"\n", "\n", - "# Asserts\n", - "assert fibonacci(0) == 0, f'Expected 0 but got {fibonacci(0)}' # This will throw an error if the result is incorrect \n", - "assert fibonacci(1) == 1, f'Expected 1 but got {fibonacci(1)}' # This will throw an error if the result is incorrect\n", - "assert fibonacci(2) == 1, f'Expected 1 but got {fibonacci(2)}' # This will throw an error if the result is incorrect\n", - "assert fibonacci(3) == 2, f'Expected 2 but got {fibonacci(3)}' # This will throw an error if the result is incorrect" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [], - "source": [ - "def fib_sequence(n):\n", - " \"\"\"\n", - " This function will return the first n numbers of the fibonacci sequence\n", - " \"\"\"\n", - " # result = #TODO: calculate the first n numbers of the fibonacci sequence\n", - " result = [0, 1]\n", - " if n == 0:\n", - " return []\n", - " elif n == 1:\n", - " return [0]\n", - " for i in range(2, n):\n", - " result.append(result[-1] + result[-2])\n", - " return result\n", + "runtime = np.zeros(4)\n", + "for i in range(4):\n", + " runtime[i] = timeit.timeit(lambda: funcs[i](x, y), number=1000)\n", "\n", - "# Asserts\n", - "assert fib_sequence(0) == [], f'Expected [] but got {fib_sequence(0)}' # This will throw an error if the result is incorrect\n", - "assert fib_sequence(1) == [0], f'Expected [0] but got {fib_sequence(1)}' # This will throw an error if the result is incorrect\n", - "assert fib_sequence(2) == [0, 1], f'Expected [0, 1] but got {fib_sequence(2)}' # This will throw an error if the result is incorrect\n", - "assert fib_sequence(3) == [0, 1, 1], f'Expected [0, 1, 1] but got {fib_sequence(3)}' # This will throw an error if the result is incorrect\n", - "assert fib_sequence(4) == [0, 1, 1, 2], f'Expected [0, 1, 1, 2] but got {fib_sequence(4)}' # This will throw an error if the result is incorrect\n", - "assert fib_sequence(5) == [0, 1, 1, 2, 3], f'Expected [0, 1, 1, 2, 3] but got {fib_sequence(5)}' # This will throw an error if the result is incorrect\n" + "print(f\"runtimes: {runtime}\")" ] }, { @@ -238,7 +137,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.5" + "version": "3.12.6" } }, "nbformat": 4, -- GitLab