Difficulty in fitting two lines with polynomials

조회 수: 1 (최근 30일)
Deepesh upadrashta
Deepesh upadrashta 2014년 12월 1일
편집: dpb 2014년 12월 3일
Hi, I want to fit data which resembles two piecewise linear functions using polynomials.
Y=30000X for X<=0.001;
Y=27+3000X for 0.001<X<0.003.
When I use polyfit or cftool, the fitted curve has ups and downs (change of sign in curvature) which I don't want. I'm getting smooth curve when I use polynomials of the order 20 which is really high.
I would prefer a fitted curve without change in sign of curvature even at the expense of some accuracy (with lower orders of polynomials).
How can I do it?
Thanks, Deepesh.
  댓글 수: 1
Image Analyst
Image Analyst 2014년 12월 3일
Attach your data (so we can try things with it) and attach a screenshot so we can see what you're seeing.

댓글을 달려면 로그인하십시오.

답변 (2개)

dpb
dpb 2014년 12월 3일
편집: dpb 2014년 12월 3일
Piecewise linear regression is fairly easy to code anyway...the algebra to add the condition to match the two at the breakpoint is
y = a1 + b1 x, x<=c,
y = a2 + b2 x, x>c.
Match breakpoint, or a1 + b1 c = a2 + b2 c. Rearrange this to isolate (say) a2 as
a2 = a1 + b1 c - b2 c --> a1 + c(b1-b2) = aprime
Now have
y = a1 + b1 x, x<=c,
y = aprime + b2 x, x>c.
This is easy-peasy to code in Matlab and use as target for nlinfit --
function y=piecewise(coef,x)
% return piecewise linear fit given a1,b1,c,b2 as coefficient array and vector x
% c is breakpoint, a1 is intercept of first section, b1,b2 are two segment slopes
a1=coef(1); b1=coef(2); % reduce parentheses clutter....
c=coef(3); b2=coef(4);
ix=x>c; % location > breakpoint
y(ix)=[a1+c*(b1-b2)+b2*x(ix)];
y(~ix)=[a1+b1*x(~ix)];
y=y(:);
Use this as
coeff=nlinfit(X,Y,@piecewise,coeff0);
Example: for Hayden dataset
>> coeff=nlinfit(X,Y,@piecewise,[0 .01 1.5 0.1])
coeff =
-0.0077 0.0155 1.6804 0.1067
>>

dpb
dpb 2014년 12월 1일
  댓글 수: 2
Deepesh upadrashta
Deepesh upadrashta 2014년 12월 3일
HI,
My problem is approximating two piecewise lines with polynomial.
After reading SLM help, I don't think it can do that. It is able to fit the data with polynomials upto 3 degree between knots. Thanks.
dpb
dpb 2014년 12월 3일
A cubic spline is a polynomial, just a particular form of one... :)
It would appear that the other link is a linear piecewise polynomial w/ specified knot location.
Do you want/need continuous first/second derivatives or simply only two slopes?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by