Save cubic spline coefficients to use as response in regression

24 次查看(过去 30 天)
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 个评论
Brandon
Brandon 2015-2-24
编辑:Brandon 2015-2-24
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
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,

请先登录,再进行评论。

采纳的回答

John D'Errico
John D'Errico 2015-2-24
编辑:John D'Errico 2015-2-24
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 个评论
Anthony Ortega
Anthony Ortega 2017-3-18
ive been trying to get the coefficients from my cubic spline command thank you so much for this!
tzina kokkala
tzina kokkala 2018-2-15
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?

请先登录,再进行评论。

更多回答(1 个)

Shoaibur Rahman
Shoaibur Rahman 2015-2-24
pp = spline(x,y);
NewValue = [1 2]; % may be scaler or vector
out = ppval(pp,NewValue)
  3 个评论
Shoaibur Rahman
Shoaibur Rahman 2015-2-24
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 2017-9-29
How would you find a polynomial equation from the given x and y points?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Spline Construction 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by