## swissgirl 3 years ago Is anyone familiar with Cubic splines?

1. swissgirl

I tottally dont follow cubic splines maybe someone can explain the concept a little

2. DanielxAK

I never covered the topic too deeply in any of my courses, but I can offer a bit of explanation: A spline is used to fit a function to data. Your data behaves poorly (for whatever reason), but you'd still like to be able to make a continuous assumption out of it. So, one way is to fit lines to the data points. However, linear splines don't work all that well, because you can't differentiate at the points. Quadratics are similar, except you cannot determine concavity (the second derivative) at your points. So, the ideal polynomial is a cubic, which allows you to retain continuity, differentiability, and curvature. I guess that's kind of a brief explanation. Do you have any specific questions?

3. swissgirl

I understand like the general concept I guess but I am stuck on the process. I went thru alot on google and its quite confusing I cldnt exactly follow

4. DanielxAK

Well, you first need 3 points at the minimum. I'm only going to explain it given three points, because adding more points makes it much more computationally painful: You have 3 points: x1, x2, and x3. You want to fit a cubic from x1 to x2. Similarly, you want to fit a cubic from x2 to x3. So, you'll have something like: S1(x) = a1 + b1x + c1x^2 + d1x^3 and S2(x) = a2 + b2x + c2x^3 + d2x^4 S1 is your cubic from x1 to x2 and S2 is your cubic from x2 to x3. Now, the idea is to find those coefficients. You have 8 unknowns, so you'll have 8 equations at the minimum. Are you following so far?

5. swissgirl

Yes

6. DanielxAK

Okay, now you want to define those 8 equations. The first four come straight from the definition: y1 = S1(x1) = a1 + b1x1 + c1x1^2 + d1x1^3 y2 = S1(x2) = a1 + b1x2 + c1x2^2 + d1x2^3 y2 = S2(x2) = a2 + b2x2 + c2x2^2 + d2x2^3 y3 = S2(x3) = a2 + b2x3 + c2x3^2 + d2x3^3 Okay. So, for your cubic to fit properly, it should work on the endpoints of the cubic, correct? So, your first cubic (S1) should work for x1 and x2, and your second cubic (S2) should work for x2 and x3. Does that make sense?

7. swissgirl

okkkkkkk

8. DanielxAK

That doesn't sound like a very concrete reply. Are you sure you understand? I can draw a picture (hopefully) if that will help.

9. swissgirl

hahah I get it. I just had to read it a few times. Now I get it lol

10. DanielxAK

Okay. Remember, you know (x1,y1); (x2,y2); and (x3,y3). You're solving for the abcd's. So, now, we need 4 more equations to be able to determine our coefficients. So, let's look at the first derivative. If we take the first derivative of S1 at x2, it should equal the first derivative of S2 at x2, right? Similarly, for the second derivative. So, now you have: S1'(x2) = b1 + 2c1x2 + 3d1x2^2 = b2 + 2c2x2 + 3d2x2^2 = S2'(x2) and S1''(x2) = 2c1 + 6d1x2 = 2c2 + 6d2x2 = S2''(x2) So, there's two more equations. Two left to go. I can draw a picture for this one too if you don't quite see it.

11. swissgirl

OKKKKKKK I am getting it soo far btw did u ever come across this before or u just researched this?

12. DanielxAK

I took an undergraduate course in mathematical modeling as one of my math electives. So, it was mostly taught and I had to solve problems (however, I used a computer program to solve them usually). Okay, now for the last two equations, you don't have much left to look at except the endpoints. The last two equations come from how you want your polynomial to behave after (or before) the endpoints. The two common ones (I think there's more) are natural and clamped. Natural allows the endpoints to be free, going in whatever direction the cubic would go if no restrictions were placed on that point. Clamped forces the endpoints to maintain a certain direction. You can research the different methods if you want and when to use them, but I'm going to go with natural (I find it the easiest and I prefer it the most). So, for natural: S1''(x1) = 2c1 + 6d1x1 = 0 and S2''(x3) = 2c2 + 6d2x3 = 0 So, from definition of second derivative, this is neither concave up or concave down at the endpoints, which allows your line to continue to move in a (temporarily) "straight" direction, acting as it naturally would. If that makes sense, I'll generalize it a bit afterwards.

13. swissgirl

No i got it

14. swissgirl

at least i think i do :)

15. DanielxAK

Okay. So, can you tell me how many unknowns you would have if you had 4 points?

16. swissgirl

12

17. DanielxAK

Great. So, how many would you have of the first set of equations? And how many of the second set? And, then, finally the third? (By that, I mean the way I broke it up in my explanations into three pieces.)

18. swissgirl

First equation we will have 6

19. swissgirl

Or in other words S_1,S_2 and S_3

20. DanielxAK

Correct. Unless you have any more questions, I think you have a pretty good understanding of it.

21. swissgirl

Thannkkssss I realllllyyy appreciiiatteeeee youuuurrr heellpppp.

22. swissgirl

I was stuck on it all day and you have thanfully pretty much removed my confusion :)

23. DanielxAK

You're welcome.