简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM