Save cubic spline coefficients to use as response in regression

24 views (last 30 days)
Hi community,
I have two vectors and I would like to fit a cubic spline to:
y=[18.93000031 19.42000008 19.51000023 19.67000008 19.68000031 19.71999931];
x=[58.61111111 67.32055556 70.56194444 74.22694444 78.39388889 85.11555556];
I would also like to save the parameters of the fit at every point to use in regression analysis as my response (I want to change other variables at the points where I take a measurement in an experimental design.) How can I do this. I've not a very advanced matlab user but I hope this is an easier question for a pro in the community to answer.
  3 Comments
Brandon
Brandon on 24 Feb 2015
Edited: Brandon on 24 Feb 2015
So between each x y pair as I understand there is a different cubic spline model or function that is used to generate the curve. This model has like 4 parameters I believe. I would like to save these parameters.
Ernest Modise - Kgamane
Ernest Modise - Kgamane on 2 Mar 2021
Hi D'Errico,
Kindly assist,
Using your code example:
x=[58.61111111 67.32055556 70.56194444 74.22694444 78.39388889 85.11555556];
y=[18.93000031 19.42000008 19.51000023 19.67000008 19.68000031 19.71999931];
S = spline(x,y)
S =
form: 'pp'
breaks: [58.611 67.321 70.562 74.227 78.394 85.116]
coefs: [5x4 double]
pieces: 5
order: 4
dim: 1
I get the following error
>> splineeg
Error: File: splineeg.m Line: 4 Column: 5
Invalid expression. Check for missing or extra characters.
What could be the mistake,

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 24 Feb 2015
Edited: John D'Errico on 24 Feb 2015
Ok. Why did you not say that? :)
x=[58.61111111 67.32055556 70.56194444 74.22694444 78.39388889 85.11555556];
y=[18.93000031 19.42000008 19.51000023 19.67000008 19.68000031 19.71999931];
S = spline(x,y)
S =
form: 'pp'
breaks: [58.611 67.321 70.562 74.227 78.394 85.116]
coefs: [5x4 double]
pieces: 5
order: 4
dim: 1
S.coefs
ans =
0.00053154 -0.013366 0.13235 18.93
0.00053154 0.00052218 0.020489 19.42
-0.0013274 0.005691 0.040628 19.51
0.00061302 -0.0089032 0.028855 19.67
0.00061302 -0.00124 -0.013411 19.68
Each row of S.coefs is the set of coefficients of one cubic polynomial segment. It can be evaluated by polyval, but be CAREFUL here. Those cubic polynomials are defined to be evaluated relative to the break point at the beginning of that interval.
So if I wanted to evaluate the first cubic segment at the point xhat = 59, I would do this:
xhat = 59;
polyval(S.coefs(1,:),xhat - S.breaks(1))
ans =
18.979
ppval(S,59)
ans =
18.979
As you can see, it yields the same prediction as ppval did. The reason for this offset is it makes the polynomial evaluation more stable with respect to numerical problems.
As part of my SLM toolbox (found on the File Exchange), I do provide a tool that allows you to extract the polynomials in a symbolic form.
polys = slmpar(S,'symabs')
polys =
[1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double]
[1x1 sym ] [1x1 sym ] [1x1 sym ] [1x1 sym ] [1x1 sym ]
So in symbolic form I removed the break point offset that was built into the polynomial, then return it as a symbolic "function" of x.
polys{2,1}
ans =
0.00053154425392522449377030735462313*x^3 - 0.1068293870770472518768171460159*x^2 + 7.1771491067641370730246290387668*x - 141.76724752093537873401498012911
And if I now substitute 59 into that polynomial, you also get the value you should expect.
subs(polys{2,1},59)
ans =
18.979480689855906097289602061928
I'm still not positive exactly what you intend as a goal, so if you need more help, please add further clarification. For example, my own SLM toolbox gives you a tool that does regression spline modeling.
  3 Comments
Anthony Ortega
Anthony Ortega on 18 Mar 2017
ive been trying to get the coefficients from my cubic spline command thank you so much for this!
tzina kokkala
tzina kokkala on 15 Feb 2018
May I ask, how can one plot the cubic spline S(x) from the beginning until the end point of x, when all that is available is x ,y and S.coefs?

Sign in to comment.

More Answers (1)

Shoaibur Rahman
Shoaibur Rahman on 24 Feb 2015
pp = spline(x,y);
NewValue = [1 2]; % may be scaler or vector
out = ppval(pp,NewValue)
  3 Comments
Shoaibur Rahman
Shoaibur Rahman on 24 Feb 2015
Perhaps, you have already got it! coefficients are in pp. NewValue is any value used for interpolation using the coefficients in pp. Thanks.
oshawcole
oshawcole on 29 Sep 2017
How would you find a polynomial equation from the given x and y points?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!