-- sin(x) sampled at 11 points on [0, pi], interpolated by B-splines of degree 1 to 7 x, y = {}, {} for i = 0, 10 do x[i+1] = i*math.pi/10; y[i+1] = math.sin(x[i+1]) end for _, n in ipairs({1, 2, 3, 5, 7}) do local f, e0, e2 = InterpolatingFunction.BSpline(x, y, n), 0, 0 for i = 0, 1000 do local t = i*math.pi/1000 e0 = math.max(e0, math.abs(f(t) - math.sin(t))) if n >= 2 then e2 = math.max(e2, math.abs(f(t, 2) + math.sin(t))) end end print(string.format("degree %d: %2d knots, max |f - sin| = %.1e, max |f'' + sin| = %s", n, #f.knots, e0, n >= 2 and string.format("%.1e", e2) or "-")) end -- a B-spline of degree 3 through 6 points g = InterpolatingFunction.BSpline({0, 1, 2, 3, 4, 5}, {0, 1, 4, 9, 16, 25}, 3) print("x = " .. table.concat(g.x, ", ")) print("y = " .. table.concat(g.y, ", ")) print("knots = " .. table.concat(g.knots, ", ")) c = g.coefficients for i = 1, #c do c[i] = string.format("%.4f", c[i]) end print("coefficients = " .. table.concat(c, ", ")) print("g(2.5) = " .. g(2.5) .. ", type " .. g.type .. ", degree " .. g.degree)