{ "metadata": { "name": "", "signature": "sha256:b08cd375126263c673cc2a4efcf9626cf69ad80b03be90bc937a45eb4f6b2031" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ " Interpolation Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Instructions:** Create a new notebook called `InterpolationExercises` in your `Interpolation` directory and solve the following problems inside it. Be sure to include the problem statements in a markdown cell above your solution. You don't need to put the \"helper\" code in the markdown cell, just implement the helper code in your code cell with your solution." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Preliminaries:** At the top of your notebook, include a \"Heading 1\" cell with the title **Interpolation Exercises**. Then include the inline functions and libraries by adding a code cell that invokes the `%pylab inline` magic and imports the needed packages." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Question 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is common to test functions where you know the answer they should give. Let's test the `dipole_potential()` function that we previously saved in `Electrostatics.py` to see that the function is zero along the bisector between the charges. \n", "\n", "First, set up a grid and evaluate the function.\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from Electrostatics import *\n", "x,y = np.meshgrid(np.arange(-5,5.01,0.5),np.arange(-5,5.01,0.5))\n", "Vdipole = dipole_potential(x,y,1e-9,2.)\n", "plt.contour(x,y,Vdipole,cmap=cm.coolwarm)\n", "plt.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**(a)** Use a boolean mask to locate the positions in the array where $x=0$. Print the corresponding $y$ values. Look at the contour plot. This corresponds to a line through the center of the plot from the top to bottom at $x=0$. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**(b)** Show with your mask that $V_{dipole}$ for these locations is zero. i.e. print the value of $V_{dipole}$ for these locations using your mask from part (a)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**(c)** Now repeat parts (a) and (b) but this time set up the mask to locate the positions in the array where $y=0$. This represents a line through the center of the plot from left to right at $y=0$. Do the values for $V_{dipole}$ make sense?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Question 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Thermistors are electrical devices that measure temperature. They are made out of a material whose resistance changes very predictably with temperature. In the lab we measure the resistance of the thermistor and then compute the temperature from the engineering data that the manufacturer sends with the devices. The table below is data for a set of thermistors used in a laboratory experiment. The temperature values are in $^{\\circ}$C and the resistances in mega-Ohms, M$\\Omega$.\n", "\n", " temp res temp res temp res temp res temp res\n", " --------------------------------------------------------------- \n", " 10 3.239 16 2.610 22 2.169 28 1.857 34 1.634\n", " 11 3.118 17 2.526 23 2.110 29 1.815 35 1.603\n", " 12 3.004 18 2.446 24 2.053 30 1.774 36 1.574\n", " 13 2.897 19 2.371 25 2.000 31 1.736 37 1.547\n", " 14 2.795 20 2.300 26 1.950 32 1.700 38 1.521\n", " 15 2.700 21 2.233 27 1.902 33 1.666 39 1.496\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**(a)** Let's turn this look-up table into a continuous function. Write a reusable function `temp(resistance)` that takes any value of `resistance` and returns the interpolated temperature. Be sure to include a docstring in your function. Check the documentation for `interp1d` to make sure you set up the arrays properly." ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**(b)** Show that the function returns expected values by printing the temperature for a few different resistance values, including some from the table." ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**(c)** Create a new array `resistance` from 1.5 to 3.2 in 100 steps. Plot the interpolated temperatures `temp(resistance)` as a function of `resistance`. Include as an annotation on the plot the value of the temperature for a resistance of 1.75 M$\\Omega$." ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " All content is under a modified MIT License, and can be freely used and adapted. See the full license text [here](../../LICENSE)." ] } ], "metadata": {} } ] }