diff options
| author | Christian Kolset <christian.kolset@gmail.com> | 2025-04-10 12:43:05 -0600 |
|---|---|---|
| committer | Christian Kolset <christian.kolset@gmail.com> | 2025-04-10 12:43:05 -0600 |
| commit | aa047a8826c03a38f0325353396ed95966c07d21 (patch) | |
| tree | 24f590f7054e1521eea78661dde3050eb6f02f2a | |
| parent | 9f7b779155387fbd309f1c07b7c7b4c6811a747c (diff) | |
Updated module 1
| -rw-r--r-- | tutorials/module_1/1_classes_and_objects.md | 45 | ||||
| -rw-r--r-- | tutorials/module_1/arrays.ipynb | 209 | ||||
| -rw-r--r-- | tutorials/module_1/basics_of_python.ipynb | 14 | ||||
| -rw-r--r-- | tutorials/module_1/fundamatals_of_programming.ipynb | 6 |
4 files changed, 260 insertions, 14 deletions
diff --git a/tutorials/module_1/1_classes_and_objects.md b/tutorials/module_1/1_classes_and_objects.md index 0392483..ec85633 100644 --- a/tutorials/module_1/1_classes_and_objects.md +++ b/tutorials/module_1/1_classes_and_objects.md @@ -1,8 +1,47 @@ # Modular Programming -Classes and Objects +## 1. Introduction -## What are they? +- A. What is Object-Oriented Programming? +- B. Why use OOP? (vs. procedural) +- C. Real-world analogies (e.g., modeling components like pumps, motors, or vehicles) +--- +## 2. Core OOP Concepts +- A. **Classes and Objects** + - Definitions + - Syntax in Python +- B. **Attributes and Methods** + - Instance variables + - Functions inside classes +- C. **Encapsulation** + - Public vs private variables + - Using `__init__` and `self` +- D. **Inheritance** + - Parent and child classes + - Reuse and extension of code +- E. **Polymorphism** _(brief overview)_ + - Method overriding + - Flexibility in interfaces + +--- +## 3. Python OOP Syntax and Examples +- A. Define a simple class (e.g., `Spring`) +- B. Instantiate objects and use methods +- C. Show `__init__`, `__str__`, custom methods +- D. Add a derived class (e.g., `DampedSpring` inherits from `Spring`) + +--- +## 4. Engineering Applications of OOP +- A. Modeling a mechanical system using classes + - Example: Mass-Spring-Damper system +- B. Creating reusable components (e.g., `Material`, `Beam`, `Force`) +- C. Organizing simulation code with OOP + +--- +## 5. Hands-On Coding Activity +- A. Write a class for a basic physical component (e.g., `Motor`) +- B. Add behavior (e.g., `calculate_torque`) +- C. Extend with inheritance (e.g., `ServoMotor`) +- D. Bonus: Integrate two objects to simulate interaction -## Why do we care? diff --git a/tutorials/module_1/arrays.ipynb b/tutorials/module_1/arrays.ipynb new file mode 100644 index 0000000..0a2afc2 --- /dev/null +++ b/tutorials/module_1/arrays.ipynb @@ -0,0 +1,209 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "22b60740-09ee-4205-ab11-206bbef80709", + "metadata": {}, + "source": [ + "# Arrays\n", + "\n", + "In computer programming, an array is a structure for storing and retrieving data. We often talk about an array as if it were a grid in space, with each cell storing one element of the data. For instance, if each element of the data were a number, we might visualize a “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 as though they were printed on separate pages. If we visualize the position of each element as a position in space. Then we can represent the value of the element as a property. In other words, if we were to analyze the stress concentration of an aluminum block, the property would be stress.\n", + "\n", + "- From [Numpy documentation](https://numpy.org/doc/2.2/user/absolute_beginners.html)\n", + "\n", + "\n", + "If the load on this block changes over time, then we may want to add a 4th dimension i.e. additional sets of 3-D arrays for each time increment. As you can see - the more dimensions we add, the more complicated of a problem we have to solve. It is possible to increase the number of dimensions to the n-th order. This course we will not be going beyond dimensional analysis.\n", + "\n", + "---\n", + "# Numpy - the python's array library\n", + "\n", + "In this tutorial we will be introducing arrays and we will be using the numpy library. Arrays, lists, vectors, matrices, sets - You might've heard of them before, they all store data. In programming, an array is a variable that can hold more than one value at a time. We will be using the Numpy python library to create arrays. Since we already have installed Numpy previously, we can start using the package.\n", + "\n", + "Before importing our first package, let's as ourselves *what is a package?* A package can be thought of as pre-written python code that we can re-use. This means the for every script that we write in python we need to tell it to use a certain package. We call this importing a package.\n", + "\n", + "## Importing Numpy\n", + "When using packages in python, we need to let it know what package we will be using. This is called importing. To import numpy we need to declare it a the start of a script as follows:\n", + "```python\n", + "import numpy as np\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 convention in Python programming to make the code shorter and more readable. We will be using *np* as it's a standard using in many projects.\n", + "\n", + "---\n", + "# Creating arrays\n", + "Now that we have imported the library we can create a one dimensional array or *vector* with three elements.\n", + "```python\n", + "x = np.array([1,2,3])\n", + "```\n", + "\n", + "\n", + "To create a *matrix* we can nest the arrays to create a two dimensional array. This is done as follows.\n", + "\n", + "```python\n", + "matrix = np.array([[1,2,3],\n", + "\t\t\t\t [4,5,6],\n", + "\t\t\t\t [7,8,9]])\n", + "```\n", + "\n", + "*Note: for every array we nest, we get a new dimension in our data structure.*\n", + "\n", + "## Numpy array creation functions\n", + "Numpy comes with some built-in function that we can use to create arrays quickly. Here are a couple of functions that are commonly used in python.\n", + "### np.arange\n", + "The `np.arange()` function returns an array with evenly spaced values within a specified range. It is similar to the built-in `range()` function in Python but returns a Numpy array instead of a list. The parameters for this function are the start value (inclusive), the stop value (exclusive), and the step size. If the step 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 ending before 4, with a step size of 1.\n", + "\n", + "### np.linspace\n", + "The `np.linspace()` function returns an array of evenly spaced values over a specified range. Unlike `np.arange()`, which uses a step size to define the spacing between elements, `np.linspace()` uses the number of values you want to generate and calculates the spacing automatically. It accepts three parameters: the start value, the stop value, and the 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 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", + "- `np.zeros()`\n", + "- `np.ones()`\n", + "- `np.eye()` \n", + "\n", + "## Working with Arrays\n", + "Now that we have been introduced to some ways to create arrays using the Numpy functions let's start using them.\n", + "### Indexing\n", + "Indexing in Python allows you to access specific elements within an array based on their position. This means you can directly retrieve and manipulate individual items as needed.\n", + "\n", + "Python uses **zero-based indexing**, meaning the first element is at position **0** rather than **1**. This approach is common in many programming languages. For example, in a list with five elements, the first element is at index `0`, followed by elements at indices `1`, `2`, `3`, and `4`.\n", + "\n", + "Here's an example of data from a rocket test stand where thrust was 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", + ">>> thrust_lbs[3]\n", + "```\n", + "\n", + "Due to the nature of zero-based indexing. If we want to call the value `4.054973` that will be the 3rd index. \n", + "### Operations on arrays\n", + "- Arithmetic operations (`+`, `-`, `*`, `/`, `**`)\n", + "- `np.add()`, `np.subtract()`, `np.multiply()`, `np.divide()`\n", + "- `np.dot()` for dot product\n", + "- `np.matmul()` for matrix multiplication\n", + "- `np.linalg.inv()`, `np.linalg.det()` for linear algebra\n", + "#### Statistics\n", + "- `np.mean()`, `np.median()`, `np.std()`, `np.var()`\n", + "- `np.min()`, `np.max()`, `np.argmin()`, `np.argmax()`\n", + "- Summation along axes: `np.sum(arr, axis=0)`\n", + "#### Combining arrays\n", + "- Concatenation: `np.concatenate((arr1, arr2), axis=0)`\n", + "- Stacking: `np.vstack()`, `np.hstack()`\n", + "- Splitting: `np.split()`\n", + "\n", + "# Exercise\n", + "Let's solve a statics problem given the following problem\n", + "\n", + "A simply supported bridge of length L=20L = 20L=20 m is subjected to three point loads:\n", + "\n", + "- $P1=10P_1 = 10P1=10 kN$ at $x=5x = 5x=5 m$\n", + "- $P2=15P_2 = 15P2=15 kN$ at $x=10x = 10x=10 m$\n", + "- $P3=20P_3 = 20P3=20 kN$ at $x=15x = 15x=15 m$\n", + "\n", + "The bridge is supported by two reaction forces at points AAA (left support) and BBB (right support). We assume the bridge is in static equilibrium, meaning the sum of forces and sum of moments about any point must be zero.\n", + "\n", + "#### Equilibrium Equations:\n", + "\n", + "1. **Sum of Forces in the Vertical Direction**:\n", + " $RA+RB−P1−P2−P3=0R_A + R_B - P_1 - P_2 - P_3 = 0RA+RB−P1−P2−P3=0$\n", + "2. **Sum of Moments About Point A**:\n", + " $5P1+10P2+15P3−20RB=05 P_1 + 10 P_2 + 15 P_3 - 20 R_B = 05P1+10P2+15P3−20RB=0$\n", + "3. **Sum of Moments About Point B**:\n", + " $20RA−15P3−10P2−5P1=020 R_A - 15 P_3 - 10 P_2 - 5 P_1 = 020RA−15P3−10P2−5P1=0$\n", + "\n", + "#### System of Equations:\n", + "\n", + "{RA+RB−10−15−20=05(10)+10(15)+15(20)−20RB=020RA−5(10)−10(15)−15(20)=0\\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", + "```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", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7f2bcfe9-6e3d-424d-a08f-520682b3bdb5", + "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.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/tutorials/module_1/basics_of_python.ipynb b/tutorials/module_1/basics_of_python.ipynb index 2fb996c..49f7a5a 100644 --- a/tutorials/module_1/basics_of_python.ipynb +++ b/tutorials/module_1/basics_of_python.ipynb @@ -168,7 +168,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 1, "id": "a9bc3bda-e55d-4f5a-813d-8f6316f12c64", "metadata": {}, "outputs": [], @@ -181,7 +181,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 2, "id": "f6af843b-159f-4d10-9c84-abebde7e7bbc", "metadata": {}, "outputs": [ @@ -251,7 +251,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 3, "id": "a7e71be9-dd9a-42c9-84e2-cb5f8f8939b1", "metadata": {}, "outputs": [ @@ -271,14 +271,6 @@ "y = \"Hello\"\n", "print(f' Variable y is type: {type(y)}')" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "d26ad8d0-a96e-4436-aedd-b07ddd7ae696", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/tutorials/module_1/fundamatals_of_programming.ipynb b/tutorials/module_1/fundamatals_of_programming.ipynb new file mode 100644 index 0000000..363fcab --- /dev/null +++ b/tutorials/module_1/fundamatals_of_programming.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 5 +} |
