by Dr Philip Yip (Dell Community Rockstar and Microsoft Windows Insider MVP)
Trigonometry – sin and cos
This example graphically shows how you obtain the sin(θ) and cos(θ) plots and their ratio tan(θ) using a series of right angle triangles of Hypotenuse 1 and a Circle of Radius 1. It also gives the user some practice with their plotting with MATLAB.
Pythagorean Triple (3,4,5)
We can plot a classic 3,4,5 right angle triangle in MATLAB using the code:
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).
Let's have a look at these:
Simply by definition:
Now if look from the perspective of the angle γ then:
This means:
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:
It then follows:
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:
We can divide through by
If we plug in our values we can see:
We can measure these angles using a protractor and find that θ=36.87.
If we normalise meaning we divide through by 5, we instead get the side lengths O=4/5, A=3/5 and H=1.
We have plotted this on a grid centred at an origin. H for both triangles is 1. The end point of each triangle has an x value corresponding to the triangles Adjacent and a y value corresponding to the triangles Opposite sides. Since H=1 these are the values of sin(θ) and cos(θ) respectively.
0 Degrees – A Straight Line
Continuing with first let's see when the angle θ=0, this means that and
We can update the plot to include this additional triangle. Try this yourself:
%% Three Triangles and Line % 3,4,5 triangle normalised so H=1
x1=[0,(4/5),(4/5),0];
y1=[0,0,(3/5),0]; % 30, 60, 90 triangle normalised so H=1
x2=[0,(3^0.5)/2,(3^0.5)/2,0];
y2=[0,0,0.5,0]; % Line O=H=1
x3=[0,1];
y3=[0,0]; x4=[0,(1/2^0.5),(1/2^0.5),0];
y4=[0,0,(1/2^0.5),0]; scatter(x1,y1,50,'r'); hold('on'); plot(x1,y1,'r:'); scatter(x2,y2,50,'g'); plot(x2,y2,'g:'); scatter(x3,y3,50,'c'); plot(x3,y3,'c:'); scatter(x4,y4,50,'m'); plot(x4,y4,'m:'); hold('off'); % Square Grid centred at 0,0 xlim([-1.2,1.2]); ylim([-1.2,1.2]);
ax=gca;
ax.XAxisLocation='origin';
ax.YAxisLocation='origin'; grid('minor'); axis('square'); xlabel('x'); ylabel('y');
If we update the plots to include an Arc of constant radius (Hypotenuse) of 1 we can see that the end point of each triangle lies on this Arc.
So far we have triangles for: θ=0, 30, 36.87 and 45. Let's draw the point for θ=15 and remove θ=36.87 so we are evenly spaced. For this triangle O=0.2588 and A=0.9659.
%% First Quarter: 0:15:45 Degrees % Line \theta=15 x0=[0,1]; y0=[0,0]; % Line \theta=15
x15=[0,0.9659,0.9659,0];
y15=[0,0,0.2588,0]; % Line \theta=30
x30=[0,(3^0.5)/2,(3^0.5)/2,0];
y30=[0,0,0.5,0]; % Line \theta=45 x45=[0,(1/2^0.5),(1/2^0.5),0];
y45=[0,0,(1/2^0.5),0]; scatter(x0,y0,50,'c'); hold('on'); plot(x0,y0,'c:'); scatter(x15,y15,50,'b'); plot(x15,y15,'b:'); scatter(x30,y30,50,'g'); plot(x30,y30,'g:'); scatter(x45,y45,50,'m'); plot(x45,y45,'m:'); hold('off'); % Square Grid centred at 0,0 xlim([-1.2,1.2]); ylim([-1.2,1.2]);
ax=gca;
ax.XAxisLocation='origin';
ax.YAxisLocation='origin'; grid('minor'); axis('square'); xlabel('x'); ylabel('y');
This gives us:
Angles from 45-90
From earlier we deduced:
We have drawn the triangle at θ=36.87, we can also redraw this triangle starting on the y axis (90) and working back from 90 to subtract θ. This gives 90-θ=90-36.87=53.13 (the angle from the x-axis that we are interested in). In other words this is the transpose of the first triangle. Draw the normalised 3,4,5 sided triangle and it's transpose.
We are taken to the fourth quadrant, note as we are drawing a point on a circle with radius 1, this mirror image in y is still on the circle.
Therefore:
Quarter 1:
Quarter 2:
Quarter 3:
Quarter 4:
This means from the 4 points we calculated from the line (θ=0) and 3 triangles with θ=15, 30 and 45 respectively using symmetry we have obtained 24 points which covers the full circle and is crude enough to get a first hand approximation of the sine, cosine and tangent. Try to plot this:
We can plot with respect to and because this is the sin() likewise we can plot with respect to and because this is the cos(). Moreover is the ratio of these two values. Try this yourself.
Plot of A=A/H=cos(θ) as H=1 versus θ. Note that the sin(θ-90)=cos(θ) as expected: the cos(θ) plot is identical in form to the sin(θ) plot but out of phase by 90.
Plot of A=O/A=sin(θ)/cos(θ)=tan(θ), the ratio of the two plots above.
A Schematic of the Steps we Just Carried Out
Plot a circle of radius one. Draw lines until you reach the circle (radius 1) at angles 0,15,30 and 45:
From these angles exploit the fact that sin(θ)=cos(90-θ) and meaning you can transpose the O (y) and A (x) values to complete the arc up until 90.
Next exploit the fact that you are plotting on a circle and the second quadrant is mirrored in x (-x and +y).
Next exploit the fact that you are plotting on a circle and the third quadrant is mirrored in x and y (-x and -y).
Finally exploit the fact that you are plotting on a circle and the third quadrant is mirrored in y only (x and -y).
These functions are cyclic, meaning if you go around the circle again you will get the same graph again. MATLAB has the following inbuilt functions that work with angles in Degrees:
sind(deg) cosd(deg) tand(deg)
Use them to make a plot of sind and cosd for angles from 0 to 1080 degrees. You should have both of these on the same plot with a legend, sind should have a blue line and cosd should have a red line.
%% plot using sind and cosd
% Data
t1=[0:1080]';
y1=sind(t1);
y2=cosd(t1); % Plots plot(t1,y1,'color','r','LineWidth',2); hold('on'); plot(t1,y2,'color','b','LineWidth',2); hold('off'); xlabel('\theta in Degrees'); grid('minor'); xlim([0,1080]); legend('sind(\theta)','cosd(\theta)','Location','NorthEast');
And the following return an angle in Degrees:
asind(ratio) acosd(ratio) asind(ratio)
Use these to verify the angle of the 3 by 4 by 5 triangle: