extrapolate python

Solutions on MaxInterview for extrapolate python by the best coders in the world

showing results for - "extrapolate python"
Matteo
27 Feb 2016
1import matplotlib.pyplot as plt
2import numpy as np
3from scipy.interpolate import InterpolatedUnivariateSpline
4
5# given values
6xi = np.array([0.2, 0.5, 0.7, 0.9])
7yi = np.array([0.3, -0.1, 0.2, 0.1])
8# positions to inter/extrapolate
9x = np.linspace(0, 1, 50)
10# spline order: 1 linear, 2 quadratic, 3 cubic ... 
11order = 1
12# do inter/extrapolation
13s = InterpolatedUnivariateSpline(xi, yi, k=order)
14y = s(x)
15
16# example showing the interpolation for linear, quadratic and cubic interpolation
17plt.figure()
18plt.plot(xi, yi)
19for order in range(1, 4):
20    s = InterpolatedUnivariateSpline(xi, yi, k=order)
21    y = s(x)
22    plt.plot(x, y)
23plt.show()
24