1

I have this cubic spline function:

s(x) = 4 + k1*x + 2x^2 - (1/6)*x^3  for x in [0,1]
s(x) = 1 - (4/3)*(x-1) + k2*(x-1)^2 - (1/6) * (x-1)^3 for x in [1,2]
s(x) = 1 + k3*(x-2) + (x-2)^2 - (1/6) * (x-2)^3 for x in [2,3]

I would like to implement a simple function, that giving this function, it determines the coefficients k1, k2 and k3, but I couldn't do that..

Has anyone an idea?

ZelelB
  • 1,836
  • 7
  • 45
  • 71
  • Just to be clear: I you might want to note the three `s(x)` functions separately, even though I understand that you need a piecewise function in the end. Besides showing an effort of trying yourself, your question also lacks a problem specification: what specifies the coefficients? – Andras Deak -- Слава Україні Nov 10 '15 at 11:33
  • 1
    What do you want to achieve? Get a continuous function? Get a differentiable function? – Daniel Nov 10 '15 at 11:34
  • @AndrasDeak I just dont know where to begin :-/ I want to determine k1, k2 and k3, so that s(x) fulfills the interpolation in the interval [0,3] as a cubic spline-function – ZelelB Nov 10 '15 at 11:37
  • So look at the definition of a cubic spline, and add it to your question. It has to know a few things: it has to interpolate, so the values of `s(x)` are given in certain points by the input. That can give you equations. But then again, it has to have some analiticity criterion: being continuous, or even differentiable, as @Daniel asked. Without specifying what you want to achieve (even to yourself), you won't be able to succeed. – Andras Deak -- Слава Україні Nov 10 '15 at 11:44
  • get a continuous, 2 times differentiable function. – ZelelB Nov 10 '15 at 11:46
  • 1
    So, take a paper and a pen, look at the piecewise function, and determine the condition for which you get a continuous, twice differentiable function. – Andras Deak -- Слава Україні Nov 10 '15 at 11:54
  • with paper and pen, i solved it already :-) I am wondering about how doing that as a function in matlab – ZelelB Nov 10 '15 at 12:17
  • Well, you need to be able to give an equation to matlab that it can solve... If this was Mathematica, you could probably use a function named `PleaseSolveMyPiecewiseFunctionForCubicSpline[]`, but native matlab can only solve numerical equations, and even with the Symbolic Math Toolbox it would probably be a bit convoluted to set up. I mean, you could try defining a `symfun` for each piecewise function, computing their derivatives symbolically, then solving all the equations coming from the continuity conditions. But you want a "simple" function. – Andras Deak -- Слава Україні Nov 10 '15 at 12:39
  • An approach you could use would be to make a function with input `[k1,k2,k3]` and outputs the conditions you want to match. Then you can use something like [`fzero`](http://uk.mathworks.com/help/matlab/ref/fzero.html) – Steve Nov 10 '15 at 14:07

1 Answers1

0

Denoting the 3 functions as s0(x), s1(x) and s2(x), you can expand s1(x) and s2(x) and collect the coefficients for x^3, x^2, x and constant terms. Then, you get

s1(x)= 4 +k1*x+2*x^2-(1/6)*x^3
s2(x)= (5/2+k2)+(-11/6-2k2)*x+(k2+1/2)*x^2-(1/6)*x^3
s3(x)= ..... (left our for your own practice)

For these 3 functions to be all cubic polynomials, they should be identical. So, you can have

4 = 5/2 + k2
k1 = -11/6 - 2k2
2 = k2+1/2

from which you can get k1=-29/6, k2=3/2.

Compare the equations s0(x) and s2(x), you can also get k3 = 7/6.

fang
  • 3,473
  • 1
  • 13
  • 19