[R] Minimizing two non-linear functions with genoud - Trying to minimize or converge near zero
Ravi Varadhan
rvaradhan at jhmi.edu
Fri Feb 5 00:54:36 CET 2010
This works!
f=function(x) {
# x = c(0.16,80)
Vcmax = x[2]
gi = x[1]
# First dataset
f.1=(-(((Vcmax-0.89189)/gi)+164.6573+272.38*(1+21*10/165.82))+sqrt((((Vcmax-0.89189)/gi)+164.6573+272.38*(1+21*10/165.82))^2-4*(-1/gi)*(0.89189*(164.6573+272.38*(1+21*10/165.82))-Vcmax*(164.6573-(5*21/2.605459)))))/(-2/gi)
if (is.nan(f.1)) f.1 = 1e30
# Second data set
f.2= (-(((Vcmax-0.89189)/gi)+164.3077+272.38*(1+2*10/165.82))+sqrt((((Vcmax-0.89189)/gi)+164.3077+272.38*(1+2*10/165.82))^2-4*(-1/gi)*(0.89189*(164.3077+272.38*(1+2*10/165.82))-Vcmax*(164.3077-(5*2/2.605459)))))/(-2/gi)
if (is.nan(f.2)) f.2 = 1e30
# Values here are the measured values. f.1 and f.2 should be equal or close to the value on their left.
y.1 = 7.478327 - f.1
y.2 = 12.73134 - f.2
# This should be close to 0.
y = y.1^2 + y.2^2
return(y)
}
res <- optim(par=c(0.15,50), fn=f, lower=c(0,0), upper=c(1,250), method="L-BFGS-B")
> f.1(res$par)
[1] 7.478115
> f.2(res$par)
[1] 12.73143
>
Ravi.
____________________________________________________________________
Ravi Varadhan, Ph.D.
Assistant Professor,
Division of Geriatric Medicine and Gerontology
School of Medicine
Johns Hopkins University
Ph. (410) 502-2619
email: rvaradhan at jhmi.edu
----- Original Message -----
From: Guillaume Théroux Rancourt <Guillaume.Theroux-Rancourt at fsaa.ulaval.ca>
Date: Thursday, February 4, 2010 5:31 pm
Subject: Re: [R] Minimizing two non-linear functions with genoud - Trying to minimize or converge near zero
To: "r-help at r-project.org" <r-help at r-project.org>
> Thank you Ravi!
>
> What I forgot to mention in my first post is that gi should be
> between [0,1], while Vcmax is usually between [0,150], but can be
> higher [0,250]. That was the Domains of the genoud function initially.
>
> Also, in this example, f.1 should equal 7.478 and f.2 = 12.731 with
> the same Vcmax and gi.
>
> Using optim, I see that it is very sensitive to the starting
> parameters. Using c(0.15, 100) give parameters close to what should be
> expected, but it is still not close enough to 7.478 for f.1 (for example).
>
> Thank you for your help,
>
> Guillaume
>
> > I do not understand completely what you are trying to do, but may
> be this works for you!?
> >
> > f=function(x) {
> >
> > # x = c(0.16,80)
> > Vcmax = x[2]
> > gi = x[1]
> >
> > # First dataset
> > f.1=function(x){
> > (-(((Vcmax-0.89189)/gi)+164.6573+272.38*(1+21*10/165.82))+sqrt((((Vcmax-0.89189)/gi)+164.6573+272.38*(1+21*10/165.82))^2-4*(-1/gi)*(0.89189*(164.6573+272.38*(1+21*10/165.82))-Vcmax*(164.6573-(5*21/2.605459)))))/(-2/gi)
> > }
> >
> > # Second data set
> > f.2=function(x){
> > (-(((Vcmax-0.89189)/gi)+164.3077+272.38*(1+2*10/165.82))+sqrt((((Vcmax-0.89189)/gi)+164.3077+272.38*(1+2*10/165.82))^2-4*(-1/gi)*(0.89189*(164.3077+272.38*(1+2*10/165.82))-Vcmax*(164.3077-(5*2/2.605459)))))/(-2/gi)
> > }
> >
> > # Values here are the measured values. f.1 and f.2 should be equal
> or close to the value on their left.
> > y.1 = abs(7.478327 - f.1(x))
> > y.2 = abs(12.73134 - f.2(x))
> >
> > # This should be close to 0.
> > y = (y.1 - y.2)^2
> >
> > return(y*y)
> > }
> >
> > dom = matrix(c(0,0,200,1.5), 2, 2)
> >
> > res <- optim(par=c(0.15,100), fn=f, method="BFGS") # no need for
> "genoud" here
> >
> > # First dataset
> > f.1=function(x){
> > Vcmax = res$par[1]
> > gi = res$par[2]
> > (-(((Vcmax-0.89189)/gi)+164.6573+272.38*(1+21*10/165.82))+sqrt((((Vcmax-0.89189)/gi)+164.6573+272.38*(1+21*10/165.82))^2-4*(-1/gi)*(0.89189*(164.6573+272.38*(1+21*10/165.82))-Vcmax*(164.6573-(5*21/2.605459)))))/(-2/gi)
> > }
> >
> > # Second data set
> > f.2=function(x){
> > Vcmax = res$par[1]
> > gi = res$par[2]
> > (-(((Vcmax-0.89189)/gi)+164.3077+272.38*(1+2*10/165.82))+sqrt((((Vcmax-0.89189)/gi)+164.3077+272.38*(1+2*10/165.82))^2-4*(-1/gi)*(0.89189*(164.3077+272.38*(1+2*10/165.82))-Vcmax*(164.3077-(5*2/2.605459)))))/(-2/gi)
> > }
> >
> >
> > f.1(res$par)
> > f.2(res$par)
> >
> >
> > Hope this is helpful,
> > Ravi.
> >
> > ____________________________________________________________________
> >
> > Ravi Varadhan, Ph.D.
> > Assistant Professor,
> > Division of Geriatric Medicine and Gerontology
> > School of Medicine
> > Johns Hopkins University
> >
> > Ph. (410) 502-2619
> > email: rvaradhan at jhmi.edu
> >
> >
> > ----- Original Message -----
> > From: Guillaume Théroux Rancourt <Guillaume.Theroux-Rancourt at fsaa.ulaval.ca>
> > Date: Thursday, February 4, 2010 3:02 pm
> > Subject: [R] Minimizing two non-linear functions with genoud -
> Trying to minimize or converge near zero
> > To: "r-help at r-project.org" <r-help at r-project.org>
> >
> >
> >> Hello R users,
> >>
> >> I am trying to minimize two functions with genoud. It is actually
> one
> >> function with two sets of data, each of them having two unknown
> >> variables (called Vcmax and gi) which have the same value in each
> of
> >> the function. They are called f.1 and f.2 in the code below.
> >>
> >> My objective to minimize the functions in order to get the two
> >> variables equal in each of the functions. Furthermore, I have a
> >> measured comparison value for each of the function expression, and
> the
> >> results of f.1 and f.2 should be very close or equal to their
> measured
> >> value, so that measured.1 - f.1 = 0.
> >>
> >> I have been able to run genoud with the code below. However, I
> >> haven't been able to restrain the values of the difference between
> the
> >> measured and estimated value to 0. I am fairly new at writing R
> >> functions and I think there might be something I haven't written
> that
> >> makes the output parameters of genoud not replicable.
> >>
> >> I have made several runs of this function and when comparing with
> the
> >> measured value, I got answers between 1 and 12, where it should
> have
> >> been very close to 7.47.
> >>
> >> This example has already been solved with the solver Excel add-in
> and
> >> theh result are:
> >> Vcmax = 104.32, gi = 0.11
> >>
> >> The values were also estimated using another approach and they are:
> >> Vcmax = 64.48, gi = 0.28
> >>
> >>
> >> Here is my code.
> >>
> >>
> >> ######
> >>
> >> f=function(x) {
> >>
> >> x = c(0.16,80)
> >> Vcmax = x[2]
> >> gi = x[1]
> >>
> >> # First dataset
> >> f.1=function(x){
> >> (-(((Vcmax-0.89189)/gi)+164.6573+272.38*(1+21*10/165.82))+sqrt((((Vcmax-0.89189)/gi)+164.6573+272.38*(1+21*10/165.82))^2-4*(-1/gi)*(0.89189*(164.6573+272.38*(1+21*10/165.82))-Vcmax*(164.6573-(5*21/2.605459)))))/(-2/gi)
> >> }
> >>
> >> # Second data set
> >> f.2=function(x){
> >> (-(((Vcmax-0.89189)/gi)+164.3077+272.38*(1+2*10/165.82))+sqrt((((Vcmax-0.89189)/gi)+164.3077+272.38*(1+2*10/165.82))^2-4*(-1/gi)*(0.89189*(164.3077+272.38*(1+2*10/165.82))-Vcmax*(164.3077-(5*2/2.605459)))))/(-2/gi)
> >> }
> >>
> >> # Values here are the measured values. f.1 and f.2 should be equal
> or
> >> close to the value on their left.
> >> y.1 = (7.478327 - f.1(x))
> >> y.2 = (12.73134 - f.2(x))
> >>
> >> # This should be close to 0.
> >> y = y.1 - y.2
> >>
> >> return(y)
> >> }
> >>
> >> dom = matrix(c(0,0,200,1.5), 2, 2)
> >>
> >> res = genoud(f, nvars=2, max=FALSE,Domains=dom,pop.size=5000,print.level=0)
> >>
> >>
> >> # In order to test the results to see I the estimated variables
> make
> >> the "test" function = 7.478327 or near.
> >> # This is the same as f.1
> >>
> >> test=function(Vcmax, gi){
> >> (-(((Vcmax-0.89189)/gi)+164.6573+272.38*(1+21*10/165.82))+sqrt((((Vcmax-0.89189)/gi)+164.6573+272.38*(1+21*10/165.82))^2-4*(-1/gi)*(0.89189*(164.6573+272.38*(1+21*10/165.82))-Vcmax*(164.6573-(5*21/2.605459)))))/(-2/gi)
> >> }
> >>
> >> test(res$par[1],res$par[2])
> >>
> >> ## End
> >>
> >>
> >> Thank you for your help!
> >>
> >>
> >> Guillaume Théroux Rancourt
> >> Ph.D. candidate --- Plant Biology
> >> Université Laval, Québec, QC, Canada
> >> guillaume.theroux-rancourt.1 at ulaval.ca
> >>
> >> ______________________________________________
> >> R-help at r-project.org mailing list
> >>
> >> PLEASE do read the posting guide
> >> and provide commented, minimal, self-contained, reproducible code.
>
>
> ______________________________________________
> R-help at r-project.org mailing list
>
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list