1 view (last 30 days)
Erik on 23 Apr 2014
Answered: Niklas Nylén on 23 Apr 2014
Hi i seem to have difficulties to understand how least square method works. I would appreciate if i could get help understanding what is wrong with this code, and suggestion on how to change it. I have tried to change it a lot of times but it never seem to work.
Note: its been a while since I have used matlab and it worked a year ago, som my guess is the language have changed.
function[A Deltapar] = MKA(x,y,deltay,n)
A = [];
Deltapar = [];
X=[];
%------------------------------------------------------------------------------------------------------------------ % checking if the size of the matrix is correct for indata
if (size(x,2)=1)
x=x';
endif
if (size(y,2)=1)
y=y';
endif
if(size(deltay,2)=1)
deltay=deltay';
endif
%------------------------------------------------------------------------------------------------------------------
% Checking if the matrix x and y and delta y have the correct size and after that executing least square method adaptation f
if(size(x)(2)==1 && size(y)(2) ==1 && size(deltay)(2)==1)
%------------------------------------------------------------------------------------------------------------------ % Conducting weighted least squares fit on the matrices x and y have the same format.
% If not, a message is returned back an x and y are not the same length.
if(length(x)==length(y))
for i=0:n
X=[X x.^i];
endfor
sigma=deltay;
vminus1 = diag(1./sigma.^2);
A = inv(X' * vminus1 * X) * (X' * vminus1 * y);
DeltaA = inv(X' * vminus1 * X);
Deltapar = sqrt(diag(DeltaA));
endif
disp('x,y har inte samma langd');
end %------------------------------------------------------------------------------------------------------------------ % Print "you did not write the input data on the correct form"
endif disp('you have not written the input data on the correct form'); end

Niklas Nylén on 23 Apr 2014
The code you provided seems to be in Octave, not MATLAB syntax.