The lagged and iterated differences of a vector.
x is length
n, this function returns a length \(n-k\) vector of the \(k\)th order difference between the lagged terms.
diff(x) returns the vector of differences between adjacent elements in the vector, i.e. [x - x, x - x, ...].
diff(x,1,2) is the second-order differences vector, equivalently diff(diff(x)).
diff(x,1,0) returns the vector x unchanged.
diff(x,2) returns the vector of differences [x - x, x - x, ...], equivalent to
x[(1+lag):n] - x[1:(n-lag)].
# S4 method for Expression diff(x, lag = 1, differences = 1, ...)
An integer indicating which lag to use.
An integer indicating the order of the difference.
axis argument, specifying the dimension across which to apply the function:
1 indicates rows,
2 indicates columns, and
NA indicates rows and columns. The default is
axis = 1.
An Expression representing the
kth order difference.
## Problem data m <- 101 L <- 2 h <- L/(m-1) ## Form objective and constraints x <- Variable(m) y <- Variable(m) obj <- sum(y) constr <- list(x == 0, y == 1, x[m] == 1, y[m] == 1, diff(x)^2 + diff(y)^2 <= h^2) ## Solve the catenary problem prob <- Problem(Minimize(obj), constr) result <- solve(prob) ## Plot and compare with ideal catenary xs <- result$getValue(x) ys <- result$getValue(y) plot(c(0, 1), c(0, 1), type = 'n', xlab = "x", ylab = "y") lines(xs, ys, col = "blue", lwd = 2) grid()