cubic spline interpolation - mixed boundary conditions possible?
Afficher commentaires plus anciens
We need two boundary conditions. Is it possible to specify the first and second derivative at the same boundary point?
In csape and others, I have not seen such mixed boundary conditions. I also think it is not possible as these two equations lead to conflicting entries in the matrix and right-hand-side.
I want to "connect" two interpolation domains ( f1 on x1 between [a,b] and f2 on x2 between [b, c]) together by first creating f1 and using f1'(b) and f2''(b) as boundary conditions for the construction of f2. But I am not sure if this makes sense.
7 commentaires
SA-W
le 12 Fév 2024
Torsten
le 12 Fév 2024
If there is a jump in the function value at b, it does not make sense to connect the two pieces by conditions on derivative and second derivative. Use two separate splines on [a,b] and [b,c].
SA-W
le 12 Fév 2024
But if the derivatives of f1 and f2 are different at b, why do you want to feed f1'(b) into f2 as boundary condition ? Use two separate splines on [a, b] and [b, c]. They will automatically incorporate the different slopes of f1 and f2 at b.
SA-W
le 12 Fév 2024
All information you give is contradictory - so it doesn't make sense to continue replying.
Either you want to have f1'(b) = f2'(b) to make your Newton-method work. In this case, use one spline over the complete interval [a c].
If you want to incorporate that the derivatives are different, use two usual splines - one over [a, b] and one over [b, c]. They will automatically reflect a jump of f' at b.
Réponse acceptée
Plus de réponses (2)
Matt J
le 12 Fév 2024
0 votes
This FEX download will let you impose conditions like that,
8 commentaires
SA-W
le 12 Fév 2024
Matt J
le 12 Fév 2024
Only you know if it makes sense for you. But the SLM engine will definitely let you do it. Once you do the spline fit on [a,b], you will know its derivatives at a,b. You can then impose the derivatives at b as constraints when you fit [b,c].
Bruno Luong
le 12 Fév 2024
But OP asks for interpolation not fitting
Matt J
le 23 Fév 2024
Fitting and interpolation become equivalent when you put the control points at the data points.
Bruno Luong
le 24 Fév 2024
Modifié(e) : Bruno Luong
le 24 Fév 2024
Not if you provide extra BC (meaning more than two independent).
Matt J
le 24 Fév 2024
I'm not sure why not. You probably have to add more degrees of freedom to the model to force the spline through all the points and satisfy boundary conditions, but SLM claims to be able to do that.
Bruno Luong
le 24 Fév 2024
Of course just be specific, what DOF you add and how you add with SLM
Matt J
le 24 Fév 2024
It appears you use slmset() with the 'xy','xyp' and 'xypp' settings to force the curve to have certain values and derivatives at prescribed points.
John D'Errico
le 12 Fév 2024
Modifié(e) : John D'Errico
le 12 Fév 2024
You generally don't want to do this. That is, fit one spline, then construct a second spline to match the first. The result will not be the optimal one, because the second curve shape is now entirely dependent on the first. The most extreme case of this would be a two segment spline. So you have some data in the first segment. Fit a cubic polynomial through the data for the first segment, ignoring everything to the right of it.. Then try to fit the second segment, while forcing the curve to be C2 across the boundary. That would result in complete crap. We can do it easily enough. For example...
x = linspace(0,2,100)';
y = sin(2*x);
ind1 = x<=1; ind2 = x>=1;
Seg1 = fit(x(ind1),y(ind1),'poly3') % polyfit would also be efficient here
% we want the curve to be C2 across the break at x==1
Bc0 = Seg1(1);
[Bc1,Bc2] = differentiate(Seg1,1);
Now, in this silly example of what not to do, we choose to fit a cubic segment to the second half of our data. I'll use lsqlin to do that, to enforce the boundary constraints match.
beq = [Bc0;Bc1;Bc2];
Aeq = [1 1 1 1;3 2 1 0;6 2 0 0];
C = x(ind2).^[3 2 1 0];
D = y(ind2);
coef2 = lsqlin(C,D,[],[],Aeq,beq)
plot(x,y,'k.',x(ind1),Seg1(x(ind1)),'r-',x(ind2),polyval(coef2,x(ind2)),'b-')
As you can see, the red ucrve fits nicely to the first part of the data, but then a fit to the second part, where the function is constrained to match so it is C2 with the first segment is terrible.
Instead, you want to perform a fit where both sections are fit at the same time, with a constraint that the function is C2 at the join point. A simple least squares spline with the same knots, will fit very nicely.

SIGH. But, CAN you do that? Well, yes, using my SLM toolbox. That part is easy enough. I don't know that you can do it using the MATLAB included tools though. Even so, I would suggest it is better to do the entire modeling part together, rather than in two halves. If not, then you can see crapola result, as I showed above.
3 commentaires
SA-W
le 12 Fév 2024
John D'Errico
le 12 Fév 2024
Modifié(e) : John D'Errico
le 12 Fév 2024
NO. You apparently have multiple misconceptions about splines. A cubic spline interpolant will not enforce both constant first AND second derivatives at the end points. That is simply wrong.
Yes, one approach are the natural end conditions, which says the 2nd derivative at the ends is set to zero. This can be derived from a calculus of variations solution for the shape of a flexible beam, thus a mathematical model for a thin flexible beam. A zero endpoint second derivative makes sense there.
However, a more common (and arguably better most of the time) solution is to use what is known as the not-a-knot end condtions. Effectively, the idea is to enforce third derivative continuity at the 2nd and next to last break. Done that way, there is no derivative constraint applied at either boundary.
So what you are saying does not make sense. It merely says you don't fully understand splines.
SA-W
le 12 Fév 2024
Catégories
En savoir plus sur Spline Postprocessing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


