フィルターのクリア

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
  1 件のコメント
Amr Hashem
Amr Hashem 2015 年 8 月 19 日
Thanks

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeQuaternion Math についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by