简体   繁体   English

计算列表中值的导数

[英]Calculating derivative at values in a list

I have a function, say f = f(x) 我有一个函数,说f = f(x)

I have a list of values of x. 我有一个x值列表。 q is just the data list q只是数据列表

I wish to create a new list with values of f'(x) for all x values I have. 我希望为我拥有的所有x值创建一个值为f'(x)的新列表。

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). f = sqrt(x),R将计算sqrt(x)的值,如果您未定义x(无论如何都不是您想要的),R会计算。 You have to pass an expression and substitute just don't evaluate. 您必须传递一个表达式并替换为不求值。 You want to eval after the derivative is computed. 您要在计算导数后进行评估。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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