how to calculate the slope of a line?
24 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Star Strider
on 18 Aug 2015
Edited: Star Strider
on 18 Aug 2015
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
0 Comments
More Answers (1)
John D'Errico
on 18 Aug 2015
Edited: John D'Errico
on 18 Aug 2015
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
See Also
Categories
Find more on Stress and Strain 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!