Skip to content

first and second derivative calculation

5 messages · Doran, Harold, Marlin Keith Cox, David Winsemius +1 more

#
3 * t^2 - 6 * (2 * t) + 5
3 * (2 * t) - 6 * 2

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Marlin Keith Cox
Sent: Friday, January 22, 2010 4:37 PM
To: r-help at r-project.org
Subject: [R] first and second derivative calculation

I would like to calculate a first and second derivative and am having
problems finding a simple solution.  My syntax may be off as I am not a
mathematician, so pardon ahead of time.
data:
t<-seq(0,4, by=.1)
The function is:
H(t) = t^3-6*t^2+5*t + 30

from here I plot the curve:
plot(x,y ,xlab="x-values", ylab="f(x)", type="l")
But would like to similarly plot the curve for both the first and second
derivatives.
I can calculate the derivatives by hand but would like to get R to do this
for me.
by hand:
H'(t) = 3*t^2 - 12*t + 5
H''(t) = 6*t-12
Keith
#
On Jan 22, 2010, at 6:49 PM, Marlin Keith Cox wrote:

            
There might be some sort of deparse() operation that one could do on  
y1, but what follows sidesteps that level of programming.


y1fn <- function(t) {3 * t^2 - 6 * (2 * t) + 5}
par(new=TRUE)
plot(t, y1fn(t), ylab="", xlab="", axes=FALSE)
  axis(side=4, at=seq(-7,5,by=1) )
David.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
#
How about?

  eval( D( expression( t^3-6*t^2+5*t+30 ), "t" ) )
David Winsemius wrote: