Reputation: 259
I read the article on https://www.value-at-risk.net/cubic-spline-interpolation/
I understand all but I don't know how I can get the values for the matrix:
I know that there is something like hi = hi+1 - hi
I visited several websites, read different explenations, but I never found out how exactly I come to this values in the matrix.
Upvotes: 3
Views: 8284
Reputation: 51903
The matrix is just system of equations encoded as matrix so it can be easily computed by inverse matrix.
For example second line of matrix (8,4,2,1,0,0,0,0)
after matrix multiplication means this:
a3.2^3+a2.1^2+a1.2^1+a0=5
which is your p1(2)=5
the lines are:
p1(1)=1
p1(2)=5
p2(2)=5
p2(3)=4
p1'(2)-p2'(2)=0
p1''(2)-p2''(2)=0
p1''(1)=0
p2''(3)=0
so for example last matrix line ( 0,0,0,0,18,2,0,0 )
is this:
b3.18 + b2.2 = 0
If we derive p2(t)
up to 2-nd derivation
p2(t) = b3.t^3 + b2.t^2 + b1.t + b0
p2'(t) = 3.b3.t^2 + 2.b2.t + b1
p2''(t) = 2.3.b3.t + 1.2.b2 = 6.b3.t + 2.b2
Now for t=3
we get:
p2''(3) = 6.b3.3 + 2.b2 = 18.b3 + 2.b2
And encoded into matrix (last line)
(0,0,0,0,18,2,0,0) * ( a3,a2,a1,a0,b3,b2,b1,b0) = 0
Which matches your example. Hope it is clear now ...
Beware this example of yours is just for y
axis as you got 2D curve you need to do this for x
axis in the same manner again ...
Now let rewrite the matrix equation again:
M*A=B
Where M
is your 8x8
matrix, A=(a3,a2,a1,a0,b3,b2,b1,b0)
and B=(1,5,5,4,0,0,0,0)
you can solve this like this:
inverse(M)*M*A = inverse(M)*B
A = inverse(M)*B
So you obtain the A
which holds your p1,p2
polynomials coefficients for B
which holds your position (y
coordinates in one pass and x coordinates in the next) so you got p1x,p1y,p2x,p2y
polynomials. Which is what you need for the interpolation.
However this approach is a bit backward and usually predefined polynomial forms are used like SPLINE,BEZIER with defined properties like continuity, linearity, etc (no need for inverse matrix operation). But if you need custom properties like in this example then you do not have much of a choice.
For more info see How can i produce multi point linear interpolation?
Upvotes: 4