how to obtain a slope

122 views (last 30 days)
shalipse
shalipse on 21 Nov 2016
Commented: Image Analyst on 21 Nov 2016
Hello All,
Could someone please explain to me how to obtain the slope of each individual line in a plot. I have three plots and would like to draw a straight line to pass through each and compute the slopes (see image attached).
Thank you
Regards
Ismaeel

Accepted Answer

Star Strider
Star Strider on 21 Nov 2016
The easiest way would be to use the polyfit function for a 1-order (linear) polynomial:
b = polyfit(x, y, 1);
slope = b(1);
Do the same for each curve in a for loop. (If you want to evaluate the line to plot it, use the polyval function.)
Without knowing how your data are organised (as row or column vectors for example), it is not possible to write specific code for the loop.
  3 Comments
Star Strider
Star Strider on 21 Nov 2016
My pleasure.
You had originally posted three plots on the same axes with oscillations around what appeared to be a line of constant slope (different for each one), each in a different colour so I assume each from a different source.
You cannot do a regression with only one variable. You need to have at least one x (independent) and at least one y (dependent) variable to do a regression. From your description, you have vectors of dependent or y variables. You would need to choose something for your independent x variable in order to do the regression, although it is up to you to define it. (It could just be the index values of your vector, or it could be time, distance, or whatever it is that you measured to get your data.)
The slope is defined as (change in y-variable)/(change in x-variable) with associated units, so the units of both of your variables are important to correctly interpret your data.
Image Analyst
Image Analyst on 21 Nov 2016
Can you restore those 3 plots? They're not posted anymore.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with Curve Fitting Toolbox 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!