{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to programming for Geoscientists (through Python)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Lecture 3 solutions\n", "## Gerard J. Gorman (g.gorman@imperial.ac.uk) http://www.imperial.ac.uk/people/g.gorman" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 3.1: Make a table of function values\n", "\n", "Write a program that prints a table with $t$ values in the first column and the corresponding $y(t) = v_0 t − 0.5gt^2$ values in the second column.\n", "Use $n$ uniformly spaced $t$ values throughout the interval [0, $2v_0/g$]. Set $v0 = 1$, $g = 9.81$, and $n = 11$." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.000000, 0.000000\n", "0.020387, 0.018349\n", "0.040775, 0.032620\n", "0.061162, 0.042813\n", "0.081549, 0.048930\n", "0.101937, 0.050968\n", "0.122324, 0.048930\n", "0.142712, 0.042813\n", "0.163099, 0.032620\n", "0.183486, 0.018349\n", "0.203874, 0.000000\n" ] } ], "source": [ "# Make a table of function values\n", "\n", "v0 = 1.0\n", "g = 9.81\n", "n = 11\n", "\n", "# The step size between each uniformly spaced value. \n", "# The end-points of the interval are included.\n", "step = (2*v0/g)/(n-1) \n", "\n", "for i in range(0, n):\n", " t = step*i\n", " y = v0*t - 0.5*g*t**2\n", " print(\"%f, %f\" % (t, y))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Modify the program so that the $t$ and $y$ values are stored in two lists *t* and *y*. Thereafter, transverse the lists with a *for* loop and write out a nicely formatted table of *t* and *y* values using a *zip* construction." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.000000, 0.000000\n", "0.020387, 0.018349\n", "0.040775, 0.032620\n", "0.061162, 0.042813\n", "0.081549, 0.048930\n", "0.101937, 0.050968\n", "0.122324, 0.048930\n", "0.142712, 0.042813\n", "0.163099, 0.032620\n", "0.183486, 0.018349\n", "0.203874, 0.000000\n" ] } ], "source": [ "# Use list comprehension to build the lists\n", "t = [step*i for i in range(n)]\n", "y = [v0*ti - 0.5*g*ti**2 for ti in t]\n", "\n", "for (T,Y) in zip(t,y):\n", " print(\"%f, %f\" % (T, Y))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 3.2: Implement a Gaussian function\n", "\n", "Make a Python function *gauss*( *x*, *m*=0, *s*=1) for computing the Gaussian function \n", "$$f(x)=\\frac{1}{\\sqrt{2\\pi}s}\\exp\\left(-\\frac{1}{2} \\left(\\frac{x-m}{s}\\right)^2\\right)$$\n", "Call the function and print out the result for x equal to −5, −4.9, −4.8, ..., 4.8, 4.9, 5, using default values for *m* and *s*." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "For x = -5.000000, gaussian(x) = 0.000001\n", "For x = -4.900000, gaussian(x) = 0.000002\n", "For x = -4.800000, gaussian(x) = 0.000004\n", "For x = -4.700000, gaussian(x) = 0.000006\n", "For x = -4.600000, gaussian(x) = 0.000010\n", "For x = -4.500000, gaussian(x) = 0.000016\n", "For x = -4.400000, gaussian(x) = 0.000025\n", "For x = -4.300000, gaussian(x) = 0.000039\n", "For x = -4.200000, gaussian(x) = 0.000059\n", "For x = -4.100000, gaussian(x) = 0.000089\n", "For x = -4.000000, gaussian(x) = 0.000134\n", "For x = -3.900000, gaussian(x) = 0.000199\n", "For x = -3.800000, gaussian(x) = 0.000292\n", "For x = -3.700000, gaussian(x) = 0.000425\n", "For x = -3.600000, gaussian(x) = 0.000612\n", "For x = -3.500000, gaussian(x) = 0.000873\n", "For x = -3.400000, gaussian(x) = 0.001232\n", "For x = -3.300000, gaussian(x) = 0.001723\n", "For x = -3.200000, gaussian(x) = 0.002384\n", "For x = -3.100000, gaussian(x) = 0.003267\n", "For x = -3.000000, gaussian(x) = 0.004432\n", "For x = -2.900000, gaussian(x) = 0.005953\n", "For x = -2.800000, gaussian(x) = 0.007915\n", "For x = -2.700000, gaussian(x) = 0.010421\n", "For x = -2.600000, gaussian(x) = 0.013583\n", "For x = -2.500000, gaussian(x) = 0.017528\n", "For x = -2.400000, gaussian(x) = 0.022395\n", "For x = -2.300000, gaussian(x) = 0.028327\n", "For x = -2.200000, gaussian(x) = 0.035475\n", "For x = -2.100000, gaussian(x) = 0.043984\n", "For x = -2.000000, gaussian(x) = 0.053991\n", "For x = -1.900000, gaussian(x) = 0.065616\n", "For x = -1.800000, gaussian(x) = 0.078950\n", "For x = -1.700000, gaussian(x) = 0.094049\n", "For x = -1.600000, gaussian(x) = 0.110921\n", "For x = -1.500000, gaussian(x) = 0.129518\n", "For x = -1.400000, gaussian(x) = 0.149727\n", "For x = -1.300000, gaussian(x) = 0.171369\n", "For x = -1.200000, gaussian(x) = 0.194186\n", "For x = -1.100000, gaussian(x) = 0.217852\n", "For x = -1.000000, gaussian(x) = 0.241971\n", "For x = -0.900000, gaussian(x) = 0.266085\n", "For x = -0.800000, gaussian(x) = 0.289692\n", "For x = -0.700000, gaussian(x) = 0.312254\n", "For x = -0.600000, gaussian(x) = 0.333225\n", "For x = -0.500000, gaussian(x) = 0.352065\n", "For x = -0.400000, gaussian(x) = 0.368270\n", "For x = -0.300000, gaussian(x) = 0.381388\n", "For x = -0.200000, gaussian(x) = 0.391043\n", "For x = -0.100000, gaussian(x) = 0.396953\n", "For x = -0.000000, gaussian(x) = 0.398942\n", "For x = 0.100000, gaussian(x) = 0.396953\n", "For x = 0.200000, gaussian(x) = 0.391043\n", "For x = 0.300000, gaussian(x) = 0.381388\n", "For x = 0.400000, gaussian(x) = 0.368270\n", "For x = 0.500000, gaussian(x) = 0.352065\n", "For x = 0.600000, gaussian(x) = 0.333225\n", "For x = 0.700000, gaussian(x) = 0.312254\n", "For x = 0.800000, gaussian(x) = 0.289692\n", "For x = 0.900000, gaussian(x) = 0.266085\n", "For x = 1.000000, gaussian(x) = 0.241971\n", "For x = 1.100000, gaussian(x) = 0.217852\n", "For x = 1.200000, gaussian(x) = 0.194186\n", "For x = 1.300000, gaussian(x) = 0.171369\n", "For x = 1.400000, gaussian(x) = 0.149727\n", "For x = 1.500000, gaussian(x) = 0.129518\n", "For x = 1.600000, gaussian(x) = 0.110921\n", "For x = 1.700000, gaussian(x) = 0.094049\n", "For x = 1.800000, gaussian(x) = 0.078950\n", "For x = 1.900000, gaussian(x) = 0.065616\n", "For x = 2.000000, gaussian(x) = 0.053991\n", "For x = 2.100000, gaussian(x) = 0.043984\n", "For x = 2.200000, gaussian(x) = 0.035475\n", "For x = 2.300000, gaussian(x) = 0.028327\n", "For x = 2.400000, gaussian(x) = 0.022395\n", "For x = 2.500000, gaussian(x) = 0.017528\n", "For x = 2.600000, gaussian(x) = 0.013583\n", "For x = 2.700000, gaussian(x) = 0.010421\n", "For x = 2.800000, gaussian(x) = 0.007915\n", "For x = 2.900000, gaussian(x) = 0.005953\n", "For x = 3.000000, gaussian(x) = 0.004432\n", "For x = 3.100000, gaussian(x) = 0.003267\n", "For x = 3.200000, gaussian(x) = 0.002384\n", "For x = 3.300000, gaussian(x) = 0.001723\n", "For x = 3.400000, gaussian(x) = 0.001232\n", "For x = 3.500000, gaussian(x) = 0.000873\n", "For x = 3.600000, gaussian(x) = 0.000612\n", "For x = 3.700000, gaussian(x) = 0.000425\n", "For x = 3.800000, gaussian(x) = 0.000292\n", "For x = 3.900000, gaussian(x) = 0.000199\n", "For x = 4.000000, gaussian(x) = 0.000134\n", "For x = 4.100000, gaussian(x) = 0.000089\n", "For x = 4.200000, gaussian(x) = 0.000059\n", "For x = 4.300000, gaussian(x) = 0.000039\n", "For x = 4.400000, gaussian(x) = 0.000025\n", "For x = 4.500000, gaussian(x) = 0.000016\n", "For x = 4.600000, gaussian(x) = 0.000010\n", "For x = 4.700000, gaussian(x) = 0.000006\n", "For x = 4.800000, gaussian(x) = 0.000004\n", "For x = 4.900000, gaussian(x) = 0.000002\n", "For x = 5.000000, gaussian(x) = 0.000001\n" ] } ], "source": [ "# Implement a Gaussian function\n", "\n", "from math import exp, pi, sqrt\n", "\n", "def gaussian(x, m = 0, s = 1):\n", " # Note: 'm' is the mean, and 's' is the standard deviation.\n", " coefficient = 1/(sqrt(2*pi)*s)\n", " result = coefficient*exp(-0.5*(float(x - m)/s)**2) # Be careful here. x, m, and s are supplied as integers - let's cast the numerator to a float to ensure we don't encounter integer division problems.\n", " return result\n", "\n", "x = -5.0\n", "while x <= 5.0:\n", " print(\"For x = %f, gaussian(x) = %f\" % (x, gaussian(x)))\n", " x = x + 0.1 # Increment x by a step size of 0.1." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 3.3: Express a step function as a Python function\n", "\n", "The following \"step\" function is known as the Heaviside function and\n", "is widely used in mathematics:\n", "$$H(x)=\\begin{cases}0, & \\text{if $x<0$}.\\\\\\\\\n", "1, & \\text{if $x\\ge 0$}.\\end{cases}$$\n", "Write a Python function H(x) that computes H(x)." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "# Express a step function as a Python function\n", "\n", "def step(x):\n", " if(x < 0):\n", " H = 0\n", " else: # Otherwise x must be greater than or equal to zero\n", " H = 1\n", " return H # Return the result\n", "\n", "x = 0.5\n", "print(step(x)) # The value that is returned by the function will be printed out here. For the case of x = 0.5, this should print '1'." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 1 }