diff options
Diffstat (limited to 'tutorials/module_1/notebook_1/array.ipynb')
| -rw-r--r-- | tutorials/module_1/notebook_1/array.ipynb | 251 |
1 files changed, 251 insertions, 0 deletions
diff --git a/tutorials/module_1/notebook_1/array.ipynb b/tutorials/module_1/notebook_1/array.ipynb new file mode 100644 index 0000000..f087b7e --- /dev/null +++ b/tutorials/module_1/notebook_1/array.ipynb @@ -0,0 +1,251 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Arrays\n", + "\n", + "In computer programming, an array is a structure for storing and\n", + "retrieving data. We often talk about an array as if it were a grid in\n", + "space, with each cell storing one element of the data. For instance, if\n", + "each element of the data were a number, we might visualize a\n", + "“one-dimensional” array like a list:\n", + "\n", + "| 1 | 5 | 2 | 0 |\n", + "|-----|-----|-----|-----|\n", + "\n", + "A two-dimensional array would be like a table:\n", + "\n", + "| 1 | 5 | 2 | 0 |\n", + "|-----|-----|-----|-----|\n", + "| 8 | 3 | 6 | 1 |\n", + "| 1 | 7 | 2 | 9 |\n", + "\n", + "A three-dimensional array would be like a set of tables, perhaps stacked\n", + "as though they were printed on separate pages. If we visualize the\n", + "position of each element as a position in space. Then we can represent\n", + "the value of the element as a property. In other words, if we were to\n", + "analyze the stress concentration of an aluminum block, the property\n", + "would be stress.\n", + "\n", + "- From [Numpy\n", + " documentation](https://numpy.org/doc/2.2/user/absolute_beginners.html)\n", + "\n", + "<figure>\n", + "<img src=\"attachment:figures/multi-dimensional-array.png\"\n", + "alt=\"Mathworks 3-D array\" />\n", + "<figcaption aria-hidden=\"true\">Mathworks 3-D array</figcaption>\n", + "</figure>\n", + "\n", + "If the load on this block changes over time, then we may want to add a\n", + "4th dimension i.e. additional sets of 3-D arrays for each time\n", + "increment. As you can see - the more dimensions we add, the more\n", + "complicated of a problem we have to solve. It is possible to increase\n", + "the number of dimensions to the n-th order. This course we will not be\n", + "going beyond dimensional analysis.\n", + "\n", + "## Numpy - the python’s array library\n", + "\n", + "In this tutorial we will be introducing arrays and we will be using the\n", + "numpy library. Arrays, lists, vectors, matrices, sets - You might’ve\n", + "heard of them before, they all store data. In programming, an array is a\n", + "variable that can hold more than one value at a time. We will be using\n", + "the Numpy python library to create arrays. Since we already have\n", + "installed Numpy previously, we can start using the package.\n", + "\n", + "Before importing our first package, let’s as ourselves *what is a\n", + "package?* A package can be thought of as pre-written python code that we\n", + "can re-use. This means the for every script that we write in python we\n", + "need to tell it to use a certain package. We call this importing a\n", + "package.\n", + "\n", + "### Importing Numpy\n", + "\n", + "When using packages in python, we need to let it know what package we\n", + "will be using. This is called importing. To import numpy we need to\n", + "declare it a the start of a script as follows:\n", + "\n", + "``` python\n", + "import numpy as np\n", + "```\n", + "\n", + "- `import` - calls for a library to use, in our case it is Numpy.\n", + "- `as` - gives the library an alias in your script. It’s common\n", + " convention in Python programming to make the code shorter and more\n", + " readable. We will be using *np* as it’s a standard using in many\n", + " projects.\n", + "\n", + "## Creating arrays\n", + "\n", + "Now that we have imported the library we can create a one dimensional\n", + "array or *vector* with three elements.\n", + "\n", + "``` python\n", + "x = np.array([1,2,3])\n", + "```\n", + "\n", + "To create a *matrix* we can nest the arrays to create a two dimensional\n", + "array. This is done as follows.\n", + "\n", + "``` python\n", + "matrix = np.array([[1,2,3],\n", + " [4,5,6],\n", + " [7,8,9]])\n", + "```\n", + "\n", + "*Note: for every array we nest, we get a new dimension in our data\n", + "structure.*\n", + "\n", + "### Numpy array creation functions\n", + "\n", + "Numpy comes with some built-in function that we can use to create arrays\n", + "quickly. Here are a couple of functions that are commonly used in\n", + "python. \\#### np.arange The `np.arange()` function returns an array with\n", + "evenly spaced values within a specified range. It is similar to the\n", + "built-in `range()` function in Python but returns a Numpy array instead\n", + "of a list. The parameters for this function are the start value\n", + "(inclusive), the stop value (exclusive), and the step size. If the step\n", + "size is not provided, it defaults to 1.\n", + "\n", + "``` python\n", + ">>> np.arange(4)\n", + "array([0. , 1., 2., 3. ])\n", + "```\n", + "\n", + "In this example, `np.arange(4)` generates an array starting from 0 and\n", + "ending before 4, with a step size of 1.\n", + "\n", + "#### np.linspace\n", + "\n", + "The `np.linspace()` function returns an array of evenly spaced values\n", + "over a specified range. Unlike `np.arange()`, which uses a step size to\n", + "define the spacing between elements, `np.linspace()` uses the number of\n", + "values you want to generate and calculates the spacing automatically. It\n", + "accepts three parameters: the start value, the stop value, and the\n", + "number of samples.\n", + "\n", + "``` python\n", + ">>> np.linspace(1., 4., 6)\n", + "array([1. , 1.6, 2.2, 2.8, 3.4, 4. ])\n", + "```\n", + "\n", + "In this example, `np.linspace(1., 4., 6)` generates 6 evenly spaced\n", + "values between 1. and 4., including both endpoints.\n", + "\n", + "Try this and see what happens:\n", + "\n", + "``` python\n", + "x = np.linspace(0,100,101)\n", + "y = np.sin(x)\n", + "```\n", + "\n", + "#### Other useful functions\n", + "\n", + "- `np.zeros()`\n", + "- `np.ones()`\n", + "- `np.eye()`\n", + "\n", + "### Working with Arrays\n", + "\n", + "Now that we have been introduced to some ways to create arrays using the\n", + "Numpy functions let’s start using them. \\#### Indexing Indexing in\n", + "Python allows you to access specific elements within an array based on\n", + "their position. This means you can directly retrieve and manipulate\n", + "individual items as needed.\n", + "\n", + "Python uses **zero-based indexing**, meaning the first element is at\n", + "position **0** rather than **1**. This approach is common in many\n", + "programming languages. For example, in a list with five elements, the\n", + "first element is at index `0`, followed by elements at indices `1`, `2`,\n", + "`3`, and `4`.\n", + "\n", + "Here’s an example of data from a rocket test stand where thrust was\n", + "recorded as a function of time.\n", + "\n", + "``` python\n", + "thrust_lbf = np.array(0.603355, 2.019083, 2.808092, 4.054973, 1.136618, 0.943668)\n", + "\n", + "print(thrust_lbs[3])\n", + "```\n", + "\n", + "Due to the nature of zero-based indexing. If we want to call the value\n", + "`4.054973` that will be the 3rd index. \\#### Operations on arrays -\n", + "Arithmetic operations (`+`, `-`, `*`, `/`, `**`) - `np.add()`,\n", + "`np.subtract()`, `np.multiply()`, `np.divide()` - `np.dot()` for dot\n", + "product - `np.matmul()` for matrix multiplication - `np.linalg.inv()`,\n", + "`np.linalg.det()` for linear algebra \\##### Statistics - `np.mean()`,\n", + "`np.median()`, `np.std()`, `np.var()` - `np.min()`, `np.max()`,\n", + "`np.argmin()`, `np.argmax()` - Summation along axes:\n", + "`np.sum(arr, axis=0)`\n", + "\n", + "## Exercise\n", + "\n", + "Let’s solve a statics problem given the following problem\n", + "\n", + "A simply supported bridge of length L = 20 m is subjected to three point\n", + "loads:\n", + "\n", + "- $P_1 = 10 kN$ at $x = 5 m$\n", + "- $P_2 = 15 kN$ at $x = 10 m$\n", + "- $P_3 = 20 kN$ at $x = 15 m$\n", + "\n", + "The bridge is supported by two reaction forces at points AAA (left\n", + "support) and BBB (right support). We assume the bridge is in static\n", + "equilibrium, meaning the sum of forces and sum of moments about any\n", + "point must be zero.\n", + "\n", + "##### Equilibrium Equations:\n", + "\n", + "1. **Sum of Forces in the Vertical Direction**:\n", + " $R_A + R_B - P_1 - P_2 - P_3 = 0$\n", + "2. **Sum of Moments About Point A**:\n", + " $5 P_1 + 10 P_2 + 15 P_3 - 20 R_B = 0$\n", + "3. **Sum of Moments About Point B**:\n", + " $20 R_A - 15 P_3 - 10 P_2 - 5 P_1 = 0$\n", + "\n", + "##### System of Equations:\n", + "\n", + "$$\n", + "\\begin{cases} R_A + R_B - 10 - 15 - 20 = 0 \\\\ 5(10) + 10(15) + 15(20) - 20 R_B = 0 \\\\ 20 R_A - 5(10) - 10(15) - 15(20) = 0 \\end{cases}\n", + "$$\n", + "\n", + "### Solution\n", + "\n", + "``` python\n", + "import numpy as np\n", + "\n", + "# Define the coefficient matrix A\n", + "A = np.array([\n", + " [1, 1],\n", + " [0, -20],\n", + " [20, 0]\n", + "])\n", + "\n", + "# Define the right-hand side vector b\n", + "b = np.array([\n", + " 45,\n", + " 5*10 + 10*15 + 15*20,\n", + " 5*10 + 10*15 + 15*20\n", + "])\n", + "\n", + "# Solve the system of equations Ax = b\n", + "x = np.linalg.lstsq(A, b, rcond=None)[0] # Using least squares to handle potential overdetermination\n", + "\n", + "# Display the results\n", + "print(f\"Reaction force at A (R_A): {x[0]:.2f} kN\")\n", + "print(f\"Reaction force at B (R_B): {x[1]:.2f} kN\")\n", + "```" + ], + "attachments": { + "figures/multi-dimensional-array.png": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAcwAAADPBAMAAACKFvyvAAAAMFBMVEX///8AAAB3d3fd3d2ZmZlE\nREQREREiIiLu7u5mZma7u7szMzPMzMxVVVWqqqqIiIigNNJfAAAMa0lEQVR42u1dv48cSRWu2mXH\nu17b92pty8ePYPZwgnQrYSS4hGAiYk+GQKA9S0cAyR5CiNB3gSMCrPsLSCw2BF8A2RHABCTOQRwB\n6CSEhM4ZGV1VXVWvfsxMd3VVV832tqzZqTc9nvmqX1e97+v6pgmpYmPk6m+UsYnAhGnAvD6YVwYl\nUJgCygkMt+i0pGwKKMW8kmmDelA2OPOdGDVlbDaYBGo5lizb0eQdWBAm9c6YtDCfoQ6EKlAyA/ND\nQk758z3+6hF7vD8nIira5PRERr/BG4dzvVfz+Pw+saLkOTFRqAElRTAfkgPRWvKX9/bPm4CIijZ5\n/IGM7i3434XZi5D5t4gVvSlgMhmFajK2hfkLlbvi9eWCvENkVO5/JqP7F82fG0/NXoQc3CNW9PiR\n+BgZheIoUaFHJToE89aFbEHb/su8jYLZXbSbbUWs6Gv536zMm8uelxtgPmnScYZg7n+XtNFzBIi3\nyaxJWit6yosCFS0Ccx3v4t/wb/zPgYB1AJ89WJBLIqO8/UbzziVpojd/1QRvf2L2Wt55/2cERZe8\nL++DikJFKAXMf/InN8/N40fEjr7gD0dfbx7uLNZHX5iXeBSKonSKMA5zjz85XuhHOaGg6Mf8QUwd\nZL4++rF5qcyEYhXrkKs8cDoQqsnYxDChKEyE0p85r0rpbhUFLNvR9DsQiqHMmLSs6NEMFOtXMGnX\nlD6pYbKyMLdlbCKY4U8ZDSYuCiD/hIJo9ZgwrdJnE8yGQLN5gFYLQu3RatVuCTTvQBUtQas3lD4O\nzI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