Gradient of a straight line

Prerequisites

Cartesian Coordinates Show

Gradient of a straight line

Introduction

  • A gradient describes how much y changes relative to a change in x.
  • For example, we can define a line as starting at (0, 0) and ending at (10, 25): A graph showing a straight line.
    • Instead of giving explicit start and end points for the line we can describe it by how much y changes relative to x.
    • In the line above, a change of 10 in x produces a change of 25 in y.
    • The gradient is defined as the change in y divided by the change in x.
    • If we use Δ to denote 'change' then the formula for the gradient (g) of a line is:
    The equation for calculating the gradient of a straight line.
    LaTeX formula:g = \frac{\Delta y}{\Delta x}
    • In the line above, the gradient is: Calculating the gradient of the line from (0,0) to (10,25)

Finding the change in x and y

  • In our above example, our first coordinate was (0, 0). This made it easy to calculate how much x and y changed.
  • However when both coordinates are non-zero, it gets a bit more complicated.
  • For example, calculate the gradient of the line between (1, 2) and (7, 10)
    1. To find how much x and y change, we need to subtract the first coordinate from the second
    2. Calculating the gradient of the line from (1,2) to (7,10) step 1
    3. Calculating the gradient of the line from (1,2) to (7,10) step 2
  • We can use this in our equation to find the gradient of any line: The expanded equation for calculating the gradient of a straight line.

Special Cases

  • There are two special cases to look out for when calculating the gradient of a line:
    1. When the line is horizontal
      • There will be no change in y
      • The gradient is 0
    2. When the line is vertical
      • There will be no change in x
      • The gradient is infinity

code (Python)

import math

def findGradient(p1, p2):
    deltaY = p2[1] - p1[1]
    deltaX = p2[0] - p1[0]

    if deltaX == 0:
        return math.inf
    else:
        return deltaY / deltaX

gradient = findGradient([1, 2], [7, 10])
print(gradient) # prints 1.3333333333333333