3
$\begingroup$

Two main questions:

  1. I am wondering if it is possible to construct a cubic spline that interpolates data WITHOUT a constant term $a$. That is, the polynomial takes the form $f(t) = bt + ct^2 + dt^3$, where $t$ is time.

  2. Can this spline be used in global time. So rather than the traditional $b(t[i + 1]- t[i]) + c(t[i + 1]- t[i])^2 + d(t[i + 1] - t[i])^3$, we can just use $bt[i] + ct[i]^2 + dt[i]^3$.

I have tried different manipulations of the spline conditions to try and compensate for getting rid of the constant term, but have never been successful. If anyone has any insight into this I would greatly appreciate it!

$\endgroup$
6
  • $\begingroup$ What smoothness do you require from your spline, besides interpolating data? If you have no smoothness constraint at the knot points, surely between any two knots (2 conditions) you can fit a cubic curve (3 degrees of freedom). But if at each knot you require first and second derivatives to match, that's 2 more conditions per knot, so a back-of-envelope calculation would say you have too many conditions (4 per knot, except possibly at ends) and too few degrees of freedom (3 per piece). Are you willing to settle for just first derivative matching, but not second? $\endgroup$ Commented May 6, 2021 at 4:38
  • $\begingroup$ Hi Jukka, Thank you very much for the response and I apologize for such a late reply. I am willing to use just the first derivative condition as you said. The catch, however, is can we make this work in GLOBAL time. That is, instead of always using t[i + 1] - t[i], we always just use the exact time value we're at, i.e. t[i]. I tried this and got an unsolvable matrix for some functions I tried it with. Perhaps I was doing something wrong, but I assumed the matrix was singular due to certain rows becoming multiples of others but I'm not entirely sure. Any thoughts? $\endgroup$
    – user206092
    Commented May 13, 2021 at 18:27
  • $\begingroup$ By global time, do you mean that the $t$ that goes into your cubic pieces is time from origin (instead of time from the beginning of that piece)? Each cubic piece still has its own set of three parameters, i.e. $i$th piece has $b_i, c_i, d_i$? It should be doable if you set up the matrix correctly. $\endgroup$ Commented May 14, 2021 at 4:25
  • $\begingroup$ Correct. I mean time measured from the very start and not the piece. Each cubic would still have the same three parameters, the time value is all that changes. Maybe I'll take a closer look. I wasn't able to get it to work. Thank you!! $\endgroup$
    – user206092
    Commented May 14, 2021 at 18:16
  • $\begingroup$ Umm, the pieces will need to have different values for the parameters. If all pieces have the same parameters $b,c,d$ then you have just one cubic. $\endgroup$ Commented May 14, 2021 at 19:22

1 Answer 1

2
$\begingroup$

It depends on what level of smoothness you require at your knots. The question only says that the spline has to interpolate data (= each piece must match the given values at the two knots that it connects). But when people say cubic spline, they usually mean that at each internal knot, the two pieces also have matching 1st and 2nd derivatives. But more generally you could require less smoothness, so let's consider three cases.

Suppose you have $n+1$ knots, thus $n$ pieces connecting them. (Since the question talks about "time", I am assuming that the final knot does not come back to the initial knot, that is, our spline does not form a closed loop.) You have $n-1$ internal knots (each between two adjacent pieces).

Without constant term, for each $i=1,\ldots,n$, the $i$th piece has 3 free parameters $b_i, c_i, d_i$, for a total of $3n$ parameters.

  1. If no smoothness required: Each of the $n$ pieces must match both knots at its ends, so you have $2n$ conditions. Your $3n$ free parameters are ample and solutions should exist. But your "cubic spline" will look pretty funny because it will in general make corners at the knots.

  2. If you require first derivatives to match at internal knots, that is $n-1$ more conditions. You now have $3n-1$ conditions, still less than your $3n$ free parameters, so it should be possible. Now your "cubic spline" has no corners at the knots (since first derivatives match), but its curvature will in general change abruptly (since second derivatives need not match). This may not be what you mean by "cubic spline".

  3. If you require first and second derivative to match at the $n-1$ internal knots, you have $2n + 2(n-1) = 4n-2$ conditions, which is more than $3n$ (for $n>2$). You have more conditions than free parameters, so in general solutions will not exist. (They might exist in special cases.)

The usual definition has four parameters per piece, so $4n$ parameters total, which leaves 2 extra degrees of freedom even after matching first and second derivatives ($4n-2$ conditions). The usual way to use up these 2 dof is to impose that at the first and last knots, the second derivative vanishes (so called "natural" cubic spline).


Since we can at most require first-order smoothness (matching slopes), let us do that. A straightforward way is to set up $3n$ linear equations: $n$ for hitting left ends for each of the $n$ pieces; $n$ for hitting right ends, and $n-1$ for matching slopes. Oops, we only got $3n-1$ equations because there are only $n-1$ internal knots. For one more equation, let us arbitrarily fix the slope at first knot to some value $q$, for example $q=0$.

At the end of this answer there is very straightforward Matlab code illustrating how to do it. Optimized for simplicity, not efficiency or numerical stability.

But let us first look visually at the interpolation of some small data (six knots). The solid lines show the cubic pieces over each interval.

Cubic spline without constant term

Recall that the cubics do not have a constant term. So each cubic piece would hit the origin if you continued it to $x=0$, as illustrated with the dashed lines. I do not quite understand what real-world situation would call for such a requirement, and it causes the cubics to swing wildly; wilder towards the right end.

So in the end my answer is: Yes, it is possible, but why?

% Example data
x = [2 3 4 5 6 7];
y = sin(3*x);
n = length(x)-1;      % number of intervals
q = 0;                % required slope at first knot

% Intervals are numbered 1 to n.
% For each interval i there are 3 parameters:
% p(i)     = b coefficient of i'th polynomial
% p(i+n)   = c coefficient of i'th polynomial
% p(i+2*n) = d coefficient of i'th polynomial

% Initialize the coefficient matrix and RHS vector.
A = zeros(3*n, 3*n);
rhs = zeros(3*n, 1);

% Require hitting left ends.
for i=1:n
    A(i, i)     = x(i);
    A(i, i+n)   = x(i)^2;
    A(i, i+2*n) = x(i)^3;
    rhs(i)      = y(i);
end

% Require hitting right ends.
for i=1:n
    A(i+n, i)     = x(i+1);
    A(i+n, i+n)   = x(i+1)^2;
    A(i+n, i+2*n) = x(i+1)^3;
    rhs(i+n)      = y(i+1);
end

% Require matching slopes.
for i=1:n-1
    % Left-side slope at i'th internal knot, positive coefficients
    A(i+2*n, i)     = 1;         % b
    A(i+2*n, i+n)   = 2*x(i+1);    % c
    A(i+2*n, i+2*n) = 3*x(i+1)^2;  % d
    % Right-side slope at i'th internal knot, negative coefficients
    A(i+2*n, i+1)     = -1;         % b
    A(i+2*n, i+1+n)   = -2*x(i+1);    % c
    A(i+2*n, i+1+2*n) = -3*x(i+1)^2;  % d
    % Slope difference must be zero
    rhs(i+2*n) = 0;
end

% Required slope at first knot.
A(3*n, 1)     = 1;
A(3*n, 1+n)   = 2*x(1);
A(3*n, 1+2*n) = 3*x(1)^2;
rhs(3*n)      = q;

% Now require A*p = rhs.
p = A\rhs;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Visualization

% Plot knots
clf
plot(x,y,'ko','markerfacecolor','k','markersize',10);
hold on
grid on

% Plot pieces
cols = 'rgbmcy';
for i=1:n
    xx = linspace(x(i), x(i+1), 100);
    yy = p(i)*xx + p(i+n)*xx.^2 + p(i+2*n)*xx.^3;
    plot(xx,yy,[cols(i) '-'],'linewidth',3);
end

% Plot continuations to origin
for i=1:n
    xx = linspace(0,x(i), 300);
    yy = p(i)*xx + p(i+n)*xx.^2 + p(i+2*n)*xx.^3;
    plot(xx,yy,[cols(i) '--']);
end

set(gca,'ylim',[-5 5]);
$\endgroup$
2
  • $\begingroup$ Thank you so much for the feedback! The reason is very specific for wanting to do things this way, but it looks like it will not be sufficient as is based on what you showed me with the oscillations becoming erratic. This is what I was trying to find out and I appreciate all of your help! $\endgroup$
    – user206092
    Commented May 19, 2021 at 18:32
  • $\begingroup$ You could try varying $q$ to get less oscillations, but I don't know how much it helps. $\endgroup$ Commented May 19, 2021 at 18:57

You must log in to answer this question.