function [P] = polyfit_cnstred(t,y,n)%POLYFIT_CNSTRED OUTPUT: Polynomial coefficients of the polynomial fit that
% is constrainted to pass through the first value of Y. The function LSQLIN
% uses the optimization toolbox.
C = [ ];Aeq = [ ];for i = n:-1:0 C = [ C t.^i ]; Aeq = [ Aeq t(1).^i ];endd = y;% x = polyfit(t,y,n);
A = [ ];b = [ ];beq = [ y(1) ];P = lsqlin(C,d,A,b,Aeq,beq);end
But the result seems to be wrong, or maybe I haven't understood how the lsqlin works. The plot i get is shown in the figure. As explained in the code of the function, the polyfit is constrained to pass through the first value of the data. My guess was that the fitted curve would continue to pass through the data points, trying to minimize the distance between the fitted curve and the data points. The result is obviously different from my guess.
Any suggestions or explenations? I don't have extensive experience in matlab so maybe there's something wrong with the code.
%
Best Answer