[R] Defining a variable outside of optim or differential equation solver.

Thomas Petzoldt thpe at simecol.de
Fri Jul 20 14:16:26 CEST 2012

Hi Tjun Kat,

you can define variables outside the ode function, but normally NOT 
state variables, because their values need to be updated by the solver 
during the simulation process.

But, if you want to block this for any debugging purposes and want to 
e.g. fix a derivative to a certain value, even this is possible. Note 
however that this is a very special case and I suspect that you don't 
want this.

Can you please tell, why you want to define states outside? I guess you 
want to emulate a feature that is already available in deSolve, e.g. 
forcings or events. In that case, please have a look into the 
documentation and one of the papers tutorial slides etc. that can be 
found on:


Note also that your code contains 3 errors:

1) The call must be "function(t, y, p)", i.e. with "p" even if this is 
not required by the model, because ode needs this interface.

2) the closing parenthesis ")" of list is missing.

3) dvdpol vs. vdpol

Hope it helps

Thomas Petzoldt

On 7/18/2012 3:59 AM, Tjun Kiat Teo wrote:
 > This is applicable to either using optim  or the differential equation
 > solver or any similar solver
 > Suppose I want to use the differential equation solver and this is my 
 > d<-y[2]
 > vdpol<-function(t,y)
 > {
 > list(c(1,
 >         d,
 >         3,
 >         4
 >        )
 > }
 > stiff<-ode(y=rep(0,4),times=c(0,1),func=dvdpol,parms=1)
 > The thing is I want d to be composed of one of state variables in the
 > differential function vdopl. Can it be done ?
 > tjun kiat
 > ______________________________________________
 > R-help at r-project.org mailing list
 > https://stat.ethz.ch/mailman/listinfo/r-help
 > PLEASE do read the posting guide 
 > and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list