Linear Spline
Overview
linear splines are the simplest spline. They use basic linear interpolation between
the know points.
Data Interpolation
The spline splits the domain at a set of n knot points. Between the knot points, are a set of functions {% S_k %} which
represent the interpolation between points.
{% S_k(x) = a_k + b_k(x-x_k) %}
The functions are subject to the following conditions
{% S_0(x_0) = y_0 %}
{% S_{k-1}(x_k) = S_k(x_k) = y_k %}
{% S_{n-1}(x_n) = y_n %}
That is, the interpolation functions match the function to be approximated at each knot point.
The interpolation is calculate as follows:
{% S_k(x) = y_k + \frac{y_{k+1} - y_k}{x_{k+1} - x_k}(x-x_k) %}
Implementation
The davinci library hosts a spline api that contains the linear spline. The api has a function called value.
Value takes 3 parameters, an array of knot points (x values), the y values associated with each point, and
an x value for which you want value of the spline. For instance, the following calculates what the value of
a spline will be at x=2.5.
let ln = await import('/lib/approximation//spline/v1.0.0/linear.mjs');
let knots = [0,1,2,3,4];
let y = [3,3,4,2,6]
let val = ln.value(knots, y, 2.5);
Try it!
A graph of how the spline would estimate the function with the given knot points and values is shown below.