Skip to content

Commit 6f16d3b

Browse files
authored
Merge pull request #87 from UoMResearchIT/18-make-long-calculation-easier-to-read
18 make long calculation easier to read
2 parents caa7f89 + 6eb6903 commit 6f16d3b

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

episodes/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
:::::::::::::::::::::::::

0 commit comments

Comments
 (0)