# I need help with cubic splines containing pre existing conditions

3 views (last 30 days)
Leo Fischer on 19 Nov 2023
Edited: John D'Errico on 20 Nov 2023
My spline passes through (0,0), (1,1),and (2,2), but also S0’(0)=S1’(2)=1, I don’t know how to implement this last condition, my coefs don’t fit this criteria, so I know they’re wrong
Torsten on 19 Nov 2023
Edited: Torsten on 19 Nov 2023
s(x) = x
Show us your linear system that you use to determine the spline coefficients.
Leo Fischer on 20 Nov 2023
So, my Sn = {S0 = a0(x-x0)^3 + b0(x-x0)^2 + c0(x-x0) + d0
D0 = S0(x0) = y0 B0 = s0’’(x0)/2 And then I just go around the clamped properties to find out the other values, was that what you were asking?

John D'Errico on 19 Nov 2023
Edited: John D'Errico on 19 Nov 2023
Easy enough. And ... also somewhat useless, because this is not how you will have been asked to solve the problem. But it might give you an idea of what you missed. And that is what I want you to do, to think about the problem, and what you did.
a = sym('a',[1,4])
b = sym('b',[1,4]);
syms x real
y1(x) = dot(x.^(3:-1:0),a);
y2(x) = dot(x.^(3:-1:0),b);
dy1 = diff(y1,x);
dy2 = diff(y2,x);
ddy1 = diff(dy1);
ddy2 = diff(dy2);
absol = solve(y1(0) == 0,y1(1) == 1,y2(1) == 1, y2(2) == y1(2),dy1(0) == 1,dy2(2) == 1,dy1(1) == dy2(1),ddy1(1) == ddy2(1),[a,b])
absol = struct with fields:
a1: 0 a2: 0 a3: 1 a4: 0 b1: 0 b2: 0 b3: 1 b4: 0
subs(y1,absol)
ans(x) =
x
subs(y2,absol)
ans(x) =
x
The cubic spline is now easily seen to be that which @Torsten suggested. Note that I have used an absolute form for the polynomial segments, not a relative one, as tools like spline will do.
What did you do wrong? Take a careful look at the constraints I posed in the call to solve. Now think about what is missing or incorrect in the code you wrote.
Leo Fischer on 20 Nov 2023
Basically, my spline had to follow those three points I mentioned, but when it came to that derivative part, my c0 = 0,5, when it had to be equal to 1, but then, when I used the same code for the other stuff I had to do, it worked perfectly. I guess it might have been something about the order I was inputting my code, I’ll try to revise some more before updating you
John D'Errico on 20 Nov 2023
Edited: John D'Errico on 20 Nov 2023
All I can suggest is to be more careful about how you implemented the various conditions. Look very carefully at what you wrote, since you show no code at all for us to see. Having written splines code that have been used an uncountable number of times, I can say that it will work, but that you need to do it right.
If I had to guess, it is that you computed a derivative incorrectly, so that it works on some specia lcase, but it fails otherwise.

Matt J on 19 Nov 2023
Edited: Matt J on 19 Nov 2023
e=0:2;
xq=linspace(0,2,20);
yq=spline(e,[1,e,1],xq);
plot(e,e,'o',xq,yq,'.'); legend('Control Points','Interpolated','Location','southeast')

### Categories

Find more on Spline Postprocessing in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}