1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# Arrays
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:
| 1 | 5 | 2 | 0 |
| --- | --- | --- | --- |
A two-dimensional array which can be would be like a table:
| 1 | 5 | 2 | 0 |
| --- | --- | --- | --- |
| 8 | 3 | 6 | 1 |
| 1 | 7 | 2 | 9 |
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.
- From [Numpy documentation](https://numpy.org/doc/2.2/user/absolute_beginners.html)

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.
## Numpy - the python's array library
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.
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.
### Importing Numpy
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:
```python
import numpy as np
```
- `import` - calls for a library to use, in our case it is Numpy.
- `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.
## Creating arrays
Now that we have imported the library we can create a one dimensional array or *vector* with three elements.
```python
x = np.array([1,2,3])
```
To create a *matrix* we can nest the arrays to create a two dimensional array. This is done as follows.
```python
matrix = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
```
*Note: for every array we nest, we get a new dimension in our data structure.*
### Numpy array creation functions
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.
#### np.arange
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.
```python
>>> np.arange(4)
array([0. , 1., 2., 3. ])
```
In this example, `np.arange(4)` generates an array starting from 0 and ending before 4, with a step size of 1.
#### np.linspace
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.
```python
>>> np.linspace(1., 4., 6)
array([1. , 1.6, 2.2, 2.8, 3.4, 4. ])
```
In this example, `np.linspace(1., 4., 6)` generates 6 evenly spaced values between 1. and 4., including both endpoints.
Try this and see what happens:
```python
x = np.linspace(0,100,101)
y = np.sin(x)
```
#### Other useful functions
- `np.zeros()`
- `np.ones()`
- `np.eye()`
### Working with Arrays
Now that we have been introduced to some ways to create arrays using the Numpy functions let's start using them.
#### Indexing
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.
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`.
Here's an example of data from a rocket test stand where thrust was recorded as a function of time.
```python
thrust_lbf = np.array(0.603355, 2.019083, 2.808092, 4.054973, 1.136618, 0.943668)
print(thrust_lbs[3])
```
Due to the nature of zero-based indexing. If we want to call the value `4.054973` that will be the 3rd index.
#### Operations on arrays
- Arithmetic operations (`+`, `-`, `*`, `/`, `**`)
- `np.add()`, `np.subtract()`, `np.multiply()`, `np.divide()`
- `np.dot()` for dot product
- `np.matmul()` for matrix multiplication
- `np.linalg.inv()`, `np.linalg.det()` for linear algebra
##### Statistics
- `np.mean()`, `np.median()`, `np.std()`, `np.var()`
- `np.min()`, `np.max()`, `np.argmin()`, `np.argmax()`
- Summation along axes: `np.sum(arr, axis=0)`
## Exercise
Let's solve a statics problem given the following problem
A simply supported bridge of length L = 20 m is subjected to three point loads:
- $P_1 = 10 kN$ at $x = 5 m$
- $P_2 = 15 kN$ at $x = 10 m$
- $P_3 = 20 kN$ at $x = 15 m$
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.
##### Equilibrium Equations:
1. **Sum of Forces in the Vertical Direction**:
$R_A + R_B - P_1 - P_2 - P_3 = 0$
2. **Sum of Moments About Point A**:
$5 P_1 + 10 P_2 + 15 P_3 - 20 R_B = 0$
3. **Sum of Moments About Point B**:
$20 R_A - 15 P_3 - 10 P_2 - 5 P_1 = 0$
##### System of Equations:
$$
\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}
$$
### Solution
```python
import numpy as np
# Define the coefficient matrix A
A = np.array([
[1, 1],
[0, -20],
[20, 0]
])
# Define the right-hand side vector b
b = np.array([
45,
5*10 + 10*15 + 15*20,
5*10 + 10*15 + 15*20
])
# Solve the system of equations Ax = b
x = np.linalg.lstsq(A, b, rcond=None)[0] # Using least squares to handle potential overdetermination
# Display the results
print(f"Reaction force at A (R_A): {x[0]:.2f} kN")
print(f"Reaction force at B (R_B): {x[1]:.2f} kN")
```
|