Author: Mehreen Saeed

The slope of a line, and its relationship to the tangent line of a curve is a fundamental concept in calculus. It is important for a general understanding of function derivatives.

In this tutorial, you will discover what is the slope of a line and what is a tangent to a curve.

After completing this tutorial, you will know:

- The slope of a line
- The average rate of change of f(x) on an interval w.r.t. x
- The slope of a curve
- The tangent line to a curve at a point

Let’s get started.

## Tutorial Overview

This tutorial is divided into two parts; they are:

- The slope of a line and a curve
- The tangent line to a curve

## The Slope of a Line

Let’s start by reviewing the slope of a line. In calculus the slope of a line defines its steepness as a number. This number is calculated by dividing the change in the vertical direction to the change in the horizontal direction when moving from one point on the line to another. The figure shows how the slope can be calculated from two distinct points A and B on a line.

A straight line can be uniquely defined by two points on the line. The slope of a line is the same everywhere on the line; hence, any line can also be uniquely defined by the slope and one point on the line. From the known point we can move to any other point on the line according to the ratio defined by the slope of the line.

## The Average Rate of Change of a Curve

We can extend the idea of the slope of a line to the slope of a curve. Consider the left graph of the figure below. If we want to measure the ‘steepness’ of this curve, it is going to vary at different points on the curve. The average rate of change when moving from point A to point B is negative as the value of the function is decreasing when x is increasing. It is the same when moving from point B to point A. Hence, we can define it over the interval [x0,x1] as:

(y1-y0)/(x1-x0)

We can see that the above is also an expression for the slope of the secant line that includes the points A and B. To refresh your memory, a secant line intersects the curve at two points.

Similarly, the average rate of change between point C and point D is positive and it’s given by the slope of the secant line that includes these two points.

## Defining the Slope of the Curve

Let’s now look at the right graph of the above figure. What happens when we move point B towards point A? Let’s call the new point B’. When the point B’ is infinitesimally close to A, the secant line would turn into a line that touches the curve only once. Here the x coordinate of B’ is (x0+h), with h an infinitesimally small value. The corresponding value of the y-coordinate of the point B’ is the value of this function at (x0+h), i.e., f(x0+h).

The average rate of change over the interval [x0,x0+h] represents the rate of change over a very small interval of length h, where h approaches zero. This is called the slope of the curve at the point x0. Hence, at any point A(x0,f(x0)), the slope of the curve is defined as:

The expression of the slope of the curve at a point A is equivalent to the derivative of f(x) at the point x0. Hence, we can use the derivative to find the slope of the curve. You can review the concept of derivatives in this tutorial.

### Examples of Slope of the Curve

Here are a few examples of the slope of the curve.

- The slope of f(x) = 1/x at any point k (k≠0) is given by (-1/k^2). As an example:
- Slope of f(x) = 1/x at (x=2) is -1/4
- Slope of f(x) = 1/x at (x=-1) is -1

- The slope of f(x) = x^2 at any point k is given by (2k). For example:
- Slope of f(x) = x^2 at (x=0) is 0
- Slope of f(x) = x^2 at (x=1) is 2

- The slope of f(x) = 2x+1, is a constant value equal to 2. We can see that f(x) defines a straight line.
- The slope of f(x) = k, (where k is a constant) is zero as the function does not change anywhere. Hence its average rate of change at any point is zero.

## The Tangent Line

It was mentioned earlier that any straight line can be uniquely defined by its slope and a point that passes through it. We also just defined the slope of a curve at a point A. Using these two facts, we’ll define the tangent to a curve f(x) at a point A(x0,f(x0)) as a line that satisfies two of the following:

- The line passes through A
- The slope of the line is equal to the slope of the curve at the point A

Using the above two facts, we can easily determine the equation of the tangent line at a point (x0,f(x0)). A few examples are shown next.

## Examples of Tangent Lines

### 1. f(x) = 1/x

The graph of f(x) along with the tangent line at x=1 and x=-1 are shown in the figure. Below are the steps to determine the tangent line at x=1.

- Equation of a line with slope m and y-intercept c is given by: y=mx+c
- Slope of the line at any point is given by the function f'(x) = -1/x^2
- Slope of the tangent line to the curve at x=1 is -1, we get y=-x+c
- The tangent line passes through the point (1,1) and hence substituting in the above equation we get:
- 1 = -(1)+c ⟹ c = 2

- The final equation of the tangent line is y = -x+2

### 2. f(x) = x^2

Shown below is the curve and the tangent lines at the points x=2, x=-2, x=0. At x=0, the tangent line is parallel to the x-axis as the slope of f(x) at x=0 is zero.

This is how we compute the equation of the tangent line at x=2:

- Equation of a line with slope m and y-intercept c is given by: y=mx+c
- Slope of the line at any point is given by the function f'(x) = 2x
- Slope of the tangent line to the curve at x=2 is 4, we get y=4x+c
- The tangent line passes through the point (2,4) and hence substituting in the above equation we get:
- 4 = 4(2)+c ⟹ c = -4

- The final equation of the tangent line is y = 4x-4

### 3. f(x) = x^3+2x+1

This function is shown below, along with its tangent lines at x=0, x=2 and x=-2. Below are the steps to derive an equation of the tangent line at x=0.

- Equation of a line with slope m and y-intercept c is given by: y=mx+c
- Slope of the line at any point is given by the function f'(x) = 3x^2+2
- Slope of the tangent line to the curve at x=0 is 2, we get y=2x+c
- The tangent line passes through the point (0,1) and hence substituting in the above equation we get:
- 1 = 2(0)+c ⟹ c = 1

- The final equation of the tangent line is y = 2x+1

Note that the curve has the same slope at both x=2 and x=-2, and hence the two tangent lines at x=2 and x=-2 are parallel. The same would be true for any x=k and x=-k as f'(x) = f'(-x) = 3x^2+2

## Extensions

This section lists some ideas for extending the tutorial that you may wish to explore.

- Velocity and acceleration
- Integration of a function

If you explore any of these extensions, I’d love to know. Post your findings in the comments below.

## Further Reading

This section provides more resources on the topic if you are looking to go deeper.

### Tutorials

### Resources

- Additional resources on Calculus Books for Machine Learning

### Books

- Thomas’ Calculus, 14th edition, 2017. (based on the original works of George B. Thomas, revised by Joel Hass, Christopher Heil, Maurice Weir)
- Calculus, 3rd Edition, 2017. (Gilbert Strang)
- Calculus, 8th edition, 2015. (James Stewart)

## Summary

In this tutorial, you discovered the concept of the slope of a curve at a point and the tangent line to a curve at a point.

Specifically, you learned:

- What is the slope of a line
- What is the average rate of change of a curve over an interval w.r.t. x
- Slope of a curve at a point
- Tangent to a curve at a point

## Do you have any questions?

Ask your questions in the comments below and I will do my best to answer.

The post A Gentle Introduction to Slopes and Tangents appeared first on Machine Learning Mastery.