derivative of a function

user236215 picture user236215 · Jul 27, 2010 · Viewed 40.7k times · Source

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?

Answer

George Dontas picture George Dontas · Jul 28, 2010
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}')