Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to develop some method of iterating through all the polynomial orders that you would like to consider for a fit. Throughout this iteration you then calculate the error between the model P and the data y. Mean squared error is a common measure of similarity that I would suggest.</p> <p>Currently you have no way of changing the model order and it is in fact VERY high (127) so your final result is unstable.</p> <p>In this modified code I've generated my own noisy meanValues which would be best fit using a second order fit. Yet the order value is set to 4 so you will find that the third and fourth order coefficients in v are very small compared to the 0th, 1st and 2nd coefficients.</p> <p>At least for my generated data you should be able to verify that the MSE between y and P for the 2nd order fit is lower than a 4th order fit. There doesn't seem to be much of a trend in your data so you would do best to test a few different orders and take the one with the lowest MSE. That's not to say it correctly models the system that produced your data, so be careful.</p> <pre><code>clear all; meanValues = (1:127)/25; meanValues(:) = meanValues(:).^2; for i = 1:length(meanValues) meanValues(i) = meanValues(i) + rand(1,1)*4; end x=(1:length(meanValues)); y=meanValues(:); Order = 4; A(:,1) = ones(127,1); for j = 1:Order A(:,j+1) = (x'.^j); end % A=fliplr(vander(x)); v=A \ y; P(1: length(x))=0; for i=1: length(x) for j=1: length(v) P(i)=P(i)+v(j)*x(i).^(j-1); end end plot(x,y,'r*'); hold on; plot(x, P); </code></pre> <p>EDIT: This version computes MSE and finds the minimum order. Only takes 0.324198 seconds to check up to 100th order fit. Maybe there is some advantage to using polyfit... I'm not sure.</p> <pre><code>clear all; meanValues = (1:127)/25; meanValues(:) = meanValues(:).^2; for i = 1:length(meanValues) meanValues(i) = meanValues(i) + rand(1,1)*4; end x=(1:length(meanValues)); y=meanValues(:); tic minMSE = Inf; nOrder = 100; for Order = 1:nOrder A(:,1) = ones(127,1); for j = 1:Order A(:,j+1) = (x'.^j); end % A=fliplr(vander(x)); v=A \ y; P = zeros(1,length(x)); for i=1: length(x) for j=1: length(v) P(i)=P(i)+v(j)*x(i).^(j-1); end end P = P'; newMSE = norm(P-y); if (newMSE &lt; minMSE) minMSE = newMSE; minOrder = Order; minP = P'; end end toc plot(x,y,'r*'); hold on; plot(x, minP); minMSE minOrder </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload