[R] Minimizing two non-linear functions with genoud - Trying to minimize or converge near zero

Ravi Varadhan rvaradhan at jhmi.edu
Thu Feb 4 21:49:46 CET 2010


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(1,1), 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.



More information about the R-help mailing list