As a bit of a thought experiment, I wondered how hard it would be to create a cubic spline interpolation within Alteryx. As with many of my experiments *BaseA* rules apply.

Stealing an idea from Tasha Alfano, I thought I would do it in both Python and Alteryx from first principles. A quick shout out to MathAPI - a handy site and used to render all the LaTeX to SVG.

So let’s start by reviewing how to create a cubic spline and then build it up. I chose to use the algorithm as described in Wikiversity. Specifically with type II simple boundary conditions. I’m not going through the maths but will define the steps to build the spline.

First, step is given an *X* array and a *Y* array of equal length *n* (greater than 2), we want to build a *tridiagonal matrix* which we will then solve to produce the coefficients for the piece-wise spline. The goal of the spline is that it hits every point *(x, y)* and that the first and second derivatives match at these points too.

Sticking with notation in the paper, lets define `H`

to be an `n-1`

length array of the differences in `X`

:

for

A tridiagonal matrix is a square matrix where all values except for the main diagonal and first diagonals below and above this. For example:

```
1 2 0 0
2 3 2 0
0 2 3 2
0 0 2 1
```

One advantage of a tridiagonal matrix is that they are fairly straight forward to invert and solve linear equations based on them. For the sake of coding up the algorithm - let’s define `B`

to be the `n`

length array holding the diagonal elements, `A`

to be the `n-1`

length array of the diagonal above this and `C`

to be the `n-1`

length array of the diagonal below:

```
b0 c0 0 0
a0 b1 c1 0
0 a1 b2 c2
0 0 a2 b3
```

#jdunkerley #tech #python

4.20 GEEK