plotting a natural cubic spline
11 views (last 30 days)
John D'Errico on 24 Mar 2020
Edited: John D'Errico on 24 Mar 2020
Are you allowed to use existing code? So if you have the curve fitting toolbox, you can just use csape.
x = 0:4;
y = [1 3 3 4 2];
spl = csape(x,y,'variational')
struct with fields:
breaks: [0 1 2 3 4]
coefs: [4×4 double]
As you can see, the second derivative as plotted is zero at the ends, and the second derivative curve is piecewise linear. So csape did as was needed, producing a natural cubic spline interpolant.
Plotting the plsine is pretty easy too.
However, I would not be remotely surprised if your question is to actually formulate the equations and then solve for the spline yourself. After all, this is surely homework. So is that your need? (I'm not at all sure why you would be doing this for any other purpose than homework.)
If your need is to formulate the plsine completely from scratch, then my hope is you are not asking someone to do that part for you. It is easy enough to do anyway. Start by reading here:
Robert U on 24 Mar 2020
Hi Justin Howard,
I guess there is a mistake concerning the second derivative at the start and end points of the requested spline interpolation. Usually just the slope is given which would be denoted S'.
You can use the function spline() to provide a cubic spline interpolation of the given points including the slopes at start and end points.
x = [0,1,2,3,4];
y = [0,1,3,3,4,2,0];
x_spline = 0:0.1:4;
y_spline = spline(x,y,x_spline);
fh = figure;
ah = axes(fh);