# Re: [R] help with optimize statement

From: Jost Burkardt <jost.burkardt_at_web.de>
Date: Sat 25 Feb 2006 - 07:17:11 EST

Hi Tom,

first I can't see, why you think the crossing-point should be 290, look at the plot

x <- seq(200, 500, 10)
plot( exp(2.4591201+x/(-0.4015233+x)) ~ x, ylim=c(0,40), type="l") lines(5.924e-5*x+3.437 ~x)

2) For optimizing a function of 1 parameter, you should use optimize instead of optim

3) As your Function return the difference of the michaelis-menton eqn and the line, optimize will maximize the difference, you could use the sum of squares:

solveEqn<-function(x,vals){
Vmax<-vals[1]
Ks<-vals[2]
m<-vals[3]
c<-vals[4]

diff<-0
mmVal<-exp(Vmax+x/(Ks+x))
slVal<-x*m+c

diff<-mmVal-slVal
return((mmVal-slVal)^2)
}

optimize(solveEqn,c(0,1e6),vals=c(2.4591201,-0.4015233,5.924e-5,3.437))

x <- seq(0, 1e6, len=100)
plot( exp(2.4591201+x/(-0.4015233+x)) ~ x, ylim=c(0,40), type="l") lines(5.924e-5*x+3.437 ~x)

Jost Burkardt

tom wright wrote:
> Can some help me spot what I'm doing wrong here.
> I have two equations, one a michalis-menton eqn and one a straight line.
> I need to work out where they cross.
>
> I created the function:
> solveEqn<-function(x,vals){
> Vmax<-vals[1]
> Ks<-vals[2]
> m<-vals[3]
> c<-vals[4]
> diff<-0
> mmVal<-exp(Vmax+x/(Ks+x))
> slVal<-x*m+c
>
> diff<-mmVal-slVal
> return(diff)
> }
>
>>optim(c(200,500),solveEqn,vals=c(2.4591201,-0.4015233,5.924e-5,3.437))
>
> Error in optim(c(200, 500), solveEqn, vals = c(2.4591201, -0.4015233, :
> objective function in optim evaluates to length 2 not 1
>
>
> If i replace return(diff) with sum(diff) then the optim function runs
> but returns the wrong value (which should be about 290).
>
> Many thanks
> Tom
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help