|
1 | 1 | {
|
2 | 2 | "metadata": {
|
3 |
| - "name": "" |
| 3 | + "name": "Lecture-4-Introduction-to-programming-for-geoscientists" |
4 | 4 | },
|
5 | 5 | "nbformat": 3,
|
6 | 6 | "nbformat_minor": 0,
|
7 | 7 | "worksheets": [
|
8 | 8 | {
|
9 | 9 | "cells": [
|
10 | 10 | {
|
11 |
| - "cell_type": "heading", |
12 |
| - "level": 1, |
| 11 | + "cell_type": "markdown", |
13 | 12 | "metadata": {},
|
14 | 13 | "source": [
|
15 |
| - "Introduction to programming for Geoscientists (through Python)" |
| 14 | + "#Introduction to programming for Geoscientists (through Python)\n", |
| 15 | + "###[Gerard Gorman](http://www.imperial.ac.uk/people/g.gorman), [Christian Jacobs](http://www.imperial.ac.uk/people/c.jacobs10)" |
16 | 16 | ]
|
17 | 17 | },
|
18 | 18 | {
|
|
21 | 21 | "source": [
|
22 | 22 | "#Lecture 4: Array computing and curve plotting\n",
|
23 | 23 | "\n",
|
24 |
| - "[Gerard J. Gorman](http://www.imperial.ac.uk/people/g.gorman) <g.gorman@imperial.ac.uk>\n", |
| 24 | + "Learning objectives: \n", |
25 | 25 | "\n",
|
26 |
| - "Learning objectives: You will learn how to compute using arrays, *i.e.* vectorise code, and generate 2D graphs." |
| 26 | + "* Learn how to compute using arrays, *i.e.* vectorise code.\n", |
| 27 | + "* Learn how to generate 2D graphs." |
27 | 28 | ]
|
28 | 29 | },
|
29 | 30 | {
|
30 | 31 | "cell_type": "markdown",
|
31 | 32 | "metadata": {},
|
32 | 33 | "source": [
|
| 34 | + "##Vectors and arrays\n", |
| 35 | + "\n", |
33 | 36 | "You have known **vectors** since high school mathematics, *e.g.*, point $(x,y)$ in the plane, point $(x,y,z)$ in space. In general, we can describe a vector $v$ as an $n$-tuple of numbers: $v=(v_0,\\ldots,v_{n-1})$. One way to store vectors in Python is by using *lists*: $v_i$ is stored as *v[i]*."
|
34 | 37 | ]
|
35 | 38 | },
|
|
348 | 351 | "cell_type": "markdown",
|
349 | 352 | "metadata": {},
|
350 | 353 | "source": [
|
351 |
| - "###Generalized array indexing\n", |
| 354 | + "## <span style=\"color:blue\">Exercise 1: Fill lists and arrays with function values</span>\n", |
| 355 | + "A function with many applications in science is defined as:</br></br>\n", |
| 356 | + "$h(x) = \\frac{1}{\\sqrt{2\\pi}}\\exp(-0.5x^2)$</br></br>\n", |
| 357 | + "\n", |
| 358 | + "* Fill two lists *xlist* and *hlist* with *x* and *h(x)* values for uniformly spaced *x* coordinates in [\u22124, 4]. You may adapt the first example in the lecture 4 notes.\n", |
| 359 | + "\n", |
| 360 | + "* Fill two arrays *x* and *y* with *x* and *h(x)* values, respectively, where *h(x)* is defined above. Let the *x* values be uniformly spaced in [\u22124, 4]. Use list comprehensions to create the *x* and *y* arrays.\n", |
| 361 | + "\n", |
| 362 | + "* Vectorize the code by creating the *x* values using the *linspace* function and by evaluating *h(x)* for an array argument." |
| 363 | + ] |
| 364 | + }, |
| 365 | + { |
| 366 | + "cell_type": "code", |
| 367 | + "collapsed": false, |
| 368 | + "input": [], |
| 369 | + "language": "python", |
| 370 | + "metadata": {}, |
| 371 | + "outputs": [] |
| 372 | + }, |
| 373 | + { |
| 374 | + "cell_type": "markdown", |
| 375 | + "metadata": {}, |
| 376 | + "source": [ |
| 377 | + "## <span style=\"color:blue\">Exercise 2: Apply a function to a vector</span>\n", |
| 378 | + "Given a vector $v = (2, 3, \u22121)$ and a function $f(x) = x^3 + xe^x + 1$, apply $f$ to each element in $v$. Then calculate $f(v)$ as $v^3 + ve^v + 1$ using vector computing rules. Show that the two results are equal." |
| 379 | + ] |
| 380 | + }, |
| 381 | + { |
| 382 | + "cell_type": "code", |
| 383 | + "collapsed": false, |
| 384 | + "input": [], |
| 385 | + "language": "python", |
| 386 | + "metadata": {}, |
| 387 | + "outputs": [] |
| 388 | + }, |
| 389 | + { |
| 390 | + "cell_type": "markdown", |
| 391 | + "metadata": {}, |
| 392 | + "source": [ |
| 393 | + "## <span style=\"color:blue\">Exercise 3: Simulate by hand a vectorized expression</span>\n", |
| 394 | + "Suppose *x* and *t* are two arrays of the same length, entering a vectorized expression:" |
| 395 | + ] |
| 396 | + }, |
| 397 | + { |
| 398 | + "cell_type": "code", |
| 399 | + "collapsed": false, |
| 400 | + "input": [ |
| 401 | + "y = cos(sin(x)) + exp(1/t)" |
| 402 | + ], |
| 403 | + "language": "python", |
| 404 | + "metadata": {}, |
| 405 | + "outputs": [] |
| 406 | + }, |
| 407 | + { |
| 408 | + "cell_type": "markdown", |
| 409 | + "metadata": {}, |
| 410 | + "source": [ |
| 411 | + "If *x* holds two elements, 0 and 2, and *t* holds the elements 1 and 1.5, calculate by hand (using a calculator) the *y* array. Thereafter, write a program that mimics the series of computations you did by hand (use explicit loops, but at the end you can use NumPy functionality to check the results)." |
| 412 | + ] |
| 413 | + }, |
| 414 | + { |
| 415 | + "cell_type": "code", |
| 416 | + "collapsed": false, |
| 417 | + "input": [], |
| 418 | + "language": "python", |
| 419 | + "metadata": {}, |
| 420 | + "outputs": [] |
| 421 | + }, |
| 422 | + { |
| 423 | + "cell_type": "markdown", |
| 424 | + "metadata": {}, |
| 425 | + "source": [ |
| 426 | + "##Generalised array indexing\n", |
352 | 427 | "We can select a slice of an array using *a[start:stop:inc]*, where the slice *start:stop:inc* implies a set of indices starting from *start*, up to *stop* in increments of *inc*. In fact, any integer list or array can be used to indicate a set of indices:"
|
353 | 428 | ]
|
354 | 429 | },
|
|
458 | 533 | ],
|
459 | 534 | "prompt_number": 16
|
460 | 535 | },
|
| 536 | + { |
| 537 | + "cell_type": "markdown", |
| 538 | + "metadata": {}, |
| 539 | + "source": [ |
| 540 | + "## <span style=\"color:blue\">Exercise 4: Demonstrate array slicing</span>\n", |
| 541 | + "Create an array *w* with values 0, 0.1, 0.2, ..., 3. Write out *w[:]*, *w[:-2]*, *w[::5]*, *w[2:-2:6]*. Convince yourself in each case that you understand which elements of the array are printed." |
| 542 | + ] |
| 543 | + }, |
| 544 | + { |
| 545 | + "cell_type": "code", |
| 546 | + "collapsed": false, |
| 547 | + "input": [], |
| 548 | + "language": "python", |
| 549 | + "metadata": {}, |
| 550 | + "outputs": [] |
| 551 | + }, |
461 | 552 | {
|
462 | 553 | "cell_type": "markdown",
|
463 | 554 | "metadata": {},
|
|
570 | 661 | "cell_type": "markdown",
|
571 | 662 | "metadata": {},
|
572 | 663 | "source": [
|
| 664 | + "## <span style=\"color:blue\">Exercise 5: Plot a formula</span>\n", |
| 665 | + "* Make a plot of the function $y(t) = v_0t \u2212 0.5gt^2$ for $v_0 = 10$, $g = 9.81$, and $t \\in [0, 2v_0/g]$. The label on the *x* axis should be 'time (s)' and the label on the *y* axis should be 'height (m)'.\n", |
| 666 | + "* Extend the program such that the minimum and maximum *x* and *y* values are computed, and use the extreme values to specify the extent of the *x* and *y* axes. Add some space above the heighest curve." |
| 667 | + ] |
| 668 | + }, |
| 669 | + { |
| 670 | + "cell_type": "code", |
| 671 | + "collapsed": false, |
| 672 | + "input": [], |
| 673 | + "language": "python", |
| 674 | + "metadata": {}, |
| 675 | + "outputs": [] |
| 676 | + }, |
| 677 | + { |
| 678 | + "cell_type": "markdown", |
| 679 | + "metadata": {}, |
| 680 | + "source": [ |
| 681 | + "## <span style=\"color:blue\">Exercise 6: Plot another formula</span>\n", |
| 682 | + "The function</br></br>\n", |
| 683 | + "$f(x, t) = \\exp(-(x - 3t)^2)\\sin(3\\pi(x - t))$\n", |
| 684 | + "</br></br>\n", |
| 685 | + "describes, for a fixed value of *t*, a wave localized in space. Make a program that visualizes this function as a function of *x* on the interval [\u22124, 4] when *t* = 0." |
| 686 | + ] |
| 687 | + }, |
| 688 | + { |
| 689 | + "cell_type": "code", |
| 690 | + "collapsed": false, |
| 691 | + "input": [], |
| 692 | + "language": "python", |
| 693 | + "metadata": {}, |
| 694 | + "outputs": [] |
| 695 | + }, |
| 696 | + { |
| 697 | + "cell_type": "markdown", |
| 698 | + "metadata": {}, |
| 699 | + "source": [ |
| 700 | + "##Multiple curves in one plot\n", |
573 | 701 | "We can also plot several curves in one plot:"
|
574 | 702 | ]
|
575 | 703 | },
|
|
644 | 772 | "For further examples check out the [PyLab website](http://scipy.org/PyLab)."
|
645 | 773 | ]
|
646 | 774 | },
|
| 775 | + { |
| 776 | + "cell_type": "markdown", |
| 777 | + "metadata": {}, |
| 778 | + "source": [ |
| 779 | + "## <span style=\"color:blue\">Exercise 7: Plot a formula for several parameters</span>\n", |
| 780 | + "Make a program that reads a set of $v_0$ values using raw_input and plots the corresponding curves $y(t) = v_0t \u2212 0.5gt^2$ in the same figure (set $g = 9.81$). Let $t \\in [0, 2v_0/g$] for each curve, which implies that you need a different vector of $t$ coordinates for each curve." |
| 781 | + ] |
| 782 | + }, |
| 783 | + { |
| 784 | + "cell_type": "code", |
| 785 | + "collapsed": false, |
| 786 | + "input": [], |
| 787 | + "language": "python", |
| 788 | + "metadata": {}, |
| 789 | + "outputs": [] |
| 790 | + }, |
647 | 791 | {
|
648 | 792 | "cell_type": "markdown",
|
649 | 793 | "metadata": {},
|
|
974 | 1118 | }
|
975 | 1119 | ],
|
976 | 1120 | "prompt_number": 30
|
| 1121 | + }, |
| 1122 | + { |
| 1123 | + "cell_type": "markdown", |
| 1124 | + "metadata": {}, |
| 1125 | + "source": [ |
| 1126 | + "## <span style=\"color:blue\">Exercise 8: Implement matrix-vector multiplication</span>\n", |
| 1127 | + "A matrix $\\mathbf{A}$ and a vector $\\mathbf{b}$, represented in Python as a 2D array and a 1D array respectively, are given by:\n", |
| 1128 | + "\n", |
| 1129 | + "$$\n", |
| 1130 | + "\\mathbf{A} = \\left\\lbrack\\begin{array}{ccc}\n", |
| 1131 | + "0 & 12 & -1\\cr\n", |
| 1132 | + "-1 & -1 & -1\\cr\n", |
| 1133 | + "11 & 5 & 5\n", |
| 1134 | + "\\end{array}\\right\\rbrack\n", |
| 1135 | + "$$\n", |
| 1136 | + "\n", |
| 1137 | + "$$\n", |
| 1138 | + "\\mathbf{b} = \\left\\lbrack\\begin{array}{c}\n", |
| 1139 | + "-2\\cr\n", |
| 1140 | + "1\\cr\n", |
| 1141 | + "7\n", |
| 1142 | + "\\end{array}\\right\\rbrack\n", |
| 1143 | + "$$\n", |
| 1144 | + "\n", |
| 1145 | + "Multiplying a matrix by a vector results in another vector $\\mathbf{c}$, whose components are defined by the general rule\n", |
| 1146 | + "\n", |
| 1147 | + "$$\\mathbf{c}_i = \\sum_j\\mathbf{A}_{i,j}\\mathbf{b}_j$$\n", |
| 1148 | + "\n", |
| 1149 | + "Define $\\mathbf{A}$ and $\\mathbf{b}$ as NumPy arrays, and multiply them together using the above rule." |
| 1150 | + ] |
| 1151 | + }, |
| 1152 | + { |
| 1153 | + "cell_type": "code", |
| 1154 | + "collapsed": false, |
| 1155 | + "input": [], |
| 1156 | + "language": "python", |
| 1157 | + "metadata": {}, |
| 1158 | + "outputs": [] |
977 | 1159 | }
|
978 | 1160 | ],
|
979 | 1161 | "metadata": {}
|
|
0 commit comments