Least Square Method adaptation

조회 수: 2 (최근 30일)
Erik
Erik 2014년 4월 23일
답변: Niklas Nylén 2014년 4월 23일
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.
thanks in advance.
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
Niklas Nylén 2014년 4월 23일
The code you provided seems to be in Octave, not MATLAB syntax.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by