Cosine, sine and tangent in R
R provides several functions to compute trigonometric functions, such as the sine, cosine, tangent, arc-cosine, arc-sine and arc-tangent with the cos(), sin(), tan(), acos(), asin() and atan() functions. In this tutorial we will review how to compute and plot these functions.
Cosine function with cos
The cosine for an angle \(x\) is the length of side adjacent to angle \(x\) divided by the length of the hypotenuse. You can use the cos
function to compute the cosine function for a single value or a vector of values.
cos(1) # 0.5403
cos(0) # 1
cos(pi) # -1
cos(0.5) # 0.8776
cos(-1) # 0.5403
The following line of code displays the cosine function from -10 to 10.
plot(cos, -10, 10, col = 4, main = "cos(x)")
Sine function with sin
The sine is the length of the side opposite to angle \(x\) divided by the length of the hypotenuse. The function named sin
can be used to compute the sin of an angle, as shown in the examples below.
sin(1) # 0.8415
sin(0) # 0
sin(pi/2) # 1
sin(0.5) # 0.4794
sin(-1) # -0.8415
Use the following code to visually represent the sine.
plot(sin, -10, 10, col = 4, main = "sin(x)")
Tangent function with tan
The tangent of an angle \(x\) is the length of side opposite angle \(x\) divided by the length of the side adjacent to angle \(x\). In R, you can make use of the tan
function to calculate the tangent of an angle.
tan(1) # 1.557
tan(0) # 0
tan(pi) # -1.225e-16
tan(0.5) # 0.5463
tan(-1) # -1.557
The following line of code can be used to represent the tangent function from \(-2 \pi\) to \(2 \pi\).
plot(tan, -2 * pi, 2 * pi, col = 4, main = "tan(x)", type = "l")
Arc-cosine function with acos
The arccosine is the inverse function of the cosine for an angle \(x\). Use the acos
function to compute the values as in the examples below.
acos(1) # 0
acos(0) # 1.571
acos(pi) # NaN
acos(0.5) # 1.047
acos(-1) # 3.142
As the domain of the arccosine ranges from -1 to 1 you can use the following line of code to represent it.
plot(acos, -1, 1, col = 4, main = "acos(x)")
Arc-sine function with asin
The arcsine is the inverse function of the sine for an angle \(x\). In R, you can make use of the asin
function as in the following block of code.
asin(1) # 1.571
asin(0) # 0
asin(pi) # NaN
asin(0.5) # 0.5236
asin(-1) # -1.571
The domain of the arcsine also ranges from -1 to 1. Use the following line of code to represent this function.
plot(asin, -1, 1, col = 4, main = "asin(x)")
Arc-tangent function with atan
The arctangent is the inverse of the tangent function for an angle \(x\). Use the atan
function in R to compute it.
atan(1) # 0.7854
atan(0) # 0
atan(pi) # 1.263
atan(0.5) # 0.4636
atan(-1) # -0.7854
Use the following line of code if you want to plot the arctangent function in R:
plot(atan, -10, 10, col = 4, main = "atan(x)")
R provides other functions named cospi()
, sinpi()
and tanpi()
that compute cos(pi*x)
, sin(pi*x)
and tan(pi*x)
, respectively.
Angles are in radians for the standard versions, not degrees, and in ‘half-rotations’ for cospi()
, sinpi()
and tanpi()
.