derivative of a function

徘徊边缘 提交于 2019-12-09 04:03:50

问题


I am using D to get derivatives of a function. However, R does not simplify the expression when returning the derivative. I need to figure out if a function has a derivative that can be expressed generically. Is there some way in R to simplify the expression?

> D(expression(sqrt(1 - x^2)), 'x')
-(0.5 * (2 * x * (1 - x^2)^-0.5))
> D(D(expression(sqrt(1 - x^2)), 'x'), 'x')
-(0.5 * (2 * (1 - x^2)^-0.5 - 2 * x * (-0.5 * (2 * x * (1 - x^2)^-1.5))))

Secondly, is there a way in R to do numerical integration?


回答1:


library(Ryacas)
x <- Sym("x")
Simplify(deriv(sqrt(1 - x^2),x,2))  # return the result simplified

gives

expression((x^2 - 1 - x^2)/root(1 - x^2, 2)^3)

You can also try

PrettyForm(Simplify(deriv(sqrt(1 - x^2),x,2)))

which gives

   2        2  
  x  - 1 - x   
---------------
              3
    /      2 \ 
Sqrt\ 1 - x  / 

As for numerical integration try giving this to see what is available

library(sos)
findFn('{numerical+integration}')



回答2:


As far as I know, R will not simplify the result of D(). It sounds as though you want a proper computer algebra system, and R is definitely not a full CAS. Mathematica and Maple are the most well-known, but there are also a number of open-source alternatives (as discussed on this SO post).

R can do numerical integration - for this kind of question it is worth searching in the R help pages first (i.e. help.search('integrate')). You can use integrate() in the stats package. There is also area() in the MASS package, but that is much simpler (i.e. for demonstration purposes).



来源:https://stackoverflow.com/questions/3340690/derivative-of-a-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!