cubic spline interpolation - mixed boundary conditions possible?
16 vues (au cours des 30 derniers jours)
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
Torsten
le 12 Fév 2024
Modifié(e) : Torsten
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
Bruno Luong
le 12 Fév 2024
Modifié(e) : Bruno Luong
le 12 Fév 2024
In principle yes. The spline on the second segment (b,c) are piecewise cubic poynomials; starting from b sequentially to each intervals [xi,xj], x0=b < x1 < x2 < ... < xn = c; up to c, each polynomial have 4 requirements
for 0 < i+1 = j <= n :
- f(xi) = yi
- f(xj) = yj
- f'(xi) = ydi
- f''(xi) = yddi
So the cubic polynomial is complelely deterined on the first interval then second, etc... On each interval coefficents can be estimated by using linear algebra with "\" or even directly computed (see comment below).
No need for any toolbox.
Now as John have warned, it can become unstable and the spline might strongly oscillate when you get to the last interval.
21 commentaires
Bruno Luong
le 26 Fév 2024
Modifié(e) : Bruno Luong
le 26 Fév 2024
"Makes sense? "
Certainly NOT.
I don't understand what you want to demonstrate with your code snippet. There is no output, no comment and then I don't see the logic. For the starter you at least mix-match the boundary conditions
- not-a-knot on [a,c] and
- chaining not_a_knot (?) on [a,b] then[b,c] with C2 condition(s) at b as described in the orignal post.
Those two are NOT the same. Then your code compare apple with orange. Torsen's asked you over and over why don't you do global interpolation on [a,c] intead of chaining [a,b] & [b,c]. So you knew they are NOT the same.
Plus de réponses (2)
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
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.
Voir également
Catégories
En savoir plus sur Spline Construction dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!