[This article was first published on R programming tutorials and exercises for data science and mathematics, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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.
< details> < summary> Answer 1: click to revealWe 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()
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()
To leave a comment for the author, please follow the link and comment on their blog: R programming tutorials and exercises for data science and mathematics.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.