In this exercise you will check how different interpolation methods work with different functions, after writing some diagnostic functions.
Review the code in the lecture notebook to refresh your memory about
Fix the interpolation window $ x \in [0,25] $, the interpolation nodes as in the code below, and only change the interpolated function and interpolation scheme.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from scipy import interpolate # Interpolation routines
# fixed parts of the problem
a,b = 0,25 # interval
xd = np.linspace(a,b,1000) # dense grid for plotting
np.random.seed(21234) # fix random number sequences
nodes = np.sort(np.random.uniform(a,b,15)) # sorted random points
Write a function to plot the true function and the interpolation function, and let it also return the measure of accuracy equal to the average square deviation between the two.
More precisely, let two measures of accuracy be calculated: one calculated off all points within the initial interval, and the second only between the min and max nodes (so, the second one excludes the extrapolated points).
# write your code here
# come up with a test of your own
Compare the accuracy of all three schemes above to interpolate
$$ f(x) = \exp(-x/10)\sin(x/2) $$Hint: use fill_value=”extrapolate” option in interp1d() to allow for extrapolation
Which interpolation scheme is most accurate?
# write your code here
Compare the accuracy of all three schemes above to interpolate
$$ f(x) = x + \exp(x/10)\sin(x) $$Which interpolation scheme is most accurate?
# write your code here
Compare the accuracy of all three schemes above to interpolate
$$ f(x) = \max \big( x + \exp(x/10)\sin(3x/4); x + 5 + \exp([x+5]/10)\sin(3[x+5]/4) \big) $$Which interpolation scheme is most accurate?
# write your code here
Compare the accuracy of all three schemes above to interpolate
$$ f(x) = \exp(-x/10)\sin(x/2) + \mathbb{1}\{\cos(x)>1/2\} $$Which interpolation scheme is most accurate?
# write your code here