user4934761
user4934761

Reputation:

Calculating derivative at values in a list

I have a function, say f = f(x)

I have a list of values of x. q is just the data list

I wish to create a new list with values of f'(x) for all x values I have.

f = sqrt(x)
xt = ts(q)
count = length(xt)
f' = rep(0,count)
for (k in 2:count){
    f'[k] = D(f,"x")[k]
    }

This isn't working. Could someone please help?

Upvotes: 0

Views: 148

Answers (1)

Federico Manigrasso
Federico Manigrasso

Reputation: 1200

you can do this

  d<-D(substitute(sqrt(x)),"x")
x<-1:5 #if u have a list use unlist example x<-list(1,2,3) x<-unlist(x)
eval(d)

The trick is that you have to pass an expression, if you do something like

f=sqrt(x), R will compute the value of sqrt(x) and if you don't define x (and in any case is not what u want). You have to pass an expression and substitute just don't evaluate. You want to eval after the derivative is computed.

Upvotes: 0

Related Questions