필터 지우기
필터 지우기

how to calculate the slope of a line?

조회 수: 11 (최근 30일)
Amr Hashem
Amr Hashem 2015년 8월 18일
댓글: Amr Hashem 2015년 8월 19일
I have a data A,B and I want to calculate the slope between two existing points
I want to find the slop between 2012 & 2013 points I try:
A=[ 234228; 249150 ; 265021 ;281904 ;299862 ];
B=[ 7380; 8045 ; 9903 ;15058 ;13610 ];
plot(A,B);
a = [2010:2014]'; b = num2str(a); c = cellstr(b);
dx = 1; dy = 1; % displacement so the text does not overlay the data points
text(A+dx, B+dy , c)
hold on
coefficients = polyfit(A, B, 1);
slope = coefficients(1);
text(266000,11500,num2str(slope,'y = %.3fx'))
but it seems to be wrong :( ANY IDEA

채택된 답변

Star Strider
Star Strider 2015년 8월 18일
편집: Star Strider 2015년 8월 18일
The easiest way is to use the diff funciton. This puts the 2012-2013 slope as the third one in the series:
SlopeBetweenPoints = diff(B)./diff(A)
Slope_2012_2013 = SlopeBetweenPoints(3)
SlopeBetweenPoints =
44.5651e-003
117.0689e-003
305.3367e-003
-80.6326e-003
Slope_2012_2013 =
305.3367e-003
EDIT — To print a table:
fprintf(1, '\nSlopes:\n')
fprintf(1, '\t%4d-%4d\t% .4f\n', [a(1:end-1), a(2:end), SlopeBetweenPoints]')
Slopes:
2010-2011 0.0446
2011-2012 0.1171
2012-2013 0.3053
2013-2014 -0.0806

추가 답변 (1개)

John D'Errico
John D'Errico 2015년 8월 18일
편집: John D'Errico 2015년 8월 18일
The slope between consecutive points is simple.
slopes = diff(B)./diff(A);
Pick whatever slope you want from the result. So, here:
A=[ 234228; 249150 ; 265021 ;281904 ;299862 ];
B=[ 7380; 8045 ; 9903 ;15058 ;13610 ];
slopes = diff(B)./diff(A)
slopes =
0.044565
0.11707
0.30534
-0.080633
slopes(3)
ans =
0.30534

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by