## Pages

### Univariate Interpolation Examples in Python (part-2)

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;

1. Preparing test data
2. BarycentricInterpolator  method
3. KroghInterpolator method
4. Source code listing

` `
```from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np ```
` `

Preparing test data

We'll create a simple x and y data for this tutorial. We can visualize it on a graph and apply linear interpolation. Linear interpolation in a graph is simply connecting data points linearly.

` `
```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 method

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() ```

KroghInterpolator method

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()```
`    `

In this tutorial, we've briefly learned how to interpolate data by using SciPy's BarycentricInterpolator and KroghInterpolator classes in Python. The full source code is listed below.

Source code listing

` `
```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()    ```
`  `

References: