Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

R sin() Function - Trigonometric Calculations

R sin() Function - Trigonometric Calculations

R Language Examples R Language Examples

R provides a complete set of trigonometric functions: sin(), cos(), tan(), and their inverses.

These functions use radians by default and are frequently used in scientific computing and signal processing.

The syntax of the trigonometric functions is as follows:

sin(x)

cos(x)

tan(x)

asin(x)   # 反正弦

acos(x)   # 反余弦

atan(x)   # 反正切

Parameter Description:

Example

Trigonometric values of common angles

angle_deg

<-

c

(

0

,

30

,

45

,

60

,

90

)

Convert degrees to radians

angle_rad

<-

angle_deg

pi

/

180

Compute the sine value

print

(

"sin value:"

)

print

(

round

(

sin

(

angle_rad

)

,

4

)

)

Compute the cosine value

print

(

"cos value:"

)

print

(

round

(

cos

(

angle_rad

)

,

4

)

)

Compute the tangent value

print

(

"tan value (note: infinite at 90 degrees):"

)

print

(

round

(

tan

(

angle_rad

)

,

4

)

)

The output of the code above is:

[1] "sin 值:"

[1] 0.0000 0.5000 0.7071 0.8660 1.0000

[1] "cos 值:"

[1] 1.0000 0.8660 0.7071 0.5000 0.0000

[1] "tan 值(注意 90 度无穷大):"

[1] 0.0000 0.5774 1.0000 1.7321      Inf

Use the inverse tangent function atan2() to compute the polar angle of a point (x, y) on the plane:

Example

Compute the polar angle of point (1,1) (should be 45 degrees)

angle_rad

<-

atan2

(

1

,

1

)

angle_deg

<-

angle_rad

180

/

pi

print

(

paste

(

"Angle of point (1,1):"

, angle_deg,

"degrees"

)

)

Compute the polar angle of point (-1, 1) (should be 135 degrees)

angle_rad

<-

atan2

(

1

,

1

)

angle_deg

<-

angle_rad

180

/

pi

print

(

paste

(

"Angle of point (-1,1):"

, angle_deg,

"degrees"

)

)

The output of the code above is:

[1] "点(1,1)的角度: 45 度"

[1] "点(-1,1)的角度: 135 度"

R Language Examples R Language Examples