|
84 | 84 | " else:\n",
|
85 | 85 | " f = np.exp(-x)\n",
|
86 | 86 | " return f\n",
|
| 87 | + "\n", |
87 | 88 | "print(func(3))"
|
88 | 89 | ]
|
89 | 90 | },
|
|
115 | 116 | "\n",
|
116 | 117 | "`func(` \n",
|
117 | 118 | "\n",
|
118 |
| - "and hit [shift][tab] to see the additional documentation. Warning: don't leave a code cell with just `func(` or `func()` as you will get an error on [Kernel][Restart & Run All]." |
| 119 | + "and hit [shift][tab] to see the additional documentation. Warning: don't leave a code cell with just `func(` or `func()` as you will get an error on [Kernel][Restart & Run All Cells]." |
119 | 120 | ]
|
120 | 121 | },
|
121 | 122 | {
|
|
145 | 146 | "cell_type": "markdown",
|
146 | 147 | "metadata": {},
|
147 | 148 | "source": [
|
148 |
| - "The names of the arguments of a function are the names used inside the function. They have no relationship to the names used outside the function. When using a variable as the argument of a function, only the *value* gets passed to the function. In the example below, the *value* of `y` is passed as the first argument ot the function `func`. Inside the function, this value is used for the variable `x`." |
| 149 | + "The names of the arguments of a function are the names used inside the function. They have no relationship to the names used outside the function. When using a variable as the argument of a function, only the *value* gets passed to the function. In the example below, the *value* of `y` is passed as the first argument of the function `func`. Inside the function, this value is used for the variable `x`." |
149 | 150 | ]
|
150 | 151 | },
|
151 | 152 | {
|
|
167 | 168 | "\n",
|
168 | 169 | "$f(x)=e^{-\\alpha x}\\cos(x)$\n",
|
169 | 170 | "\n",
|
170 |
| - "The function should take `x` and `alpha` as input arguments and return the function value. Give your function a unique name (if you also call it `func` it will overwrite the `func` function that we defined above). Make a plot of `f` vs. `x` for `x` going from 0 to $10\\pi$ using two different values of `alpha`: 0.1 and 0.2. Add a legend and label the axes." |
| 171 | + "The function should take `x` and `alpha` as input arguments and return the function value. Give your function a unique name (if you also call it `func` it will overwrite the `func` function that we defined above). Make a plot of $f(x)$ vs. $x$ for $x$ going from 0 to $10\\pi$ using two different values of $\\alpha$: 0.1 and 0.2. Add a legend and label the axes." |
171 | 172 | ]
|
172 | 173 | },
|
173 | 174 | {
|
|
195 | 196 | {
|
196 | 197 | "cell_type": "code",
|
197 | 198 | "execution_count": null,
|
198 |
| - "metadata": { |
199 |
| - "scrolled": true |
200 |
| - }, |
| 199 | + "metadata": {}, |
201 | 200 | "outputs": [],
|
202 | 201 | "source": [
|
203 | 202 | "def testfunc(x, A=1, theta=0):\n",
|
204 | 203 | " return A * np.cos(np.pi * x + theta)\n",
|
205 | 204 | "\n",
|
206 | 205 | "print(testfunc(1)) # Uses default A=1, theta=0: cos(pi)\n",
|
207 | 206 | "print(testfunc(1, A=2)) # Now A=2, and theta is still 0: 2*cos(pi)\n",
|
208 |
| - "print(testfunc(1, A=2, theta=np.pi / 4)) # Now A=2, theta=pi/4: 2*cos(5pi/4) \n", |
| 207 | + "print(testfunc(1, A=2, theta=np.pi / 4)) # Now A=2, theta=pi/4: 2*cos(5pi/4)\n", |
209 | 208 | "print(testfunc(1, theta=np.pi / 4, A=2)) # Same as above: 2*cos(5pi/4)\n",
|
210 | 209 | "print(testfunc(1, theta=np.pi / 4)) # Now theta=pi/4, and A is still 1: cos(5pi/4)"
|
211 | 210 | ]
|
212 | 211 | },
|
| 212 | + { |
| 213 | + "cell_type": "markdown", |
| 214 | + "metadata": {}, |
| 215 | + "source": [ |
| 216 | + "Note that the proper style was applied, as defined in Notebook 1: there are spaces around mathematical symbols, but not around the equal sign of the keyword argument. " |
| 217 | + ] |
| 218 | + }, |
213 | 219 | {
|
214 | 220 | "cell_type": "markdown",
|
215 | 221 | "metadata": {},
|
216 | 222 | "source": [
|
217 | 223 | "### Local variables\n",
|
218 |
| - "Variables declared inside a function can only be used inside that function. The outside of a function doesn't know about the variables used inside the function, except for the variables that are returned by the function. In the code below, remove the `#` before `print(a)` and you will get an error message, as `a` is a local variable inside the function `localtest` (then put the `#` back, else you get an error when running [Kernel][Restart & Run All])." |
| 224 | + "Variables declared inside a function can only be used inside that function. The outside of a function doesn't know about the variables used inside the function, except for the variables that are returned by the function. In the code below, remove the `#` before `print(a)` and you will get an error message, as `a` is a local variable inside the function `localtest` (then put the `#` back, else you get an error when running [Kernel][Restart & Run All Cells])." |
219 | 225 | ]
|
220 | 226 | },
|
221 | 227 | {
|
|
253 | 259 | " a = 3\n",
|
254 | 260 | " b = 5\n",
|
255 | 261 | " return a * x + b\n",
|
| 262 | + "\n", |
256 | 263 | "print(test1(4))\n",
|
257 | 264 | "\n",
|
258 | 265 | "# This function also works, but it is sloppy coding\n",
|
259 | 266 | "# since variable a is defined outside the function\n",
|
260 | 267 | "a = 3\n",
|
| 268 | + "\n", |
261 | 269 | "def test2(x):\n",
|
262 | 270 | " b = 5\n",
|
263 | 271 | " return a * x + b\n",
|
| 272 | + "\n", |
264 | 273 | "print(test2(4)) "
|
265 | 274 | ]
|
266 | 275 | },
|
|
278 | 287 | "outputs": [],
|
279 | 288 | "source": [
|
280 | 289 | "var1 = 8\n",
|
| 290 | + "\n", |
281 | 291 | "def test3():\n",
|
282 | 292 | " var1 = 4\n",
|
283 | 293 | " print('Inside the function test3, var1 equals:', var1)\n",
|
| 294 | + " \n", |
284 | 295 | "test3()\n",
|
285 | 296 | "print('Value of var1 outside test3:', var1)"
|
286 | 297 | ]
|
|
348 | 359 | " else:\n",
|
349 | 360 | " f = np.exp(-x)\n",
|
350 | 361 | " return f\n",
|
| 362 | + "\n", |
351 | 363 | "x = np.linspace(-6, 6, 100)\n",
|
352 | 364 | "#y = func(x) # Run this line after removing the # to see the error that occurs. Then put the # back"
|
353 | 365 | ]
|
|
364 | 376 | "\n",
|
365 | 377 | "`The truth value of an array with more than one element is ambiguous` \n",
|
366 | 378 | "\n",
|
367 |
| - "For some values of `x` the `if` statement may be `True`, for others it may be `False`. A simple way around this problem is to vectorize the function. That means we create a new function, let's call it `funcvec`, that is a vectorized form of `func` and can be called with an array as an argument (this is by far the easiest but not necessarily the computationally fastest way to make sure a function can be called with an array as an argument)" |
| 379 | + "For some values of `x` the `if` statement may be `True`, for others it may be `False`. A simple way around this problem is to vectorize the function. That means we create a new function, let's call it `funcvec`, that is a vectorized form of `func` and can be called with an array as an argument. This is by far the easiest but not necessarily the computationally fastest way to make sure a function can be called with an array as an argument, and, unfortunately, it won't work for all situations. " |
368 | 380 | ]
|
369 | 381 | },
|
370 | 382 | {
|
|
383 | 395 | "cell_type": "markdown",
|
384 | 396 | "metadata": {},
|
385 | 397 | "source": [
|
386 |
| - "Back now to the problem of flow around a clinder. Contours of the stream function represent stream lines around the cylinder. To make a contour plot, the function to be contoured needs to be evaluated on a grid of points. The grid of points and an array with the values of the stream function at these points can be passed to a contouring routine to create a contour plot. To create a grid of points, use the function `meshgrid` which takes as input a range of `x` values and a range of `y` values, and returns a grid of `x` values and a grid of `y` values. For example, to have 5 points in the $x$-direction from -1 to +1, and 3 points in y-direction from 0 to 10:" |
| 398 | + "Back now to the problem of flow around a clinder. Contours of the stream function represent stream lines around the cylinder. To make a contour plot, the function to be contoured needs to be evaluated on a grid of points. The grid of points and an array with the values of the stream function at these points can be passed to a contouring routine to create a contour plot. To create a grid of points, use the function `meshgrid` which takes as input an array of `x` values and an array of `y` values, and returns a grid of `x` values and a grid of `y` values. For example, to have 5 points in the $x$-direction from -1 to +1, and 3 points in y-direction from 0 to 10:" |
387 | 399 | ]
|
388 | 400 | },
|
389 | 401 | {
|
|
392 | 404 | "metadata": {},
|
393 | 405 | "outputs": [],
|
394 | 406 | "source": [
|
395 |
| - "x,y = np.meshgrid( np.linspace(-1,1,5), np.linspace(0,10,3) ) \n", |
| 407 | + "x,y = np.meshgrid(np.linspace(-1, 1, 5), np.linspace(0, 10, 3)) \n", |
396 | 408 | "print('x values')\n",
|
397 | 409 | "print(x)\n",
|
398 | 410 | "print('y values')\n",
|
|
438 | 450 | "a, b = 4, 3\n",
|
439 | 451 | "print('a:', a)\n",
|
440 | 452 | "print('b:', b)\n",
|
| 453 | + "\n", |
441 | 454 | "a, b, c = 27, np.arange(4), 'hello'\n",
|
442 | 455 | "print('a:', a)\n",
|
443 | 456 | "print('b:', b)\n",
|
444 | 457 | "print('c:', c)\n",
|
| 458 | + "\n", |
445 | 459 | "d, e, f = np.arange(0, 11, 5)\n",
|
446 | 460 | "print('d:', d)\n",
|
447 | 461 | "print('e:', e)\n",
|
|
469 | 483 | "test = newfunc()\n",
|
470 | 484 | "print(type(test))\n",
|
471 | 485 | "print(test[1]) \n",
|
| 486 | + "\n", |
472 | 487 | "a, b, c = newfunc()\n",
|
473 | 488 | "print('a:', a)\n",
|
474 | 489 | "print('b:', b)\n",
|
|
647 | 662 | "cell_type": "markdown",
|
648 | 663 | "metadata": {},
|
649 | 664 | "source": [
|
650 |
| - "The `interact` function of `ipywidgets` can now be used to interact with the `plot_line` function we just defined. The `interact` function takes as input arguments the name of the function to interact with, and then the minimum value, maximum value and step of the input arguments. When you execute the code below, a slider will appear and you can move the slider to change the value of the angle $\\alpha$ (and again, it is a bit slow, so don't move it around too quickly). " |
| 665 | + "The `interact` function of `ipywidgets` can now be used to interact with the `plot_line` function we just defined. The `interact` function takes as input arguments the name of the function to interact with, and then the minimum value, maximum value, and step of the input arguments. When you execute the code below, a slider will appear and you can move the slider to change the value of the angle $\\alpha$ (and again, it is a bit slow, so don't move it around too quickly). " |
651 | 666 | ]
|
652 | 667 | },
|
653 | 668 | {
|
|
975 | 990 | "name": "python",
|
976 | 991 | "nbconvert_exporter": "python",
|
977 | 992 | "pygments_lexer": "ipython3",
|
978 |
| - "version": "3.7.0" |
| 993 | + "version": "3.8.2" |
979 | 994 | },
|
980 | 995 | "varInspector": {
|
981 | 996 | "cols": {
|
|
1012 | 1027 | }
|
1013 | 1028 | },
|
1014 | 1029 | "nbformat": 4,
|
1015 |
| - "nbformat_minor": 1 |
| 1030 | + "nbformat_minor": 4 |
1016 | 1031 | }
|
0 commit comments