Note
Go to the end to download the full example code
1-Dimensional InterpolationΒΆ
7 import ndsplines
8 import numpy as np
9 import matplotlib.pyplot as plt
10 from scipy import interpolate
11 from scipy.stats import norm
12 import itertools
13
14
15 def gaussian(x_in):
16 z = norm.ppf(.9995)
17 x = z*(2*x_in-1)
18 return norm.pdf(x)
19
20 def sin(x_in):
21 x = np.pi*(x_in-0.5)
22 return np.sin(x)
23
24 def tanh(x_in):
25 x = 2*np.pi*(x_in-0.5)
26 return np.tanh(x)
27
28 funcs = [gaussian, sin, tanh]
29
30 x = np.linspace(0, 1, 9)
31 xx = np.linspace(-.25, 1.25, 1024)
32 k = 3
33
34 for degree in range(0,4):
35 for func in funcs:
36 fvals = func(x)
37 truef = func(xx)
38 plt.figure()
39
40 plot_sel = slice(None)
41
42 plt.gca().set_prop_cycle(None)
43 test_Bspline = interpolate.make_interp_spline(x, fvals, k=degree)
44 splinef = test_Bspline(xx.copy(), extrapolate=True)
45 plt.plot(xx, splinef, '--', lw=3.0, label='scipy.interpolate.make_interp_spline')
46
47 test_NDBspline = ndsplines.make_interp_spline(x, fvals, degrees=degree)
48 NDsplinef = test_NDBspline(xx.copy())
49 plt.plot(xx, NDsplinef, label='ndspline.make_interp_spline')
50
51 plt.plot(xx, truef, 'k--', label="True " + func.__name__)
52 plt.plot(x, fvals, 'ko')
53 plt.title('k=%d'%degree)
54
55 plt.legend(loc='best')
56 plt.show()
Total running time of the script: (0 minutes 1.969 seconds)