Sorkin, John
2020-Aug-29 21:15 UTC
[R] Solving derivates, getting the minimum of a function, and helpful documentation of the deriv function
I am trying to find the minimum of a linear 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) 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. [[alternative HTML version deleted]]
Roy Mendelssohn - NOAA Federal
2020-Aug-29 21:25 UTC
[R] Solving derivates, getting the minimum of a function, and helpful documentation of the deriv function
Hi John: Can I ask if this is the specific problem you are after, or a test for more general problem? If the former, the derivative is -0.0263 + 0.002 * B so the solution for B is: B = (0263)/0.002 If you are after a more general way fo doing this: ?solve -Roy> On Aug 29, 2020, at 2:15 PM, Sorkin, John <jsorkin at som.umaryland.edu> wrote: > > I am trying to find the minimum of a linear 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) > > 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. > > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.********************** "The contents of this message do not reflect any position of the U.S. Government or NOAA." ********************** Roy Mendelssohn Supervisory Operations Research Analyst NOAA/NMFS Environmental Research Division Southwest Fisheries Science Center ***Note new street address*** 110 McAllister Way Santa Cruz, CA 95060 Phone: (831)-420-3666 Fax: (831) 420-3980 e-mail: Roy.Mendelssohn at noaa.gov www: https://www.pfeg.noaa.gov/ "Old age and treachery will overcome youth and skill." "From those who have been given much, much will be expected" "the arc of the moral universe is long, but it bends toward justice" -MLK Jr.
Rolf Turner
2020-Aug-30 01:36 UTC
[R] Solving derivates, getting the minimum of a function, and helpful documentation of the deriv function
On Sat, 29 Aug 2020 21:15:56 +0000 "Sorkin, John" <jsorkin at 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