In this tutorial, you'll briefly learn how to use SciPy API's BarycentricInterpolator and KroghInterpolator classes in Python.
Interpolation is a method of estimating unknown data points in a known range to make the curve smoother. Univariate interpolation is a type of curve fitting that seeks the curve that best fits a set of two-dimensional data points. Since the data points are sampled from a single variable function, it is called univariate interpolation.
The tutorial covers;
- Preparing test data
- BarycentricInterpolator method
- KroghInterpolator method
- Source code listing
We'll start by loading the required libraries.
from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np
y = [-1, -2, 1, 3, 2, -1, -3, -1, 1, 2]
x = np.linspace(1, len(y), len(y))
plt.figure(figsize=(8, 6))
plt.plot(x, y, '.r', markersize=10)
plt.plot(x, y)
plt.grid()
plt.show()
BarycentricInterpolator constructs a polynomial based on a given set of points, allowing for polynomial evaluation, efficient interpolation of y values, and updating y values with the addition of new x values. The following code demonstrates the usage of the BarycentricInterpolator method. Initially, the model is fitted with x and y data, and then new x data is provided to obtain corresponding estimated y values.
xnew = np.linspace(min(x), max(x), num=100) bary = interpolate.BarycentricInterpolator(x, y) yfit = bary(xnew) plt.plot(x, y, '.') plt.plot(xnew, yfit) plt.grid() plt.show()
The KroghInterpolator evaluates both the polynomial and its derivatives. Its implementation is similar to the previous listing. The model is fitted with x and y data, and new x data is provided to obtain new estimated y values.
krogh = interpolate.KroghInterpolator(x, y) yfit = krogh(xnew) plt.plot(x, y, '.') plt.plot(xnew, yfit) plt.grid() plt.show()
Now we can visualize both methods on a graph and compare each method performance
with a given data. The following listing shows how to plot all in one
graph.
xnew = np.linspace(min(x), max(x), num=100)
# fit each method on x, y data
bary = interpolate.BarycentricInterpolator(x,y)
krogh = interpolate.KroghInterpolator(x,y)
y_bary = bary(xnew)
y_krogh = krogh(xnew)
# visualize on a graph
ig, ax = plt.subplots(2,1, figsize=(12, 8))
ax[0].plot(x, y, '.r', markersize=10)
ax[0].plot(xnew, y_bary, 'g')
ax[0].set_title("BarycentricInterpolator")
ax[0].grid()
ax[1].plot(x, y, '.r', markersize=10)
ax[1].plot(xnew, y_krogh, 'b')
ax[1].set_title("KroghInterpolator")
ax[1].grid()
plt.tight_layout()
plt.show()
from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np
# Preparing data
y = [-1, -2, 1, 3, 2, -1, -3, -1, 1, 2] x = np.linspace(1, len(y), len(y))
plt.figure(figsize=(8, 6))
plt.plot(x, y, '.r', markersize=10)
plt.plot(x, y)
plt.grid() plt.show()
xnew = np.linspace(min(x), max(x), num=100)
# fit each method on x, y data bary = interpolate.BarycentricInterpolator(x,y) krogh = interpolate.KroghInterpolator(x,y) y_bary = bary(xnew) y_krogh = krogh(xnew)
# visualize on a graph
ig, ax = plt.subplots(2,1, figsize=(12, 8)) ax[0].plot(x, y, '.r', markersize=10) ax[0].plot(xnew, y_bary, 'g') ax[0].set_title("BarycentricInterpolator") ax[0].grid() ax[1].plot(x, y, '.r', markersize=10) ax[1].plot(xnew, y_krogh, 'b') ax[1].set_title("KroghInterpolator") ax[1].grid() plt.tight_layout() plt.show()
No comments:
Post a Comment