## 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 builds a polynomial with provided set of points. It enables the evaluation of the polynomial, effective interpolation of the y values, and updates y through the addition of new x values. The below listing shows the implementation of BarycentricInterpolator method. First, we'll fit model on x and y data, then provide new x data to find out new 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

KroghInterpolator evaluates the polynomial and all of its derivatives. The implementation of KroghInterpolator method is similar to above listing. We'll fit model on x and y data, then provide new x data to find out 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.plot(x, y, '.r', markersize=10)
ax.plot(xnew, y_bary, 'g')
ax.set_title("BarycentricInterpolator")
ax.grid()

ax.plot(x, y, '.r', markersize=10)
ax.plot(xnew, y_krogh, 'b')
ax.set_title("KroghInterpolator")
ax.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.plot(x, y, '.r', markersize=10)
ax.plot(xnew, y_bary, 'g')
ax.set_title("BarycentricInterpolator")
ax.grid()

ax.plot(x, y, '.r', markersize=10)
ax.plot(xnew, y_krogh, 'b')
ax.set_title("KroghInterpolator")
ax.grid()

plt.tight_layout()
plt.show()    ```
`  `

References: