Asked by Brandon
on 24 Feb 2015

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.

Answer by John D'Errico
on 24 Feb 2015

Edited by John D'Errico
on 24 Feb 2015

Accepted Answer

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.

John D'Errico
on 24 Feb 2015

Feel free to ask for more, as I am not certain what you will be doing with those cubic polynomial segments.

Are you trying now to use that spline itself in a regression? For example, if S(x) is the spline, are you trying to find calibration coefficients of some sort, so perhaps estimating the coefficients a and b below to fit some new set of data?

a + b*S(x)

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
on 15 Feb 2018

Sign in to comment.

Answer by Shoaibur Rahman
on 24 Feb 2015

pp = spline(x,y);

NewValue = [1 2]; % may be scaler or vector

out = ppval(pp,NewValue)

Brandon
on 24 Feb 2015

Shoaibur Rahman
on 24 Feb 2015

oshawcole
on 29 Sep 2017

How would you find a polynomial equation from the given x and y points?

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## John D'Errico (view profile)

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/180114-save-cubic-spline-coefficients-to-use-as-response-in-regression#comment_268227

## Brandon (view profile)

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/180114-save-cubic-spline-coefficients-to-use-as-response-in-regression#comment_268229

Sign in to comment.