Let's say I have

```
import numpy as np
from scipy.interpolate import UnivariateSpline

# "true" data; I don't know this function
x = np.linspace(0, 100, 1000)
d = np.sin(x * 0.5) + 2 + np.cos(x * 0.1)

# sample data; that's what I actually measured
x_sample = x[::20]
d_sample = d[::20]

# fit spline
s = UnivariateSpline(x_sample, d_sample, k=3, s=0.005)

plt.plot(x, d)
plt.plot(x_sample, d_sample, 'o')
plt.plot(x, s(x))
plt.show()

```

I get

[![enter image description here](https://i.stack.imgur.com/aNFjf.png)](https://i.stack.imgur.com/aNFjf.png)

What I would now like to have are functions between all the orange dots, so something like

```
knots = s.get_knots()
f0 = <some expression> for knots[0] <= x < knots[1]
f1 = <some expression> for knots[1] <= x < knots[2]
...

```

Thereby, `fi` should be chosen in a way that it reproduces the shape of the spline fit.

I found the post [here](https://stackoverflow.com/questions/22488637/getting-spline-equation-from-univariatespline-object/25330648#25330648), but the spline produced there seems incorrect for the example above and it's also not exactly what I need as it does not return expressions.

How could I turn the spline into a piecewise function? Is there a (straightforward) way to express each interval e.g. as a polynomial?