I have two snippets of code which I would have expected to behave the same, but they don’t:
position <- function(t) {
coordinates <- c(cosh(t), sinh(t))
return(coordinates[1])
}
and
position <- function(t) {
coordinates <- c(cosh(t), sinh(t))
return(cosh(t))
}
I use the function position to plot a curve. With the first snippet the curve is not plotted. With the second snippet the curve is plotted.
What is the functional difference between the two snippets, and why?
The result of
is a numeric vector of length
2 * length(t).The command
returns only the first value of this vector. (The result of
coordinates[1]andcosh(t)are only identical iflength(t) == 1.) To return the result ofcosh(h), you could indexcoordinateswith a sequence based on the length oft: