Interpolation Exercise 2


In [173]:
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
sns.set_style('white')

In [174]:
from scipy.interpolate import griddata

Sparse 2d interpolation

In this example the values of a scalar field $f(x,y)$ are known at a very limited set of points in a square domain:

  • The square domain covers the region $x\in[-5,5]$ and $y\in[-5,5]$.
  • The values of $f(x,y)$ are zero on the boundary of the square at integer spaced points.
  • The value of $f$ is known at a single interior point: $f(0,0)=1.0$.
  • The function $f$ is not known at any other points.

Create arrays x, y, f:

  • x should be a 1d array of the x coordinates on the boundary and the 1 interior point.
  • y should be a 1d array of the y coordinates on the boundary and the 1 interior point.
  • f should be a 1d array of the values of f at the corresponding x and y coordinates.

You might find that np.hstack is helpful.


In [175]:
# YOUR CODE HERE


five_1=np.ones(11)*-5
four_1=np.ones(2)*-4
three_1=np.ones(2)*-3
two_1=np.ones(2)*-2
one_1=np.ones(2)*-1
zero=np.ones(3)*0
five=np.ones(11)*5
four=np.ones(2)*4
three=np.ones(2)*3
two=np.ones(2)*2
one=np.ones(2)*1
y=np.linspace(-5,5,11)

norm=np.array((-5,5))
mid=np.array((-5,0,5))

x=np.hstack((five_1,four_1,three_1,two_1,one_1,zero,one,two,three,four,five))
y=np.hstack((y,norm,norm,norm,norm,mid,norm,norm,norm,norm,y))


def func(x,y):
    t=np.zeros(len(x))
    t[(len(x)/2)]=1
    return t
f=func(x,y)
f


Out[175]:
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.])

In [176]:
#The following plot should show the points on the boundary and the single point in the interior:

In [177]:
fig=plt.figure()
plt.scatter(x, y);
plt.grid()



In [178]:
assert x.shape==(41,)
assert y.shape==(41,)
assert f.shape==(41,)
assert np.count_nonzero(f)==1

Use meshgrid and griddata to interpolate the function $f(x,y)$ on the entire square domain:

  • xnew and ynew should be 1d arrays with 100 points between $[-5,5]$.
  • Xnew and Ynew should be 2d versions of xnew and ynew created by meshgrid.
  • Fnew should be a 2d array with the interpolated values of $f(x,y)$ at the points (Xnew,Ynew).
  • Use cubic spline interpolation.

In [191]:
# YOUR CODE HERE
from scipy.interpolate import interp2d 

xnew=np.linspace(-5,5,100)
ynew=np.linspace(-5,5,100)

Xnew, Ynew = np.meshgrid(xnew,ynew)

Fnew=griddata((x,y),f,(Xnew,Ynew),method='cubic')

In [192]:
assert xnew.shape==(100,)
assert ynew.shape==(100,)
assert Xnew.shape==(100,100)
assert Ynew.shape==(100,100)
assert Fnew.shape==(100,100)

Plot the values of the interpolated scalar field using a contour plot. Customize your plot to make it effective and beautiful.


In [201]:
# YOUR CODE HERE
plt.figure(figsize=(6,6))
cont=plt.contour(Xnew,Ynew,Fnew, colors=('k','k'))
plt.title("Contour Map of F(x)")
plt.ylabel("Y-Axis")
plt.xlabel('X-Axis')
# plt.colorbar()
plt.clabel(cont, inline=1, fontsize=10)
plt.xlim(-5.5,5.5);
plt.ylim(-5.5,5.5);
# plt.grid()


Out[201]:
(-5.5, 5.5)

In [194]:
assert True # leave this to grade the plot

In [ ]: