Skip to content

Commit c957cc6

Browse files
author
Christian Jacobs
committed
Moved the exercises for lecture 4 into the lecture notes.
1 parent 6ddc451 commit c957cc6

File tree

1 file changed

+189
-7
lines changed

1 file changed

+189
-7
lines changed

notebook/Lecture-4-Introduction-to-programming-for-geoscientists.ipynb

Lines changed: 189 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
22
"metadata": {
3-
"name": ""
3+
"name": "Lecture-4-Introduction-to-programming-for-geoscientists"
44
},
55
"nbformat": 3,
66
"nbformat_minor": 0,
77
"worksheets": [
88
{
99
"cells": [
1010
{
11-
"cell_type": "heading",
12-
"level": 1,
11+
"cell_type": "markdown",
1312
"metadata": {},
1413
"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)"
1616
]
1717
},
1818
{
@@ -21,15 +21,18 @@
2121
"source": [
2222
"#Lecture 4: Array computing and curve plotting\n",
2323
"\n",
24-
"[Gerard J. Gorman](http://www.imperial.ac.uk/people/g.gorman) <g.gorman@imperial.ac.uk>\n",
24+
"Learning objectives: \n",
2525
"\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."
2728
]
2829
},
2930
{
3031
"cell_type": "markdown",
3132
"metadata": {},
3233
"source": [
34+
"##Vectors and arrays\n",
35+
"\n",
3336
"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]*."
3437
]
3538
},
@@ -348,7 +351,79 @@
348351
"cell_type": "markdown",
349352
"metadata": {},
350353
"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",
352427
"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:"
353428
]
354429
},
@@ -458,6 +533,22 @@
458533
],
459534
"prompt_number": 16
460535
},
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+
},
461552
{
462553
"cell_type": "markdown",
463554
"metadata": {},
@@ -570,6 +661,43 @@
570661
"cell_type": "markdown",
571662
"metadata": {},
572663
"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",
573701
"We can also plot several curves in one plot:"
574702
]
575703
},
@@ -644,6 +772,22 @@
644772
"For further examples check out the [PyLab website](http://scipy.org/PyLab)."
645773
]
646774
},
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+
},
647791
{
648792
"cell_type": "markdown",
649793
"metadata": {},
@@ -974,6 +1118,44 @@
9741118
}
9751119
],
9761120
"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": []
9771159
}
9781160
],
9791161
"metadata": {}

0 commit comments

Comments
 (0)