# Derivative of the Sine and Cosine

Many machine learning algorithms involve an optimization process for different purposes. Optimization refers to the problem of minimizing or maximizing an objective function by altering the value of its inputs.

Optimization algorithms rely on the use of derivatives in order to understand how to alter (increase or decrease) the input values to the objective function, in order to minimize or maximize it. It is, therefore, important that the objective function under consideration is differentiable.

The two fundamental trigonometric functions, the sine and cosine, offer a good opportunity to understand the manoeuvres that might be required in finding the derivatives of differentiable functions. These two functions become especially important if we think of them as the fundamental building blocks of more complex functions.

In this tutorial, you will discover how to find the derivative of the sine and cosine functions.

After completing this tutorial, you will know:

• How to find the derivative of the sine and cosine functions by applying several rules from algebra, trigonometry and limits.
• How to find the derivative of the sine and cosine functions in Python.

Let’s get started.

Derivative of the Sine and Cosine
Photo by Tim Marshall, some rights reserved.

## Tutorial Overview

This tutorial is divided into three parts; they are:

• The Derivative of the Sine Function
• The Derivative of the Cosine Function
• Finding Derivatives in Python

## The Derivative of the Sine Function

The derivative f’(x) of some function, f, at a particular point, x, may be specified as:

We shall start by considering the sine function. Hence, let’s first substitute for f(x) = sin x:

If we have a look at the trigonometric identities, we find that we may apply the addition formula to expand the sin(x + h) term:

sin(x + y) = sin x cos y + cos x sin y

Indeed, by substituting y with h we can define the derivative of sin x as:

We may simplify the expression further by applying one of the limit laws, which states that the limit of a sum of functions is equal to the sum of their limits:

We may simplify even further by bringing out any common factor that is a function of x. In this manner, we can factorise the expression to obtain the sum of two separate limits that do not depend on x:

Solving each of these two limits will give us the derivative of sin x.

Let’s start by tackling the first limit.

Recall that we may represent angle, h in radians, on the unit circle. The sine of h would then be given by the perpendicular to the x-axis (BC), at the point that meets the unit circle:

Representing angle, h, on the unit circle

We will be comparing the area of different sectors and triangles, with sides subtending the angle h, in an attempt to infer how ((sin h) / h) behaves as the value of h approaches zero. For this purpose, consider first the area of sector OAB:

Finding the area of sector, OAB

The area of a sector can be defined in terms of the circle radius, r, and the length of the arc AB, h. Since the circle under consideration is the unit circle, then r = 1:

area_of_sector_OAB = r h / 2 = h / 2

We can compare the area of the sector OAB that we have just found, to the area of the triangle OAB within the same sector.

Finding the area of triangle, OAB

The area of this triangle is defined in terms of its height, BC = sin h, and the length of its base, OA = 1:

area_of_triangle_OAB = (BC) (OA) / 2 = (sin h) / 2

Since we can clearly see that the area of the triangle, OAB, that we have just considered is smaller that the area of the sector that it is contained within, then we may say that:

(sin h) / 2 < h / 2

(sin h) / h < 1

This is the first piece of information that we have obtained regarding the behaviour of ((sin h) / h), which tells us that its upper limit value will not exceed 1.

Let us now proceed to consider a second triangle, OAB’, that is characterised by a larger area than that of sector, OAB. We can use this triangle to provide us with the second piece of information about the behaviour of ((sin h) / h), which is its lower limit value:

Comparing similar triangles, OAB and OAB’

Applying the properties of similar triangles to relate OAB’ to OCB, gives us information regarding the length, B’A, that we need to compute the area of the triangle:

B’A / OA = BC / OC = (sin h) / (cos h)

Hence, the area of triangle OAB’ may be computed as:

area_of_triangle_OAB’ = (B’A) (OA) / 2 = (sin h) / (2 cos h)

Comparing the area of triangle OAB’ to that of sector OAB, we can see that the former is now larger:

h / 2 < (sin h) / (2 cos h)

cos h < (sin h) / h

This is the second piece of information that we needed, which tells us that the lower limit value of ((sin h) / h) does not drop below cos h. We also know that as h approaches 0, the value of cos h approaches 1.

Hence, putting the two pieces of information together, we find that as h becomes smaller and smaller, the value of ((sin h) / h) itself is squeezed to 1 by its lower and upper limits. This is, indeed, referred to as the squeeze or sandwich theorem.

Let’s now proceed to tackle the second limit.

By applying standard algebraic rules:

We can manipulate the second limit as follows:

We can then express this limit in terms of sine, by applying the Pythagorean identity from trigonometry, sin2h = 1 – cos2h:

Followed by the application of another limit law, which states that the limit of a product is equal to the product of the separate limits:

We have already tackled the first limit of this product, and we have found that this has a value of 1.

The second limit of this product is characterised by a cos h in the denominator, which approaches a value of 1 as h becomes smaller. Hence, the denominator of the second limit approaches a value of 2 as h approaches 0. The sine term in the numerator, on the other hand, attains a value of 0 as h approaches 0. This drives not only the second limit, but also the entire product limit to 0:

Putting everything together, we may finally arrive to the following conclusion:

sin’(x) = (1) (cos x) + (0) (sin x)

sin’(x) = cos x

This, finally, tells us that the derivative of sin x is simply cos x.

## The Derivative of the Cosine Function

Similarly, we can calculate the derivative of the cosine function by re-using the knowledge that we have gained in finding the derivative of the sine function. Substituting for f(x) = cos x:

The addition formula is now applied to expand the cos(x + h) term as follows:

cos(x + y) = cos x cos y + sin x sin y

Which again leads to the summation of two limits:

We can quickly realise that we have already evaluated these two limits in the process of finding the derivative of sine; the first limit approaches 1, whereas the second limit approaches 0, as the value of h become smaller:

cos’(x) = (1) (-sin x) + (0) (cos x)

cos’(x) = -sin x

Which, ultimately, tells us that the derivative of cos x is conversely -sin x.

The importance of the derivatives that we have just found lies in their definition of the rate of change of the function under consideration, at some particular angle, h. For instance, if we had to recall the graph of the periodic sine function, we can observe that its first positive peak coincides with an angle of π / 2 radians.

Line Plot of the Periodic Sine Function

We can use the derivative of the sine function in order to compute directly the rate of change, or slope, of the tangent line at this peak on the graph:

sin’(π / 2) = cos(π / 2) = 0

We find that this result corresponds well with the fact that the peak of the sine function is, indeed, a stationary point with zero rate of change.

A similar exercise can be easily carried out to compute the rate of change of the tangent line at different angles, for both the sine and cosine functions.

## Finding Derivatives in Python

In this section, we shall be finding the derivatives of the sine and cosine functions in Python.

For this purpose, we will be making use of the SymPy library, which will let us deal with the computation of mathematical objects symbolically. This means that the SymPy library will let us define and manipulate the sine and cosine functions, with unevaluated variables, in symbolic form. We will be able to define a variable as symbol by making use of symbols in Python, whereas to take the derivatives we shall be using the diff function.

Before proceeding further, let us first load the required libraries.

We can now proceed to define a variable x in symbolic form, which means that we can work with x without having to assign it a value.

Next, we can find the derivative of the sine and cosine function with respect to x, using the diff function.

We find that the diff function correctly returns cos(x) as the derivative of sine, and –sin(x) as the derivative of cosine.

The diff function can take multiple derivatives too. For example, we can find the second derivative for both sine and cosine by passing x twice.

This means that, in finding the second derivative, we are taking the derivative of the derivative of each function. For example, to find the second derivative of the sine function, we take the derivative of cos(x), its first derivative. We can find the second derivative for the cosine function by similarly taking the derivative of –sin(x), its first derivative.

We can, alternatively, pass the number 2 to the diff function to indicate that we are interested in finding the second derivative.

Tying all of this together, the complete example of finding the derivative of the sine and cosine functions is listed below.

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

## Summary

In this tutorial, you discovered how to find the derivative of the sine and cosine functions.

Specifically, you learned:

• How to find the derivative of the sine and cosine functions by applying several rules from algebra, trigonometry and limits.
• How to find the derivative of the sine and cosine functions in Python.

Do you have any questions?