Can I return the last two values of this function ONLY? Or must I return them all? I know I can return the first easily enough, but what about the second, and third, etc???
Show older comments
function [m,b,Sm,Sb,CoVar,r]=ErrorCalc_SLF(X,Y)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This function takes in two measurements. It then calculates
%the slope and intercept of the best fitting line and returns
%these values, with slope=m and intercept=b, as well as the
%the error in m and b.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n=numel(X);
if n~=numel(Y)
Err='The data arrays do not have the same number of elements, please correct';
disp(Err);
else
Sx=sum(X);
Sy=sum(Y);
XY=linspace(0,0,n);
XX=linspace(0,0,n);
YY=linspace(0,0,n);
YBMX=linspace(0,0,n);
DiffX=linspace(0,0,n);
DiffY=linspace(0,0,n);
ProdDXDY=linspace(0,0,n);
D2X=linspace(0,0,n);
D2Y=linspace(0,0,n);
for i=1:n
XY(i)=X(i)*Y(i);
XX(i)=X(i)*X(i);
YY(i)=Y(i)*Y(i);
end
Sxx=sum(XX);
Sxy=sum(XY);
Syy=sum(YY);
Del=n*Sxx-Sx^2;
m=(n*Sxy-Sx*Sy)/Del;
b=(Sxx*Sy-Sx*Sxy)/Del;
for i=1:n
YBMX(i)=Y(i)-b-m*X(i);
end
Sybmx=sum(YBMX);
SigY=sqrt((1/(n-2))*Sybmx^2);
Sm=SigY*sqrt(n/Del);
Sb=SigY*sqrt(Sxx/Del);
%THIS IS THE NEW CODE----
Xbar=sum(X)/n;
Ybar=sum(Y)/n;
for i=1:n
DiffX(i)=X(i)-Xbar;
DiffY(i)=Y(i)-Ybar;
ProdDXDY(i)=DiffX(i)*DiffY(i);
D2X(i)=DiffX(i)^2;
D2Y(i)=DiffY(i)^2;
end
CoVar=(1/n)*sum(ProdDXDY);
r=(sum(ProdDXDY))/(sqrt(sum(D2X)*sum(D2Y)));
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!