Skip to main content

Straight lines: slope and intercept

The line y = mx + b: slope as rise over run and why every pair of points on a line gives the same slope, the intercept as the output at x = 0, the line through two given points, and two equations in two unknowns solved by substitution as the point where two lines cross.
M01.3.2L0 Foundation25 minIn review

Before this lesson

M01.3.1 A function is a machine

After this lesson you can

  • Compute the slope of a line from two of its points as rise over run, and show that any two points give the same slope.
  • Read the slope and the intercept off the formula of a line, and draw the line from them.
  • Find the formula of the line through two given points.
  • Solve two linear equations in two unknowns by substitution, and read the answer as the point where two lines cross.
  • Name the slope and the intercept of a model’s line as its weight and its bias.

In review. The maths and code on this page are checked by automated tests on every change, but an independent reader has not signed it off yet. Spot a mistake or a confusing step? Tell us.

The question

The first and last of the course’s data points, \({P = (1, 2)}\) and \({Q = (4, 6)}\), fix exactly one straight line. How do you turn two points into a formula that predicts \({y}\) for any \({x}\)? And the course’s model line, \({\hat{y} = 1.4x + 0.5}\), is a different line: where do the two meet?

From the last lesson

1. If \({f(x) = 2x + 3}\), what is \({f(-2)}\)?

Show answer

\({f(-2) = 2 \times (-2) + 3} = {-1}\).

2. Some vertical line meets a curve twice. What does that tell you?

Show answer

The curve is not the graph of a function of \({x}\): the input on that line would have two outputs.

Intuition

Walk along the line from \({P}\) to \({Q}\). You go 3 across and 4 up: the horizontal and vertical changes of lesson M01.2.1. The steepness of the line is how much it climbs for each step across,

\[ \text{slope} = \frac{\text{rise}}{\text{run}} = \frac{4}{3} . \]

For each step of 1 to the right, the line climbs \({4/3 \approx 1.3333}\). That number is the slope, written \({m}\). The second number of a line is where it crosses the vertical axis, the height at \({x = 0}\). It is the intercept, written \({b}\).

A coordinate plane with the vermillion line through the blue points P (1, 2) and Q (4, 6). A green slope triangle runs 3 to the right from P, labelled run 3, then 4 up to Q, labelled rise 4. A pointer from the line reads slope m equals 4 thirds, and an orange dot where the line crosses the y-axis is labelled intercept b equals 2 thirds. Beside the picture three formulas are written: m equals (6 minus 2) over (4 minus 1) equals 4 thirds; b equals 2 minus 4 thirds equals 2 thirds; and the line y equals 4 thirds x plus 2 thirds.
M01.3.2-F1

The line through \({P}\) and \({Q}\). Its slope is the rise over the run, and its intercept is its height where it crosses the \({y}\)-axis.

Two numbers fix a straight line, and every line that is not vertical can be written as

\[ y = \cparam{m}\,\cdata{x} + \cparam{b} . \]

This is the simplest model in machine learning. A model that predicts \({\hat{y} = wx + b}\) is a straight line whose slope is called the weight \({w}\) and whose intercept is called the bias \({b}\). Training it means choosing those two numbers.

Build it up

Step 1. Slope from two points. Take the rise and the run between any two points, subtracting in the same order on the top and on the bottom:

\[ m = \frac{6 - 2}{4 - 1} = \frac{4}{3} . \]

Step 2. The same slope from any two points. Pick other pairs of points on the same line and draw their triangles. A small triangle with run 0.75 rises 1. The triangle from \({P}\) to \({Q}\) runs 3 and rises 4. A third, with run 1.5, rises 2. The rise over the run is \({4/3}\) every time.

Left: the vermillion line y equals 4 thirds x plus 2 thirds on a coordinate plane, with three green slope triangles of different sizes drawn under it, one after another along the line: run 0.75 and rise 1, run 3 and rise 4, run 1.5 and rise 2. Right: a table titled Three triangles, one slope, with columns run, rise and rise divided by run, whose three rows all end in 4 thirds. Below it: between any two points of y equals m x plus b, the rise is m times the run.
M01.3.2-F2

Three slope triangles of three sizes on one line. Each is a scaled copy of the others, and all three give the same rise over run, \({4/3}\).

That is what “straight” means: a straight line climbs at the same rate everywhere. The formal statement below proves it from the formula.

Step 3. The intercept. From \({P = (1, 2)}\), step back 1 to \({x = 0}\). The line drops by the slope, \({4/3}\), so the height there is

\[ b = 2 - \frac{4}{3} = \frac{2}{3} . \]

Step 4. The line. With \({m = 4/3}\) and \({b = 2/3}\) the line is \({y = \frac{4}{3}x + \frac{2}{3}}\). Check it at \({Q}\): \({\frac{4}{3} \times 4 + \frac{2}{3} = \frac{18}{3} = 6}\). It passes through \({Q}\), as it must.

Step 5. What each number does. Change \({m}\) and the line turns about the point \({(0, b)}\): a larger positive \({m}\) is steeper, \({m = 0}\) is flat, and a negative \({m}\) falls from left to right. Change \({b}\) and the line slides up or down without turning. Two different lines with the same slope never meet: they are parallel.

Two panels of vermillion lines. Left, titled Change m, keep b equals 1: a fan: five lines through the orange point (0, 1), labelled m equals 2, m equals 1, m equals one half, m equals 0 and m equals minus 1, from steepest rising to falling; the line m equals 1 is thick. Right, titled Change b, keep m equals one half: parallel: five parallel lines labelled b equals 2, 1, 0, minus 1 and minus 2, each crossing the y-axis at an orange dot at its own height.
M01.3.2-F3

Left: the slope \({m}\) turns the line about its intercept. Right: the intercept \({b}\) slides the line up and down, and lines with one slope are parallel.

The formal statement

NoteDefinition: slope and intercept

A straight line that is not vertical is the graph of a function

\[ f(x) = mx + b , \]

where \({m}\) and \({b}\) are fixed numbers. \({m}\) is the slope and \({b}\) is the intercept, the output at \({x = 0}\), so the line crosses the \({y}\)-axis at \({(0, b)}\).

Result 1: one slope everywhere. Take any two points \({(x_1, y_1)}\) and \({(x_2, y_2)}\) on the line, with \({x_1 \neq x_2}\). Both lie on it, so \({y_1 = mx_1 + b}\) and \({y_2 = mx_2 + b}\). Subtract:

\[ \begin{aligned} y_2 - y_1 &= (mx_2 + b) - (mx_1 + b) \\ &= mx_2 - mx_1 \\ &= m(x_2 - x_1) . \end{aligned} \]

The \({b}\)s cancel, and \({m}\) is taken out of both terms. The run \({x_2 - x_1}\) is not zero, so divide both sides by it:

\[ \frac{y_2 - y_1}{x_2 - x_1} = m . \]

Whichever two points you choose, rise over run is \({m}\).

Result 2: the line through two points. Given \({(x_1, y_1)}\) and \({(x_2, y_2)}\) with \({x_1 \neq x_2}\), Result 1 says the slope must be

\[ m = \frac{y_2 - y_1}{x_2 - x_1} . \]

The line must pass through the first point, so \({y_1 = mx_1 + b}\). Take \({mx_1}\) from both sides:

\[ b = y_1 - mx_1 . \]

If \({x_1 = x_2}\) the run is 0 and there is no slope: the points lie on the vertical line \({x = x_1}\), which fails the vertical-line test of lesson M01.3.1 and is not a function.

Result 3: where two lines cross. Two equations in two unknowns, \({x}\) and \({y}\), ask for one point that satisfies both: a point on both lines. Substitution finds it. Solve one equation for one unknown, put that expression into the other equation, solve for the remaining unknown, then put the answer back into either equation, and check both. For two lines \({y = m_1x + b_1}\) and \({y = m_2x + b_2}\), the first is already solved for \({y}\), so

\[ \begin{aligned} m_1x + b_1 &= m_2x + b_2 \\ (m_1 - m_2)\,x &= b_2 - b_1 . \end{aligned} \]

If \({m_1 \neq m_2}\), divide by \({m_1 - m_2}\): there is exactly one crossing. If \({m_1 = m_2}\), the left side is 0. Then either \({b_1 \neq b_2}\), the lines are parallel and there is no solution, or \({b_1 = b_2}\), the two equations give the same line and every point of that line is a solution.

Equations need not come solved for \({y}\). Take \({2x + y = 7}\) and \({x - y = -1}\). The second gives \({x = y - 1}\). Put that into the first: \({2(y - 1) + y = 7}\), so \({3y - 2 = 7}\), \({3y = 9}\) and \({y = 3}\). Then \({x = 3 - 1 = 2}\). Check: \({2 \times 2 + 3 = 7}\) and \({2 - 3 = -1}\). The lines cross at \({(2, 3)}\).

Worked example

The line through \({P = (1, 2)}\) and \({Q = (4, 6)}\), and where it meets \({\hat{y} = 1.4x + 0.5}\).

Slope. By Result 2, \({m = \frac{6 - 2}{4 - 1} = \frac{4}{3}}\).

Intercept. \({b = y_1 - mx_1} = {2 - \frac{4}{3} \times 1} = {\frac{2}{3}}\). So the line is \({y = \frac{4}{3}x + \frac{2}{3}}\).

The crossing. Set the two outputs equal, then clear the fractions by multiplying both sides by 30, which thirds and tenths both divide:

\[ \begin{aligned} \tfrac{4}{3}x + \tfrac{2}{3} &= 1.4x + 0.5 \\ 40x + 20 &= 42x + 15 . \end{aligned} \]

Take \({40x}\) from both sides to get \({20 = 2x + 15}\). Take 15 from both sides: \({5 = 2x}\). Halve: \({x = 2.5}\). Put it into the course’s line: \({y = 1.4 \times 2.5 + 0.5} = {3.5 + 0.5} = {4}\). Check it in the other line: \({\frac{4}{3} \times 2.5 + \frac{2}{3}} = {\frac{10}{3} + \frac{2}{3}} = {4}\).

Left, titled Two lines, one crossing: the course's four blue data points with P and Q labelled, a thick sky-blue line labelled y equals 4 thirds x plus 2 thirds through P and Q, and a thinner vermillion line labelled y-hat equals 1.4 x plus 0.5. The two lines lie almost on top of each other and cross at a point ringed in yellow and labelled (2.5, 4). Right, titled Set the outputs equal, then solve: six stacked boxes joined by green arrows. The boxes read 4 thirds x plus 2 thirds equals 1.4 x plus 0.5; 40 x plus 20 equals 42 x plus 15; 20 equals 2 x plus 15; 5 equals 2 x; x equals 2.5; and, highlighted in yellow, y equals 1.4 times 2.5 plus 0.5 equals 4. The arrows are labelled times 30, minus 40 x, minus 15, divided by 2, and into either line.
M01.3.2-F4

The worked example as a trace. Each green arrow does the same thing to both sides. The two lines cross at \({(2.5, 4)}\).

The lines cross at \({(2.5, 4)}\), and that point is no accident. It is the midpoint of \({P}\) and \({Q}\) (lesson M01.2.1), and it is also the point of means of all four data points, whose \({x}\) and \({y}\) have means 2.5 and 4. Module ML01.4 shows that the best line always passes through the point of means.

The two lines are close everywhere on the picture. Their slopes differ by only \({1.4 - \frac{4}{3} = \frac{1}{15}}\) and they meet at \({x = 2.5}\), so the course’s line minus the other is \({(x - 2.5)/15}\): \({-\frac{1}{6}}\) at \({x = 0}\), \({\frac{1}{6}}\) at \({x = 5}\), and 0 at the crossing.

Code

Slope and intercept from two points, written exactly as Result 2 reads. The function takes two points, each a pair (x, y), and gives back the pair (m, b).

def line_through(p, q):
    (x1, y1), (x2, y2) = p, q
    # rise over run
    m = (y2 - y1) / (x2 - x1)
    # from y1 = m x1 + b
    b = y1 - m * x1
    return m, b

m, b = line_through((1, 2), (4, 6))
print(m, b)
1.3333333333333333 0.6666666666666667

The decimals never end, because \({4/3}\) and \({2/3}\) are not finite decimals. Python’s fractions module keeps them exact, and the same function works on fractions unchanged. The crossing of two lines is Result 3 in one line of code.

from fractions import Fraction

P = (Fraction(1), Fraction(2))
Q = (Fraction(4), Fraction(6))
m, b = line_through(P, Q)
print(m, b)

def crossing(m1, b1, m2, b2):
    # m1 x + b1 = m2 x + b2
    x = (b2 - b1) / (m1 - m2)
    return x, m1 * x + b1

# the course's line:
# 7/5 = 1.4 and 1/2 = 0.5
m2, b2 = Fraction(7, 5), Fraction(1, 2)
x, y = crossing(m, b, m2, b2)
print(x, y)
4/3 2/3
5/2 4

numpy.polyfit(x, y, 1) fits a line to points and returns the slope first, then the intercept. Through two points it finds the line through both. Through the course’s four points, which no single line passes through, it finds the best line in a sense that module ML01.4 makes exact: the course’s line.

import numpy as np

# through P and Q only
print(np.polyfit([1, 4], [2, 6], 1))
# through all four points
xs = [1, 2, 3, 4]
ys = [2, 3, 5, 6]
print(np.polyfit(xs, ys, 1))
[1.33333333 0.66666667]
[1.4 0.5]

In the model \({\hat{y} = wx + b}\), polyfit’s first number is the weight and its second is the bias.

Explore

This module’s interactive, the function machine, lives in lesson M01.3.1. Choose the rule \({2x + 3}\) and press Step a few times. Each step moves the input 0.25 to the right, and the output rises 0.5 each time: rise over run is \({0.5 / 0.25 = 2}\) wherever you are on the line, which is the slope in \({2x + 3}\).

Pitfalls

Two panels with the blue points P (1, 2) and Q (4, 6). Left, marked with a cross and titled Run over rise: 3 quarters: a vermillion line from P with slope 3 quarters and a green triangle with run 3 that rises only 2.25, so the line passes below Q; a reddish-purple bar from the line up to Q is labelled misses Q by 1.75. Right, marked with a tick and titled Rise over run: 4 thirds: the line from P with run 3 and rise 4 reaches Q, labelled hits Q.
M01.3.2-F5

Wrong beside right. Run over rise gives a line that misses \({Q}\). Rise over run reaches it.

“Slope is run over rise.”

It is rise over run: the vertical change on top. From \({P}\) to \({Q}\) that is \({4/3}\). The upside-down \({3/4}\) draws a line that climbs only 2.25 over the run of 3 and misses \({Q}\) by 1.75.

“The order of subtraction does not matter.”

It matters that it is the same on top and on bottom. \({\frac{6 - 2}{4 - 1}}\) and \({\frac{2 - 6}{1 - 4}}\) both give \({\frac{4}{3}}\), but the mixed \({\frac{6 - 2}{1 - 4}}\) gives \({-\frac{4}{3}}\), a line that falls instead of rising.

“The intercept is where the line crosses the \({x}\)-axis.”

The intercept \({b}\) is the height where the line crosses the \({y}\)-axis, at \({x = 0}\). The line \({y = \frac{4}{3}x + \frac{2}{3}}\) crosses the \({x}\)-axis where its output is 0, at \({x = -\frac{1}{2}}\). And a vertical line has no slope at all, not a slope of 0: a slope of 0 is a flat line.

“The best line passes through as many data points as it can.”

The line through \({P}\) and \({Q}\) hits two of the four points exactly, yet its mean squared error is \({\frac{1}{18} \approx 0.0556}\). The course’s line hits none of them and scores \({0.05}\), which is better. A model is judged on all the points, not on the few it hits.

Check yourself

Recall

1. What is the slope of the line through \({(1, 2)}\) and \({(3, 8)}\)?

Show answer

\({\frac{8 - 2}{3 - 1} = \frac{6}{2} = 3}\).

2. For the line \({y = -2x + 5}\), give the slope and the intercept. Does it rise or fall?

Show answer

Slope \({-2}\), intercept \({5}\). It falls by 2 for each step of 1 to the right.

3. Why does it not matter which two points of a line you use to find its slope?

Show answer

Between any two points of \({y = mx + b}\) the rise is \({m}\) times the run, so rise over run is always \({m}\).

4. Two lines have the same slope and different intercepts. How many points do they share?

Show answer

None. They are parallel, so the equations have no common solution.

5. In a model \({\hat{y} = wx + b}\), what are the weight and the bias in the language of lines?

Show answer

The weight \({w}\) is the slope and the bias \({b}\) is the intercept.

Exercises

1. ★ Find the line through \({(0, 1)}\) and \({(2, 7)}\), and use it to predict \({y}\) at \({x = 5}\).

Show solution

Slope: \({m = \frac{7 - 1}{2 - 0} = 3}\). The first point has \({x = 0}\), so the intercept is its height, \({b = 1}\). The line is \({y = 3x + 1}\), and at \({x = 5}\) it gives \({3 \times 5 + 1 = 16}\).

2. ★★ Solve by substitution, and say what the answer means for the two lines: \({x + 2y = 7}\) and \({3x - y = 7}\).

Show solution

The second equation gives \({y = 3x - 7}\). Put it into the first:

\[ \begin{aligned} x + 2(3x - 7) &= 7 \\ 7x - 14 &= 7 \\ 7x &= 21 , \end{aligned} \]

so \({x = 3}\) and \({y = 3 \times 3 - 7 = 2}\). Check: \({3 + 2 \times 2 = 7}\) and \({3 \times 3 - 2 = 7}\). The two lines cross at the single point \({(3, 2)}\).

3. ★★★ Show that the line through any two points \({(x_1, y_1)}\) and \({(x_2, y_2)}\), with \({x_1 \neq x_2}\), passes through their midpoint. Check it on \({P}\) and \({Q}\).

Show solution

By Result 2 the line is \({y = mx + b}\) with \({b = y_1 - mx_1}\), where \({m(x_2 - x_1) = y_2 - y_1}\). The midpoint has \({x = \frac{x_1 + x_2}{2}}\). The height of the line there is

\[ \begin{aligned} &m\,\tfrac{x_1 + x_2}{2} + y_1 - mx_1 \\ &\quad= y_1 + \tfrac{m(x_2 - x_1)}{2} \\ &\quad= y_1 + \tfrac{y_2 - y_1}{2} = \tfrac{y_1 + y_2}{2} , \end{aligned} \]

which is the height of the midpoint. For \({P}\) and \({Q}\): at \({x = 2.5}\) the line gives \({\frac{4}{3} \times 2.5 + \frac{2}{3} = 4}\), and the midpoint is \({(2.5, 4)}\).

Summary card

A straight line is fixed by two numbers: how steep it is and where it starts.

The anchor figure again: the line through P (1, 2) and Q (4, 6) with its green slope triangle, run 3 and rise 4, the slope 4 thirds and the intercept 2 thirds marked, and the formula y equals 4 thirds x plus 2 thirds.
M01.3.2-F1

Rise over run, and the height at 0.

  • Line. \({y = mx + b}\): slope \({m}\), intercept \({b}\), the output at \({x = 0}\).
  • Slope from two points. \({m = \frac{y_2 - y_1}{x_2 - x_1}}\), the same for every pair.
  • Intercept from a point. \({b = y_1 - mx_1}\).
  • Crossing. Set the outputs equal and solve; equal slopes mean parallel lines or the same line.
  • In a model. \({\hat{y} = wx + b}\): the weight is the slope, the bias is the intercept.

Sources and next steps

Sources. Each was opened and checked against this lesson on 12 November 2026.

  • Jay Abramson and others, Algebra and Trigonometry 2e, OpenStax. Section 4.1: the slope-intercept form, slope as change in output over change in input (“rise over run”), the intercept at \({(0, b)}\), the undefined slope of a vertical line that is not a function, and parallel lines as equal slopes with different intercepts. Section 11.1: a solution is a pair that satisfies each equation, systems have one, none or infinitely many solutions, and the four steps of substitution.
  • Google, Machine Learning Crash Course: Linear regression: the bias is the same concept as the intercept, and the weight the same concept as the slope \({m}\).
  • NumPy developers, numpy.polyfit, version 2.5: a least-squares polynomial fit whose coefficients come highest power first.

Next. M01.3.3 Parabolas and the quadratic formula moves from lines to the U-shaped graphs of \({x^2}\), and finds the lowest point of the course’s loss.

This lesson unlocks

M01.3.3 Parabolas and the quadratic formula

Your progress is saved in this browser only. No account, no tracking.
Back to top