To Calculate the slope

조회 수: 2 (최근 30일)
Hana
Hana 2016년 9월 15일
댓글: Star Strider 2016년 9월 16일
B= [ones(length(x1),1) x1(:)]\y1(:); % Estimate Regression Parameters
yf = [ones(length(x1),1) x1(:)]*B; % Calculate Regression Line
slope= B(2);
intercept = B(1);
plot(x1,y1,'.b');
plot (x1,yfaa,'r')
text(-20,-25,['y = ', num2str(round(Baa(2)*100)/100), 'x'],'fontsize',20);
  댓글 수: 2
Hana
Hana 2016년 9월 15일
Sorry. I pressed enter without asking my question. I want to know for calculating regression line I should allocate an arrays of ones or zeros? I don't know which one is correct. and also 'y = ', num2str(round(Baa(2)*100)/100), is correct or I don't need to multiply and divide it by 100? Thanks
Adam
Adam 2016년 9월 15일
What is the question? You have just pasted some code in!

댓글을 달려면 로그인하십시오.

답변 (1개)

Star Strider
Star Strider 2016년 9월 15일
Using ones is correct. I create a design matrix and use it both for estimating the parameters and calculating the regression line afterwards. This involves some minor changes to your code:
DsnMtx = [ones(size(x(:)) x1(:)];
B = DsnMtx\y1(:); % Estimate Regression Parameters
yf = DsnMtx*B; % Calculate Regression Line
That is slightly more efficient, since you only need to calculate the design matrix once.
  댓글 수: 2
Hana
Hana 2016년 9월 16일
Thanks for the answer. I get a syntax error on DsnMtx = [ones(size(x(:)) x1(:)];
and is this line correct? text(0,17,['y = ', num2str(round(B(2)*100)/100),'x'],'fontsize',20);
Thanks
Star Strider
Star Strider 2016년 9월 16일
My pleasure.
The syntax error is due to a typographical error in my code. I forgot the ‘1’. It should be:
DsnMtx = [ones(size(x1(:)) x1(:)];
I would use the sprintf function instead:
text(0,17,sprintf('y = %.2f', x),'fontsize',20);
You might want to experiment with the 'VerticalAlignment' and 'HorizontalAlignment' name-value pairs as well.

댓글을 달려면 로그인하십시오.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by