On Sat, 29 Aug 2020 21:15:56 +0000 "Sorkin, John" <jsor...@som.umaryland.edu> wrote:
> I am trying to find the minimum of a linear function: Quadratic function??? > y <- (-0.0263*b) + (0.0010*B^2) > > I am having GREAT difficulty with the documentation of the deriv > function. I have (after playing for two-hours) been able to get the > following to work: > > zoop <- deriv(expression((-0.0263*B)+(0.0010*B^2)),"B",func=TRUE) > class(zoop) > zoop(2) > > which appears to give me the value of the derivative of my expression > w.r.t. B (I am not certain what the func arugment does, but it > appears to be necessary) It causes deriv() to return a *function* rather than an *expression*. > > Following what one learns in calculus 1, I now need to set the > derivative equal to 0 and solve for B. I have no idea how to do this > > Can someone point me in the right direction. Additionally can someone > suggest documentation for deriv that is easily intelligible to > someone who wants to learn how to use the function, rather that > documentation that helps one who is already familiar with the > function. (I have a need for derivatives that is beyond finding the > minimum of a function) > > Thank you > John > > P.S. Please don�t flame. I spent a good deal of time looking at > documentation and searching the internet. There may be something on > line, but I clearly am not using the correct search terms. Couple of things that you could play around with. y <- expression(-0.0263*B + 0.0010*B^2) z <- deriv(y,"B",func=TRUE) f <- function(x,z){as.vector(attr(z(x),"gradient"))} (1) uniroot(f,c(5,15),z=z)$root # 13.15 --- right answer!!! :-) (2) library(polynom) # You may need to install this package. p <- poly.calc(x=1:2,y=f(1:2,z=z)) polyroot(p) # 13.15+0i You can get rid of the extraneous imaginary part # by using Re(polyroot(p)) HTH cheers, Rolf P.S. It's irritating the way that one has to fiddle about in order to get a function that returns the value of the derivative, rather than the value of the function being differentiated! R. -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.