Find the natural cubic spline which interpolates the data points $(1,0),\; (2,1),\; (3,0), \; (4,1), \; (5,0) $.
I know how to check if a piecewise function is a natural cubic spline, but I don't really know how to find a function that interpolates data points like that.
Here is a cubic-spline interpolation for the $5$ points given in your question:
$ f(x)= \begin{cases} -0.5(x-1)^3 + 1.5(x-1) & \text{$ 1 \leq x \leq 2$}\\ (x-2)^3 - 1.5(x-2)^2 - 0.5(x-2) + 1 & \text{$ 2 \leq x \leq 3$}\\ -(x-3)^3 + 1.5(x-3)^2 + 0.5(x-3) & \text{$ 3 \leq x \leq 4$}\\ 0.5(x-4)^3 - 1.5(x-4)^2 + 1 & \text{$ 4 \leq x \leq 5$}\\ \end{cases} $
Here is a piece of C code for any given number of points $(x_0,y_0),(x_1,y_1),\ldots,(x_N,y_N)$:
Here is a piece of Python code for any given number of points $(x_0,y_0),(x_1,y_1),\ldots,(x_N,y_N)$:
Please note that the two pieces of code above assume $x_0 < x_1 < \ldots < x_N$.