Skip to content

Commit 2e8ce25

Browse files
committed
markdown source builds
Auto-generated via `{sandpaper}` Source : 6f16d3b Branch : main Author : scottan <33283688+Scottan@users.noreply.github.com> Time : 2026-04-21 15:38:35 +0000 Message : Merge pull request #87 from UoMResearchIT/18-make-long-calculation-easier-to-read 18 make long calculation easier to read
1 parent 847a262 commit 2e8ce25

2 files changed

Lines changed: 15 additions & 12 deletions

File tree

03-numpy_essential.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -170,32 +170,35 @@ Masked arrays associate a NumPy array with another array composed only of boolea
170170

171171
To demonstrate this we are going to create a Gaussian function and use it to generate an example dataset and generate a plot. We will then add some noise to it and use a masked array to filter out the noisy data. This represents the kind of processing that can be used for datasets such as a seismographs, where we would wish to isolate single events from noisy background data.
172172

173-
Reminder: the Gaussian function is defined by:
173+
*Reminder*: the Gaussian function is defined by:
174174
![](fig/gauss_function.png){alt='Gaussian function equation.'}
175175

176+
where **x** is an array, **µ** is the position of the centre of the curve/peak and **σ** is the width of the bell.
177+
178+
176179
::::::::::::::::::::::::::::::::::::::: challenge
177180

178181
## Gaussian Function
179182

180183
1. Create a function called `gauss` which will take three arguments (inputs):
181184
**x**, **µ**, and **σ**,
182-
as defined above. (x is an array, µ is the position of the centre of the
183-
curve/peak and σ is the width of the bell)
184-
2. Create a NumPy array using the 'numpy' function 'linspace' which will contain 1000
185-
points equally spaced between x=-100 and x=100. Hint: You can print the help
186-
documentation of a function with 'help(name\_of\_the\_function)'
187-
3. Using the above gauss function and the array, create a list which contains the value
188-
of the gauss from x=-100 to x=100.
189-
4. Use the 'matplotlib' library to plot the curve with mu=0 and sigma=10.
185+
as defined above. *Hint*: you may wish to use the NumPy constant `pi`, and NumPy functions `square`, `sqrt` and `exp` for calcuating the square, square-root and exponential of **e** respectively for all elements in an array.
186+
2. Create a NumPy array using the Numpy function `linspace` which will contain 1000
187+
points equally spaced between **x**=-100 and **x**=100. *Hint*: You can print the help
188+
documentation of a function with 'help(name\_of\_the\_function)'.
189+
3. Using the above gauss function and the array, create an array which contains the values
190+
of the Gaussian from **x**=-100 to **x**=100.
191+
4. Use the 'matplotlib' library to plot the curve with **µ**=0 and **σ**=10.
190192

191193
::::::::::::::: solution
192194

193195
## challenge 1:
194196

195197
```python
196198
def gauss(x, mu=0, sigma=1):
197-
return (1. / (np.sqrt(2 * np.pi * sigma ** 2)) *
198-
np.exp( - (x - mu) ** 2 / (2 * sigma ** 2)))
199+
amp = 1. / (np.sqrt(2 * np.pi) * sigma)
200+
inv = 1. / (2. * np.square(sigma))
201+
return amp * np.exp(- np.square(x - mu) * inv)
199202
```
200203

201204
:::::::::::::::::::::::::

md5sum.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"links.md" "8184cf4149eafbf03ce8da8ff0778c14" "site/built/links.md" "2025-04-15"
77
"episodes/01-introduction.md" "45f9d3a720eff2bfc316a2bbe0dafcd5" "site/built/01-introduction.md" "2026-04-21"
88
"episodes/02-dictionaries.md" "01d0b5ca498525f3fff069efae731735" "site/built/02-dictionaries.md" "2026-04-21"
9-
"episodes/03-numpy_essential.md" "6791192a49620934efadddf015fcdc10" "site/built/03-numpy_essential.md" "2026-04-21"
9+
"episodes/03-numpy_essential.md" "b83acf6977ff7f67db725bb90b3a9e80" "site/built/03-numpy_essential.md" "2026-04-21"
1010
"episodes/04-software_package_management.md" "62db2ba2a4291e16d2bdd07b77bfc961" "site/built/04-software_package_management.md" "2025-04-15"
1111
"episodes/05-defensive_programming.md" "906c151b83b81c929caae42af7fc942c" "site/built/05-defensive_programming.md" "2026-04-21"
1212
"episodes/06-units_and_quantities.md" "97935d13cf18749dc8816cea57fc9e3a" "site/built/06-units_and_quantities.md" "2026-04-21"

0 commit comments

Comments
 (0)