Python, NumPy and MatPlotLib: Plotting Triangles on a Circle to Show the Waveform of sin, cos and tan

Triangle 1: 3,4,5 Triangle

Let’s create a plot of a 3,4,5 triangle, starting at the origin [0,0]. The smallest angle of the triangle will begin from the x-axis, thus the corner of the right angle of the triangle will lie on the x-axis. In this case a width of 4 starting so co-ordinate [4,0], we will then reach the furthest point of the triangle, this triangle has a height of 3 so this will be co-ordinate [4,0] and then in order to complete the triangle we need to return to the starting point i.e. the origin [0,0]. These four co-ordinates can be put together in a list i.e.

Python

And we can go ahead and plot the triangle.

Python

Adding text to the plot.

Python

Trigonometric Identities

The definition of the sine from the perspective of the angle θ is the ratio of the Opposite (O) to the Hypotenuse (H) for the cosine it is the ratio of the Adjacent (A) to the Hypotenuse (H) and for the tangent it is the ratio of the Opposite (O) to the Hypotenuse (H).

\displaystyle \sin \left( \theta \right)=\frac{O}{H}

\displaystyle \cos \left( \theta \right)=\frac{A}{H}

\displaystyle \tan \left( \theta \right)=\frac{O}{A}

For this particular triangle, the so called 3,4,5 triangle:

\displaystyle \sin \left( \theta \right)=\frac{3}{5}

\displaystyle \cos \left( \theta \right)=\frac{4}{5}

\displaystyle \tan \left( \theta \right)=\frac{3}{4}

Simply by definition:

\displaystyle \tan \left( \theta \right)=\frac{O}{A}=\frac{{\left( {\frac{O}{H}} \right)}}{{\left( {\frac{A}{H}} \right)}}=\frac{{\sin \left( \theta \right)}}{{\cos \left( \theta \right)}}=\frac{{\left( {\frac{3}{5}} \right)}}{{\left( {\frac{4}{5}} \right)}}=\frac{3}{4}

Now if look from the perspective of the angle γ then:

\displaystyle \sin \left( \gamma \right)=\frac{4}{5}

\displaystyle \cos \left( \gamma \right)=\frac{3}{5}

\displaystyle \tan \left( \gamma \right)=\frac{4}{3}

This means:

\displaystyle \sin \left( \gamma \right)=\frac{4}{5}=\cos \left( \theta \right)

\displaystyle \cos \left( \gamma \right)=\frac{3}{5}=\sin \left( \theta \right)

However the three sides of a triangle add up to 180 degrees. Since we are using a right angle triangle the two other angles must add up to 90 thus:

\displaystyle \gamma =90-\theta

It then follows:

\displaystyle \cos \left( \theta \right)=\sin \left( {90-\theta } \right)

\displaystyle \sin \left( \theta \right)=\cos \left( {90-\theta } \right)

Now  returning to the angle θ we have picked a right angle triangle with the sides O=3, A=4 and H=5. This triangle and all right angle triangles obey Pythagoras theorem:

\displaystyle {{H}^{2}}={{O}^{2}}+{{A}^{2}}

\displaystyle {{5}^{2}}={{4}^{2}}+{{3}^{2}}

\displaystyle 25=25

We can divide through by

\displaystyle {{H}^{2}}

To get:

\displaystyle \frac{{{{H}^{2}}}}{{{{H}^{2}}}}=\frac{{{{O}^{2}}}}{{{{H}^{2}}}}+\frac{{{{A}^{2}}}}{{{{H}^{2}}}}

\displaystyle 1={{\sin }^{2}}\left( \theta \right)+{{\cos }^{2}}\left( \theta \right)

If we plug in our values we can see:

\displaystyle 1={{\left( {\frac{4}{5}} \right)}^{2}}+{{\left( {\frac{3}{5}} \right)}^{2}}

\displaystyle 1=\frac{16}{{25}}+\frac{{9}}{{25}}

\displaystyle 1=1

We can measure these angles using a protractor and find that θ=36.87.

If we normalise by H meaning we divide through by 5, we instead get the side lengths O=4/5, A=3/5 and H=5/5=1.

Python

Triangle 2: 30,60,90 Degrees Triangle

Another special triangle is the so called 30,60, 90 degrees triangle because each angle is a multiple of 30 degrees.

Python

Here:

\displaystyle \theta =30

\displaystyle 90-\theta =60

\displaystyle O=1

\displaystyle A=\sqrt{3}

\displaystyle H=2

We can also plot this by dividing through by H i.e. O=1/2=0.5, A=√3/2, and H=4/2=2.

Let’s now plot this alongside the normalised first triangle.

Python

Triangle 3: Right Angle Isosceles Triangle

For this right triangle, O and A are equal and we are interested in a normalised triangle so H=1. It therefore follows from Pythagoras Theorem:

\displaystyle {{H}^{2}}={{O}^{2}}+{{A}^{2}}

Since O and A are equal:

\displaystyle {{H}^{2}}=2{{O}^{2}}

Since H=1:

\displaystyle {{1}^{2}}=2{{O}^{2}}

Then:

\displaystyle {{O}^{2}}=\frac{1}{2}

And:

\displaystyle O=\frac{1}{{\sqrt{2}}}=A

Since the three angles in a triangle add up to 180 degrees and a right angle triangle has a 90 degree angle.

\displaystyle \theta +\gamma +90=180

\displaystyle \theta +\gamma =90

It follows that isosceles which has two equal angles:

\displaystyle 2\theta =90

Then:

\displaystyle \theta =\frac{{90}}{2}

\displaystyle \theta =45=\gamma

We can plot this normalised triangle alongside the other two:

Python

We can actually re-arrange the code so that it the plots are listed from smallest θ to largest θ.

Straight Line

We can look at the case when θ=0, in this case γ=180-90-0=90 and when H=1, the adjacent A=H=1 and the opposite O=0.

Python

Let’s also plot a semi-circle and let’s also plot the point each triangle intersects with the circle. This will be the 2nd point in each array, so we can index that point and create a scatter plot of that single point.

Python

Triangle 4: θ=15

So far we have looked at θ=0,30,45 and 36.87. It is worthwhile also looking at what happens when θ=15 as this will give θ=0,15,30,45 i.e. equal steps of 15. Since the chart above is done on a square plot, it can be printed and a protractor can be used to measure an angle, θ=15, so that a straight line can be drawn to the circle. This line will be the Hypotenuse and will be equal to 1 as it is the radius of the circle. At the end-point of this line, a straight line can be drawn to the x-axis and then a straight line can be drawn alongside the x-axis to return one to the origin. This completes the triangle. These measurements will give θ=15 (defined), H=1 defined, O=0.2588, A=0.9659 and γ =90-θ=75.

Modify diagram…

Angles from θ=0-45

This gives us:

θγOAH
0900.00001.0000 1.0000
15750.25880.9659 1.0000
30600.50000.8660 1.0000
45450.70710.7071 1.0000
36.8753.130.36000.8000 1.0000

Let’s now examine only the angles that are multiples of 15 degrees.

Python

Angles from θ=45-90

From earlier we deduced:

\displaystyle \cos \left( \theta \right)=\sin \left( {90-\theta } \right)

This essentially means we can draw all the triangles starting from the y-axis and work our way back and every point should still lie on the semi-circle. In essence we transpose the x and y co-ordinates.

θOAH
00.00001.00001.0000
90-0=901.00000.00001.0000
150.25880.96591.0000
90-15=750.96590.25881.0000
300.50000.86601.0000
90-30=600.86600.50001.0000
450.70710.70711.0000
90-45=450.70710.70711.0000
36.870.36000.80001.0000
90-36.87=53.130.80000.36001.0000

These new points can be plotted, alongside the original.

Python

These angles can be listed in order (increments of 15)

θOAH
00.00001.00001.0000
150.25880.96591.0000
300.50000.86601.0000
450.70710.70711.0000
600.86600.50001.0000
750.96590.25881.0000
901.00000.00001.0000

Angles from θ=90-180

If we extend the plot so we see the circle at θ=90-180.

Python

We can see by symmetry about the origin that these have the negative x-value and same y-value. Therefore we can take the angle θ from the first quadrant and subtract it from 180 to get the θ in the second quadrant. A and H remain unchanged in value but O is in a different direction and it’s sign changes.

Python
θOAH
00.00001.00001.0000
150.25880.96591.0000
300.50000.86601.0000
450.70710.70711.0000
600.86600.50001.0000
750.96590.25881.0000
901.00000.00001.0000

Taking mirror symmetry in x for the second quadrant.

θOAH
180-0=1800.0000-1×1.00001.0000
180-15=1750.2588-1×0.96591.0000
180-30=1500.5000-1×0.86601.0000
180-45=1350.7071-1×0.70711.0000
180-60=1200.8660-1×0.50001.0000
180-75=1150.9659-1×0.25881.0000
180-90=901.0000-1×0.00001.0000

Gives

θOAH
901.0000-0.00001.0000
1150.9659-0.25881.0000
1300.8660-0.50001.0000
1350.7071-0.70711.0000
1500.5000-0.86001.0000
1750.2588-0.96591.0000
1800.0000-1.00001.0000

Angles from θ=180-270

We can now extend the circle into the third quadrant.

Python

We can also see that it has symmetry with the first quadrant but both the x and y values are reversed. In other words we add 180 to the original θ from the first quadrant and to get the new θ and we swap the signs of both x and y with respect to the first quadrant.

θOAH
00.00001.00001.0000
150.25880.96591.0000
300.50000.86601.0000
450.70710.70711.0000
600.86600.50001.0000
750.96590.25881.0000
901.00000.00001.0000

Taking mirror symmetry in x and y for the third quadrant.

θOAH
180+0=180-1×0.0000-1×1.00001.0000
180+15=195-1×0.2588-1×0.96591.0000
180+30=210-1×0.5000-1×0.86601.0000
180+45=225-1×0.7071-1×0.70711.0000
180+60=240-1×0.8660-1×0.50001.0000
180+75=255-1×0.9659-1×0.25881.0000
180+90=270-1×1.0000-1×0.00001.0000

Gives:

θOAH
180-0.0000-1.00001.0000
195-0.2588-0.96591.0000
210-0.5000-0.86601.0000
225-0.7071-0.70711.0000
240-0.8660-0.50001.0000
255-0.9659-0.25881.0000
270-1.0000-0.00001.0000

Python

Angles from θ=270-360

To complete the circle, we can extend it to the 4th quadrant.

Python

We can also see that it has symmetry with the first quadrant and the x values have the same sign but the y values are reversed.

θOAH
00.00001.00001.0000
150.25880.96591.0000
300.50000.86601.0000
450.70710.70711.0000
600.86600.50001.0000
750.96590.25881.0000
901.00000.00001.0000

In other words we subtract the original θ from the first quadrant from 360 to get the new θ and we swap the signs of only y with respect to the first quadrant.

θOAH
360-0=360=0-1×0.00001.00001.0000
360-15=345-1×0.25880.96591.0000
360-30=330-1×0.50000.86601.0000
360-45=315-1×0.70710.70711.0000
360-60=300-1×0.86600.50001.0000
360-75=285-1×0.96590.25881.0000
360-90=270-1×1.0000-0.00001.0000

Which is the following in order.

θOAH
270-1.00000.00001.0000
285-0.96590.25881.0000
300-0.86600.50001.0000
315-0.70710.70711.0000
330-0.50000.86001.0000
345-0.25880.96591.0000
360=0-0.00001.00001.0000
Python

Plotting the sin, cos and tan data

We have the O,A and H value for θ for every 15 degrees of θ. We can go ahead and save these in bumpy arrays and then plot, O/H which is the sin function, A/H which is the cos function and O/A which is the tan function.

Python

Recall that O was positive in the 1st quadrant, positive in the 2nd quadrant, negative in the 3rd and negative in the 4th quadrant. The symmetry between the values in the 4 quadrants can be seen.

Recall that A was positive in the 1st quadrant, negative in the 2nd quadrant, negative in the 3rd and positive in the 4th quadrant. The symmetry between the values in the 4 quadrants can likewise be seen.

Recall that the tan function is sin divided by cos. It goes up to infinity as we approach 1 divided by 0 or to minus infinity as we approach -1 divided by 0.

Recall also that θ=90-γ, this is seen in the sin and cos curves in that they are out of phase by 90 degrees.

Units of Angles (θ)

Degree

So far we have used the angle degree. The degree is an arbitrary unit and divides the circle into 360 equally spaced angles or the right angle into 90 equally spaced angles. This like time, is taken from the Babylonians who liked base 12 and base 60 units.

Gradian

A not too successful attempt to make a metric degree was the gradian. Here the right angle would be divided up into 100 parts and the circle which has 4 quadrants would thus have 100 degrees.

Length of an Arc

The circumference of a circle is equal to:

\displaystyle C=2\pi r

Where the constant in decimal format is equal to:

\displaystyle \pi =\text{ 3}\text{.1416...}

To specify the length of an arc (fraction of a circle in degrees) we would use:

\displaystyle L=2\pi r*\left( {\frac{\theta }{{360}}} \right)

Radian

The above is quite messy in decimal format so another unit of measurement is commonly used to simplify the above, the radian.

\displaystyle L=2\pi r*\left( {\frac{\theta }{{2\pi }}} \right)

For the radian, there are 2π radians in a circle and 2π/4 or π/2 radians in a right angle. The above hence simplifies to:

\displaystyle L=r\theta

This means the length of the arc is equal to the product of the radius and the angle.

Conversion Degrees to Radians

The disadvantage of the radian unit is the fact that pi does not converge to a whole number or a few decimal places when using the decimal system of numbers (0,1,2,3,4,5,6,7,8,9,10,…). Therefore it is quite common for instruments such as protractors to be in degrees opposed to radians but more convenient when using a computer for the angle to be in radians as it immediately relates to the length of an arc. In essence both units are commonly used and as a result one must be converted to the other. The following function converts from degrees to radians:

Python

And likewise the following function converts from radians to degrees:

Python

sin, cos and tan

The sin, cos and tan functions measure the sin, cos and tan of an angle in radians.

Python
Python
Python

We have the following angles.

θOAH
00.00001.00001.0000
150.25880.96591.0000
300.50000.86601.0000
450.70710.70711.0000
600.86600.50001.0000
750.96590.25881.0000
901.00000.00001.0000

If we calculate the sin of them we should get the values of O because H=1 and O/H=O/1=O.

Python
theta_deg=
[ 0 15 30 45 60 75 90]
theta_rad=
[0.         0.26179939 0.52359878 0.78539816 1.04719755 1.30899694
 1.57079633]
O=
[0.         0.25881905 0.5        0.70710678 0.8660254  0.96592583
 1.        ]

As we can see this is the case.

Likewise if we calculate the cos of them we should get the values of A because H=1 and A/H=A/1=A.

Python
theta_deg=
[ 0 15 30 45 60 75 90]
theta_rad=
[0.         0.26179939 0.52359878 0.78539816 1.04719755 1.30899694
 1.57079633]
O=
[0.         0.25881905 0.5        0.70710678 0.8660254  0.96592583
 1.        ]
theta_deg=
[ 0 15 30 45 60 75 90]
theta_rad=
[0.         0.26179939 0.52359878 0.78539816 1.04719755 1.30899694
 1.57079633]
A=
[1.00000000e+00 9.65925826e-01 8.66025404e-01 7.07106781e-01
 5.00000000e-01 2.58819045e-01 6.12323400e-17]

Once again we can see this is the case.

We can also compare the ratios of O/A which should be equal to the tan of the angles.

Python
[ 0 15 30 45 60 75 90]
theta_rad=
[0.         0.26179939 0.52359878 0.78539816 1.04719755 1.30899694
 1.57079633]
theta_deg=
[ 0 15 30 45 60 75 90]
theta_rad=
[0.         0.26179939 0.52359878 0.78539816 1.04719755 1.30899694
 1.57079633]
O=
[0.         0.25881905 0.5        0.70710678 0.8660254  0.96592583
 1.        ]
A=
[1.00000000e+00 9.65925826e-01 8.66025404e-01 7.07106781e-01
 5.00000000e-01 2.58819045e-01 6.12323400e-17]
O/A=
[0.00000000e+00 2.67949192e-01 5.77350269e-01 1.00000000e+00
 1.73205081e+00 3.73205081e+00 1.63312394e+16]
tan(θr)=O/A=
[0.00000000e+00 2.67949192e-01 5.77350269e-01 1.00000000e+00
 1.73205081e+00 3.73205081e+00 1.63312394e+16]

Once again we see this is the case.

Plotting a Circle

Previously we added an arc to the plot and expanded it to a circle. You shpould now understand that the x co-ordinate which is A is obtained by the cos function and the y co-ordinate which is O is obtained by the sin function. r is the radius which was set to 1 in our circle.

Python
Advertisements