TExcelTAD
Home › Algebra › Slope of a Line

Slope of a Line

The slope of a line measures how steep it is: how much yy changes for each step of 1 in xx. It is the single number that separates one direction of line from another, and outside geometry it has a more useful name: a rate of change, such as kilometres per hour, megabytes per second or euros per item.

Rise over run

Pick any two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) on a line. The slope mm is the vertical change divided by the horizontal change:

m=riserun=y2−y1x2−x1=ΔyΔxm = \frac{\text{rise}}{\text{run}} = \frac{y_2 - y_1}{x_2 - x_1} = \frac{\Delta y}{\Delta x}

(Δ\Delta, the Greek letter delta, means "change in".) For the points (1,2)(1, 2) and (4,11)(4, 11):

m=11−24−1=93=3m = \frac{11 - 2}{4 - 1} = \frac{9}{3} = 3

Every step of 1 to the right raises the line by 3. Swapping the points gives 2−111−4=−9−3=3\frac{2 - 11}{1 - 4} = \frac{-9}{-3} = 3 again; what matters is that you subtract in the same order on top and bottom. On a straight line, any two points give the same slope, which is precisely what makes it straight.

The four kinds of slope

Slope Line goes Example Points
positive up to the right m=2m = 2 (0,0)(0, 0), (1,2)(1, 2)
negative down to the right m=−1m = -1 (0,3)(0, 3), (3,0)(3, 0)
zero horizontal m=0m = 0 (−1,4)(-1, 4), (5,4)(5, 4)
undefined vertical division by 0 (2,−1)(2, -1), (2,6)(2, 6)

A larger ∣m∣\lvert m \rvert means a steeper line. A slope of 0 means "no rise"; an undefined slope means "no run", because the formula would divide by x2−x1=0x_2 - x_1 = 0.

python · try it yourself
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-3, 3, 20)
plt.figure(figsize=(6, 4))
for m, style in [(2, "-"), (0.5, "--"), (0, ":"), (-1, "-.")]:
    plt.plot(x, m * x + 1, style, label=f"slope {m}")
# a rise/run triangle on the slope-2 line, from (0, 1) to (1, 3)
plt.plot([0, 1, 1], [1, 1, 3], color="gray")
plt.text(0.35, 0.55, "run 1"); plt.text(1.1, 1.9, "rise 2")
plt.axhline(0, color="black", linewidth=0.8); plt.axvline(0, color="black", linewidth=0.8)
plt.ylim(-4, 6); plt.grid(True, alpha=0.3); plt.legend()
plt.show()
print("four lines through (0, 1) with slopes 2, 0.5, 0, -1")
four lines through (0, 1) with slopes 2, 0.5, 0, -1

Slope as a rate

When the axes carry units, the slope carries units too: (units of yy) per (unit of xx).

xx-axis yy-axis Slope means
time in hours distance in km speed in km/h
time in seconds downloaded MB download rate in MB/s
number of items total cost in € price per item
horizontal metres height in metres road grade (8%=0.088\% = 0.08)

A download shows 30 MB after 2 s and 105 MB after 7 s. The rate is 105−307−2=755=15 MB/s\frac{105 - 30}{7 - 2} = \frac{75}{5} = 15\ \text{MB/s}. If the rate stays constant, the graph of downloaded MB against time is a straight line with slope 15.

The slope is also tied to the angle θ\theta the line makes with the xx-axis: m=tan⁡θm = \tan\theta. A slope of 1 is 45°, a slope of 0.08 is about 4.6°. Game engines use this to decide whether a character can walk up a ramp: a "max walkable slope" of 45° means the ground's rise-over-run may be at most 1 (see Slopes, Steps and Ground Snapping).

In software: spotting a constant rate

A server reports its memory use every 10 minutes. Is it growing at a constant rate (a straight line), and if so, when will it hit the 1024 MB limit?

python · try it yourself
times  = [0, 10, 20, 30, 40]           # minutes
memory = [512, 542, 572, 602, 632]     # MB

rates = [(memory[i + 1] - memory[i]) / (times[i + 1] - times[i]) for i in range(len(times) - 1)]
print("slope of each interval:", rates)

if len(set(rates)) == 1:               # every interval has the same slope: a line
    m = rates[0]
    t_limit = (1024 - memory[0]) / m
    print(f"constant leak of {m} MB/min, limit reached after {t_limit:.1f} min")
slope of each interval: [3.0, 3.0, 3.0, 3.0]
constant leak of 3.0 MB/min, limit reached after 170.7 min

Equal slopes between consecutive points mean the data lie on one line. Real measurements are noisy, so in practice you fit the best line through them instead (Linear Models and Trend Lines). In machine learning, the slope of the loss curve tells gradient descent which way to step; that is the same idea in calculus clothing (Gradient Descent).

Tip: Slopes of parallel lines are equal; slopes of perpendicular lines multiply to −1 (2 and −12-\frac{1}{2}, for instance). Equations of Lines puts both facts to work.

Exercises

Exercise 1: Two points

Find the slope of the line through (−2,5)(-2, 5) and (4,−7)(4, -7).

Answer

m=−2m = -2.

Solution
m=−7−54−(−2)=−126=−2m = \frac{-7 - 5}{4 - (-2)} = \frac{-12}{6} = -2

The line falls 2 units for every unit to the right.

Exercise 2: Which kind?

Classify the slope of the line through each pair: (a) (3,1)(3, 1) and (3,8)(3, 8), (b) (−4,2)(-4, 2) and (6,2)(6, 2), (c) (0,0)(0, 0) and (−2,3)(-2, 3).

Answer

(a) undefined (vertical), (b) zero (horizontal), (c) negative, m=−32m = -\frac{3}{2}.

Solution
(a)run=3−3=0→division by zero, vertical line(b)rise=2−2=0→m=010=0(c)m=3−0−2−0=−32\begin{aligned} &\text{(a)}\quad \text{run} = 3 - 3 = 0 && \to \text{division by zero, vertical line} \\ &\text{(b)}\quad \text{rise} = 2 - 2 = 0 && \to m = \tfrac{0}{10} = 0 \\ &\text{(c)}\quad m = \frac{3 - 0}{-2 - 0} = -\frac{3}{2} \end{aligned}

Exercise 3: A rate

A delivery van's odometer reads 120 km at 9:00 and 330 km at 12:00. What is its average speed, and what does the odometer read at 13:30 if the speed stays the same?

Answer

70 km/h; 435 km at 13:30.

Solution
m=(330−120) km(12−9) h=2103=70 km/hfrom 12:00 to 13:30 is 1.5 h:330+70⋅1.5=435 km\begin{aligned} &m = \frac{(330 - 120)\ \text{km}}{(12 - 9)\ \text{h}} = \frac{210}{3} = 70\ \text{km/h} \\ &\text{from 12:00 to 13:30 is } 1.5\ \text{h}\text{:}\quad 330 + 70 \cdot 1.5 = 435\ \text{km} \end{aligned}

Exercise 4: Collinear points in code

Write a function collinear(p, q, r) that returns True when three points lie on one line, without dividing (so vertical lines cause no error). Test it on (1,1)(1, 1), (3,5)(3, 5), (6,11)(6, 11) and on (1,1)(1, 1), (3,5)(3, 5), (5,10)(5, 10).

Answer

True for the first triple, False for the second.

Solution

Equal slopes y2−y1x2−x1=y3−y1x3−x1\frac{y_2 - y_1}{x_2 - x_1} = \frac{y_3 - y_1}{x_3 - x_1} can be cross-multiplied, which avoids dividing by zero:

python · try it yourself
def collinear(p, q, r):
    return (q[1] - p[1]) * (r[0] - p[0]) == (r[1] - p[1]) * (q[0] - p[0])

print(collinear((1, 1), (3, 5), (6, 11)))
print(collinear((1, 1), (3, 5), (5, 10)))
print(collinear((2, 0), (2, 4), (2, 9)))     # vertical line works too
True
False
True

Slopes: 42=2\frac{4}{2} = 2 and 105=2\frac{10}{5} = 2 for the first triple; 42=2\frac{4}{2} = 2 but 94=2.25\frac{9}{4} = 2.25 for the second.

Exercise 5: Road grade

A road rises 12 m over 150 m of horizontal distance. What is its slope as a decimal and as a percentage grade, and what angle does it make with the horizontal?

Answer

m=0.08m = 0.08, an 8 % grade, about 4.57°.

Solution
m=12150=0.08=8%θ=arctan⁡(0.08)\begin{aligned} m &= \frac{12}{150} = 0.08 = 8\% \\ \theta &= \arctan(0.08) \end{aligned}
python · try it yourself
import math
m = 12 / 150
print(m, f"{m:.0%}", round(math.degrees(math.atan(m)), 2))
0.08 8% 4.57

Test yourself

What is the slope of the line through (2,3)(2, 3) and (6,1)(6, 1)?

  1. 2
  2. −2
  3. −12-\frac{1}{2}
  4. 12\frac{1}{2}

A graph shows cost in euros (yy) against kilograms (xx). Its slope is 4.5. What does that mean?

  1. The fixed fee is €4.50
  2. Each extra kilogram costs €4.50
  3. 4.5 kg cost €1
  4. The total cost is €4.50

Why is the slope of a vertical line undefined?

  1. Its rise is zero
  2. Its run is zero, so rise/run divides by zero
  3. Vertical lines are not straight
  4. Its slope is infinitely negative only