Interpolation Exercises


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.

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.


Question 1

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.

First, set up a grid and evaluate the function.


In [ ]:
from Electrostatics import *
x,y = np.meshgrid(np.arange(-5,5.01,0.5),np.arange(-5,5.01,0.5))
Vdipole = dipole_potential(x,y,1e-9,2.)
plt.contour(x,y,Vdipole,cmap=cm.coolwarm)
plt.show()

(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$.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

(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).


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

(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?


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

Question 2

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$.

temp   res   temp   res   temp   res   temp   res   temp   res
--------------------------------------------------------------- 
 10   3.239   16   2.610   22   2.169   28   1.857   34   1.634
 11   3.118   17   2.526   23   2.110   29   1.815   35   1.603
 12   3.004   18   2.446   24   2.053   30   1.774   36   1.574
 13   2.897   19   2.371   25   2.000   31   1.736   37   1.547
 14   2.795   20   2.300   26   1.950   32   1.700   38   1.521
 15   2.700   21   2.233   27   1.902   33   1.666   39   1.496

(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.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

(b) Show that the function returns expected values by printing the temperature for a few different resistance values, including some from the table.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

(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$.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

All content is under a modified MIT License, and can be freely used and adapted. See the full license text here.