HOME Mathematics Computer Science Economics
Gradient of a straight line
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):
    • 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:

      LaTeX: g = \frac{\Delta y}{\Delta x}
    • In the line above, the gradient is:
Finding the change in x and y
  • In our above example, our first coordinate was (9, 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
  • We can use this in our equation to find the gradient of a 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
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 0.3333333333333333
Related