# Exercise 4: R trigonometric functions

Find a function `FUN` that leads to the following output:

``````curve(FUN, from = -10, to = 10, col = "red", ylim = c(-2, 2))
curve(sin, add = TRUE)
grid()``````

Hint: aim to keep the answer simple. The main logic of the function can often be summarized in a single line of R code.

Answer 1: click to reveal

We can write the function as follows:

``````  FUN <- function(x) {
return(sin(x + pi / 2))
}``````

We then get:

``````  curve(FUN, from = -10, to = 10, col = "red", ylim = c(-2, 2))
curve(sin, add = TRUE)
grid()``````

Answer 2: click to reveal

We can use various trigonometric identities to derive more solutions to this puzzle. For example:

sin(x + pi/2) = cos(x)

`FUN` can then be defined as:

``````  FUN <- function(x) {
return(cos(x))
}``````

We can confirm that this function solves the puzzle:

``````  curve(FUN, from = -10, to = 10, col = "red", ylim = c(-2, 2))
curve(sin, add = TRUE)
grid()``````

For a full collection of R programming tutorials and exercises visit my website at codeRtime.org and the codeRtime YouTube channel.