-- cos(x) sampled at 7 points on [0, pi], interpolated by cubic splines x, y = {}, {} for i = 0, 6 do x[i+1] = i*math.pi/6; y[i+1] = math.cos(x[i+1]) end natural = InterpolatingFunction.Spline(x, y) -- f'' = 0 at both ends clamped = InterpolatingFunction.Spline(x, y, 0, 0) -- f'(0) = 0, f'(pi) = 0 -- the largest error between the data points, of the function and of its derivative for _, c in ipairs({{"natural", natural}, {"clamped", clamped}}) do local f, e0, e1 = c[2], 0, 0 for i = 0, 600 do local t = i*math.pi/600 e0 = math.max(e0, math.abs(f(t) - math.cos(t))) e1 = math.max(e1, math.abs(f(t, 1) + math.sin(t))) end print(string.format("%s: max |f - cos| = %.1e, max |f' + sin| = %.1e, f''(0) = %6.3f", c[1], e0, e1, f(0, 2))) end print(string.format("integral from 0 to pi/2: %.6f (exact 1)", InterpolatingFunction.Integrate(clamped, 0, math.pi/2)))