% Example: Using the cubic spline in octave.

xdat = -5:1:5;
ydat = sin(xdat);

spline_coefficients = spline(xdat,ydat);

hold off
figure(1)
plot(xdat,ydat,'k+')
hold on

% Now, I want to use the spline to estimate the values on a finer grid.
x = -6:0.01:6;
y = ppval(spline_coefficients, x);
plot(x,y,'b-')

% What does the actual sine function look like?
y_true = sin(x);
plot(x, y_true, 'g--')

% Pretty up the plot and save it
legend('data','cubic spline','sin(x)','box','off','location','bestoutside')
xlabel('x')
ylabel('y(x)')
title('Spline Demonstration')
print('first_example.pdf', '-dpdf')

hold off
