# Piecewise Polynomial fitting for data

32 views (last 30 days)

Show older comments

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

##### 0 Comments

### Answers (3)

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

Image Analyst
on 13 May 2017

##### 0 Comments

### See Also

### Categories

### Products

### Community Treasure Hunt

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

Start Hunting!