How define a multivariate function in R

Viewed 120

I have the following function:

enter image description here

I would like to write a function in R for f. It should take as arguments x, c1, ..., cn and n.

1 Answers

If the argument lengths are variable, use 3 dots (...)

f1 <- function(x, ...) {
              2 * x + sum((x - c(...))^2)
}

-testing

f1(5, 10, 5, 2, 3)
[1] 48
Related