Clear Filters
Clear Filters

Piecewise Polynomial fitting for data

41 views (last 30 days)
zafar khan
zafar khan on 12 May 2017
Commented: dpb on 16 May 2017
I need to use curve fitting on a time series data, as the data is large (a week), fitting a single polynomial curve will not represent the true data. Therefore, solution i can come up with (i don't know if there exists such a solution) I have to fit the data for first 24 hours (a reading at every half hour so 48 data points during a day) and keep the loop running for all 7 days (the data is in a single file). I have tried to code it but am getting error and secondly i cannot understand how to save results for all loops as i will need to reconstruct the fitted curve so i need the data and i will need the RMSE for each curve. Also is there a way to determine which polynomial fits the data best ie.e. minimum RMSE without applying all of polynomial fittings programatically.
The code i could think of is given below, I will appreciate if some one can help me with it.
i=7
j=48
for i=1:7
for j=1:48:48
[Fit5, gof5] = fit( x([1:j]), y([1:j]), 'poly5' );
coeff5=coeffvalues(Fit5);
end
end

Answers (3)

Star Strider
Star Strider on 12 May 2017
Consider the Signal Processing Toolbox sgolayfilt (link) function. It may do exactly what you want.
  1 Comment
dpb
dpb on 12 May 2017
Edited: dpb on 12 May 2017
Given OP's followup, +1
And, if doesn't have SP TB but does have Curve Fitting, then there's the smooth function which has that as one of many optional forms.

Sign in to comment.


dpb
dpb on 12 May 2017
Fitting a polynomial is probably not going to work all that well and certainly a fifth-order one is likely quite unstable.
W/o data to see what it is the curve looks like, hard to give any real conclusive answer, but if the purpose is interpolation, consider piecewise splines instead.
That aside, your code above has a problem with j=1:48:48 as a loop count expression; it is just j=1
Altho it is not my recommendation to do this (see above note), a more concise way to operate over your set of equi-spaced data would be to reshape the vectors to 2D array and operate by column...
x=reshape(x,48,[]).'; % arrange x, y by day (48 observations/day)
y=reshape(y,48,[]).'; % as column arrays
nc=size(x,2) % number columns --> days
nDeg=5; % the poly degree (your value, see above notes)
b=zeros(nDeg+1,nc)) % allocate room for the coefficients
for i=1:nc
b(:,i)=polyfit(x(:,i),y(:,i),nDeg); % and do the fitting
end
Undoubtedly the above will give some numerical issues warnings; (see notes above) but if you're adamant about trying, use the optional output variables as documented for polyfit to at least standardize the design matrix before solving. Then you'll need to add to the saved results the output structure returned to use for the evaluation similarly as to the coefficients array above.
Did I say I don't recommend this, yet? :) Look at splines and give us a sample (smallish) dataset...
  13 Comments
zafar khan
zafar khan on 15 May 2017
The data is volatile so you will see some seasonal patterns but not necessaryily. Fitting a distribution is not a very good idea in my case. I will still prefer to smooth the data. Will leave the idea of prediction for now and smoothing moving average produces good results which is sufficient for load modelling.
dpb
dpb on 16 May 2017
Well, whatever, it is your problem. I've removed the annoying info.

Sign in to comment.


Image Analyst
Image Analyst on 13 May 2017
See my canned Savitzky-Golay filter demos, attached. Don't use a filter order more than about 2 or 3 or you won't see much smoothing. A 5th order polynomial will hug the curve fairly closely and not provide much smoothing.

Categories

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!