Cubic Spline

Overview


This example demonstrates fitting a cubic spline to a series of points. The function will be designed to fit the following:

  • {% f(0) = 3 %}
  • {% f(1) = 3 %}
  • {% f(2) = 7 %}
  • {% f(3) = 2 %}
  • {% f(4) = 6 %}

Implementation


The following code uses the cubic spline library to fit the given points, using the natural boundary conditions.


let cb = await import('/lib/approximation/spline/v1.0.0/cubic.mjs');

let knots = [0,1,2,3,4];
let y = [3,3,7,2,6]
let gen = cb.constructNatural(knots, y);
let points = $from(0,4,100).map(p=>({
    x:p,
    y:gen(p)
}));

$val.set('data', points);
					
Try it!

Results


The following graphs the resulting function from the above calculation.

copy

Contents