MATLAB Answers

# How can I determine the y-intercept from known x and y values?

14 views (last 30 days)
Benjamin Horsley on 18 Apr 2021
Commented: Benjamin Horsley on 19 Apr 2021
I have a series of known x and y values (both 161x1 double) and I would like to plot a line graph and determine the slope and y-intercept of the data. I know there have been a few similar posts, but I'm just a little confused as to the correct method to perform this. I would like to determine the y-intercept as part of a residual analysis for biomechanical data.
Thank you.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Robert Brown on 18 Apr 2021
Edited: Robert Brown on 18 Apr 2021
define a vector, x
x = 1:10
define a slope, m
define an intercept, b
NOTE: I did this for the example, to generate the data, you of course would skip this step
m = 2
b = 17
compute y = m*x + b
NOTE: I did this for the example, to generate the data, you of course would skip this step
y = m * x + b
make a plot of the data, plotting variable x along the x axis, and variable y along the y axis
figure
plot(x,y)
xlabel('x values')
ylabel('y values')
title('plot of x,y values')
solve for slope by the following equation, when expression is order = 1 (no x-squared or x-cubed terms...etc)
slope = (y(end) - y(1)) / (x(end) - x(1))
solve for intercept by computing y - m* x = b
intercept is computed for each of my 10 points (or your 161 points), and is always 17in this example... (or whatever your intercept turns out to be)... as it should be
intercept = y - slope*x
I hope this helps !
##### 1 CommentShowHide None
Benjamin Horsley on 19 Apr 2021
Thank you, @Robert Brown.

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!