Storing a value of a function in a vector in R programming

Viewed 30

I'm new to R and I'm trying to store the value of the following function inside a vector in r. f(x) = 0.1e^x . cosx + 2ln|x| and I want to find the summation of this function when x = (2,2.1,2.2,...) my code so far is the following:

           VecA <- c(function(x){
           0.1*exp(x)*cos(x)+2*log(|x|)
            })

Can someone please help me with this. Thanks in Advance!

1 Answers

I write the function as follows:

VecA <- function(x){
  0.1*exp(x)*cos(x)+2*log(abs(x))
}

I set values in vector:

x = c(2,2.1,2.2)

Finally, I find the summation of these values:

sum(VecA(x))

#Output: [1] 3.196202

Related