Matlab error message regarding use of polyfit function in script file

2.1k views Asked by At

I am trying to run a script file which draws a line on a plotted graph. Sometimes the script file works but occasionally it does not work and the line drawn on the graph is not where I want it. When the file does not work I get the following error message:

Warning: Polynomial is not unique. Degree >= number of data points.
> In polyfit at 70
  In FIXING_force_and_rate_of_rise at 48 

Does anyone know what this error message means? What does it mean when a polynomial is not unique?

If it helps, when I click on the error message I get taken to this explanation:

Solve least squares problem.

[Q,R] = qr(V,0);
ws = warning('off','all'); 
p = R\(Q'*y);    
% Same as p = V\y;
warning(ws);
if size(R,2) > size(R,1)
   warning(message('MATLAB:polyfit:PolyNotUnique'))
elseif warnIfLargeConditionNumber(R)
    if nargout > 2
    warning(message('MATLAB:polyfit:RepeatedPoints'));
    else
        warning(message('MATLAB:polyfit:RepeatedPointsOrRescale'));
    end

A section of the code I am trying to run is pasted below. Line 48 is where I think the error is: [SlpBL2]=polyfit(Time_scale(300:T2), Force_trace(300:T2),1);

Reject the 1st 300 data points

BL1=min(Force_trace(300:end));
col='rcgy'
figure; set(gcf, 'windowstyle', 'docked', 'color', 'w')
for j=1:100
    cla
    Force_trace=Force_trace-BL1;
    Peak1=max(Force_trace(300:end-30));
    Extrapolation_point=Peak1*0.10;
    D=Force_trace<Extrapolation_point;
    T2=find(D,1,'last');
    [SlpBL2]=polyfit(Time_scale(300:T2), Force_trace(300:T2),1);
    Slope=SlpBL2(1);
    BL2=SlpBL2(2);

    plot(Time_scale, Force_trace)
    hold on
    plot([0, Time_scale(T2)], [BL2, Time_scale(T2)*Slope+BL2],col(1))

    disp(['round number ' int2str(j) ' baseline is ' num2str(BL2)])
   if abs(BL2)<1e-10
      break
   end
   BL1=BL2;
   drawnow 
end

Thanks in advance for your help!

1

There are 1 answers

2
TroyHaskin On

Code discussion

Since you are fitting a one-degree polynomial to data, passing less than two data points is the only reason that error would be thrown. This will happen if T2 is always 300 or empty (meaning that D is all false.


Explanation

In order to exactly fit a polynomial of degree n, n+1 data points are required to uniquely determine the n+1 coefficients of the polynomial. For example, a cubic has the form:

cubic polynomial.

A cubic equation requires four points of data to uniquely determine the four coefficients.

With less than n+1 data points, an exact polynomial can not be created since, assuming there is an underlying function from which the data was drawn, there are an infinite number of solutions. However, a polynomial whose coefficients form the smallest/shortest solution can readily be calculated; this is the least-squares solution. This brief overview (section 2) gives a succinct overview of this and overdetermined solutions, which is more common in my experience.

Consider this input:

>> x = linspace(0,1,50);
>> y = x.^3 + x.^2;
>> p3 = polyfit(x([1,25,50]),y([1,25,50]),3);
Warning: Polynomial is not unique; degree >= number of data points. 
> In polyfit at 70 
>> p4 = polyfit(x([1,10,25,50]),y([1,10,25,50]),3);

and the plot here:

Graphs of a cubic, an undetermined fit, and an exact fit

The undetermined solution follows the true function closely but not exactly.