16

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?

4

2 回答 2

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

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

你也可以试试

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

这使

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

至于数值积分,试着给出这个看看有什么可用的

library(sos)
findFn('{numerical+integration}')
于 2010-07-28T10:19:32.450 回答
2

据我所知,R 不会简化D(). 听起来好像你想要一个合适的计算机代数系统,而 R 绝对不是一个完整的 CAS。MathematicaMaple是最知名的,但也有许多开源替代品(如这篇 SO 帖子中所讨论的)。

R 可以进行数值积分 - 对于此类问题,值得首先在 R 帮助页面中搜索(即help.search('integrate'))。您可以integrate()stats包装中使用。包中也有area()MASS但要简单得多(即用于演示目的)。

于 2010-07-27T06:55:59.003 回答